Link to home
Start Free TrialLog in
Avatar of nongravity
nongravity

asked on

Access another forms properties or events...

I have just started VB.NET from VB6. I need to know  how to access another forms events/controls, etc like you could in vb6. I.E.
In a form called form1:

Sub command1_click
form2.visible = True
End sub

or vice versa
I have tried somethign like
Public frm1 as Form1
frm1.visible = true
and can access what i need but when the code runs i get an error about referencing a null object.   Please help.
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

For you to access properties on a form, they would need to be defined as Shared, otherwise you would need an instance of the form:

Public Shared TotalCount() As Integer

or

Dim frm As Form2

If frm Is Nothing Then
  frm = New Form2
End If

Bob

Avatar of nongravity
nongravity

ASKER

I've tried that. And when the button is clicked on form2 it errors out and gives me the error message about referencing a null object. (refering to Dim frm as Form2)
On blank form with a button I added this

button1_click
form2.button1.enabled = true

when this event fires I get that error message
forgot to mention that the button is on form1
geeez, I really need to think of everything to say before i press submit...
I changed the property from private to shared and I can access it that way but I would like to have access to all buttons, menus, controls, etc without having to change each event to "Shared" if possible. I am also upping the points on this to 325 because of my shear noobness to .net
Are you instantiating a new Form2?  If not, then you will get the ubiquitous 'Object variable or with block not set' error.

frm = New Form2

Bob
No, I'm not instancing a new form2.
Well, you're gonna need to if you want to access any of its properties.

Bob
Ok, I understand that now but I can't access form1's properties,events, etc  from form2
Is Form2 a sub-form from Form1, or does it stand by itself?

Bob
Sorry for being clear as mud on this problem...lol.  Its an Mdi app. I create new instances of the "template" forms and can access those from Form1. But on certain events in the Child forms I need update the menu on the Parent form or "Form1". Hope this helps.
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It works exaclty how I need it to.    THANK YOU!!!!!!
Bob,
how would you do the same thing if its not an mdichild?
I will start another thread and give more points if you want.