473,387 Members | 1,497 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Trying to create a user control to show data in several forms

Hi,
I'm just getting started with VB.Net, and I'm having trouble getting the
routing around of some of the data straight in my mind, which has led me
to the following problem.

Basically, I'm trying to create an object that will monitor the status of
some switches.
I created a User Control to put into a form, on which I want to have five
graphical representations of LEDs, which I've got working, light up and
turn off when the switches are pressed.
This displays fine as far as the actual graphics go, but I'm slightly
confused as to how to then add this control to my main form.

I've tried adding it directly from the My User Controls toolbar and it
give a System.NullReferenceException, which I take to mean that there is
no reference to it in the form's code.
I then added an explicit constructor for the variable that
SwitchStatusDisplay needs to check in MainMenu's _Load function, which
seemed to work in terms of actually moving the data around to various
logical areas of the form code, but didn't stop this error message showing
up in the control itself.

I have (or I'm pretty sure I have) added functions to create it properly
from my test data and testing has shown that the constructor method shown
below does get called properly and goes into the testdata object to fetch
the data properly and stepping through and watching the data shows that
the SwitchStatusArray is being set up properly (it fetches an array of
five Booleans from the testdata to be checked during OnPaint).

However, when OnPaint gets called and it tries to enter the JJ counter
loop, the SwitchStatusArray comes up as empty leading to (I believe) a
completely different System.NullReferenceException, because the
SwitchStatusArray is now empty, having been filled only a moment before by
the constructor...

I've looked around and I think that I've read that the data isn't
persistent in constructs of this type, and is erased every time it is
redrawn.

Hopefully I'm all correct on that so far.

What I cannot now work out how to do is to the proper way to get this
component to check its data every time it is drawn. If its data isn't
persistent, then it can't store a reference to the testdata and so cannot
get the data itself, but I don't know enough about how the form is being
updated generally to put in appropriate instructions to give the component
new data every time it is drawn.

I realise that I could put the code for this directly onto my MainMenu
form, but I want this control to be used at various points in the forms
I'm creating and it seems to me that this method of creating a User
Control is the best way to be able to reuse this display, if only I can
get it working.

Here's the code for the User Control and the relevant bits from my
MainMenu form to help you get the idea.

Time for another round of "Spot the frighteningly basic misunderstanding",
I guess.

Friend Class SwitchStatusDisplay
Inherits System.Windows.Forms.UserControl
#Region " Windows Form Designer generated code "
#End Region
Private SwitchStatusArray() As Boolean
Public Sub New(ByVal test As testdata)
SwitchStatusArray = test.GetSwitchStatus
End Sub

Protected Overrides Sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)
Dim g As Graphics = e.Graphics
Dim BackPen As Pen = New Pen(Color.Black, 1)
Dim LEDPen As Pen = New Pen(Color.Green, 1)
For JJ As Integer = 0 To 4
g.DrawEllipse(BackPen, New Rectangle(20 + (JJ * 25), 10, 10,
10))
If SwitchStatusArray(JJ) = True Then
'g.DrawEllipse(LEDPen, New Rectangle(20 + (JJ * 25), 11,
8, 8))
End If
Next
End Sub
End Class

MainMenu fragments
Dim ls As testdata
Dim SwitchStatusDisplay1 As SwitchStatusDisplay

Private Sub MainMenu_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
tda = New testdata()
SwitchStatusDisplay = New SwitchStatusDisplay(tda)
End Sub
end fragments
--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Aug 22 '06 #1
0 1392

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Bil Muh | last post by:
Esteemede Developers, I would like to Thank All of You in advance for your sincere guidances. I am developing a software using Visual C++ .NET Standard Edition with Windows Form (.NET)...
6
by: sheree | last post by:
I would like to create a query where one of the columns of the queries comes from a combo list box on a form. For example, if my table has the following fields: id name interest1 interest2...
6
by: SamIAm | last post by:
Hi am creating a email application that needs to mail out a very large amount of emails. I have created a multithreaded c# application that using message queuing. I have created a threadpool of 5...
1
by: Rhy Mednick | last post by:
I'm creating a custom control (inherited from UserControl) that is displayed by other controls on the form. I would like for the control to disappear when the user clicks outside my control the...
7
by: Dica | last post by:
i've used the sample code from msdn to create an encyption/decryption assembly as found here: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/SecNetHT10.asp i'm...
5
by: Learner | last post by:
Hello, Here is the code snippet I got strucked at. I am unable to convert the below line of code to its equavalent vb.net code. could some one please help me with this? static public...
6
by: AppleBag | last post by:
I'm having the worst time trying to login to myspace through code. Can someone tell me how to do this? Please try it yourself before replying, only because I have asked this a couple of times in...
4
by: etuncer | last post by:
Hello All, I have Access 2003, and am trying to build a database for my small company. I want to be able to create a word document based on the data entered through a form. the real question is...
15
by: lxyone | last post by:
Using a flat file containing table names, fields, values whats the best way of creating html pages? I want control over the html pages ie 1. layout 2. what data to show 3. what controls to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.