By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,968 Members | 1,183 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,968 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

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
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,
If SwitchStatusArray(JJ) = True Then
'g.DrawEllipse(LEDPen, New Rectangle(20 + (JJ * 25), 11,
8, 8))
End If
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:
Aug 22 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.