Easy question: How to access control on a form ?

  • Thread starter Thread starter Eh
  • Start date Start date
E

Eh

I have a project containing a WindowsForm and a module and
need to set the text property on a label control on the
form from the module.

here's the code (from MS Framework .Net Doc):
Dim test as Form2 = Form2.ActiveForm
Dim i As Integer
For i = 0 To test.Controls.Count - 1
MsgBox(test.Controls(i).Name)
Next


When I run this I get a System.NullReferenceException:
Object reference not set to an instance of an object

Any ideas anyone ?
 
Hi,
I don't know what you are doing,
But I asume that the error will not show when you change
Dim test as Form2 = Form2.ActiveForm in
Dim test as New Form2
If the result is what you want, that I cannot see with so few code.

I hope this helps a little bit.
Cor
 
The New statement creates a new instance of the form which
I do not want to do. The form is loaded before the call
to function in the module so I need to be able to set the
test object reference to an instance by getting the form
handle which the .ActiveForm property should be doing but
for some reason it isn't.
 
Eh said:
I have a project containing a WindowsForm and a module and
need to set the text property on a label control on the
form from the module.

Why do you need the module?
here's the code (from MS Framework .Net Doc):
Dim test as Form2 = Form2.ActiveForm
Dim i As Integer
For i = 0 To test.Controls.Count - 1
MsgBox(test.Controls(i).Name)
Next


When I run this I get a System.NullReferenceException:
Object reference not set to an instance of an object

Any ideas anyone ?

test is Nothing. If you want to access an object you need a reference. If
you don't have one you have to pass it, e.g. as an argument of the
procedure.
 
Back
Top