Referencing Form from a module

R

Ron Dahl

I have created a project with a form called form1 and a module called
module1.
Form1 contains two buttons, button1 and button1.

The event code for button1 is:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

DisableButton2()

End Sub

The procedure code in module1 is:

Public Sub DisableButton2()

Dim MyForm As Form1 = New Form1

MyForm.Button2.Enabled = False

End Sub

This code does not disable button1 and there is no error message.

What am I doing wrong?

Thanks in advance for any help.

Ron Dahl
 
S

Samuel L Matzen

Ron,

In the DisableButton2 method you are creating a new instance of Form1 and
disabling the button on this new instance. When the DisableButton2 method
ends this new instance of Form1 is distroyed.

Change your DisableButton2 method to look something like:

Public Sub DisableButton2( byval aForm as Form1)
aForm.Button2.Enabled=False
End Sub

And change the call to this method to be something like:

DisableButton2(Me)

This will send the DisableButton2 method a reference to the instance of
Form1 you are seeing on the display and onwhich you are clicking the button.

-Sam Matzen
 
P

Paul Wilson

You need to pass the reference of the form to manipulate it..
(Cuz what you have done is instantiated a new instance of the form & tried
to change that, & not this new instance)

Below is my solution,

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

DisableButton2(Me)

End Sub

The procedure code in module1 is:

Public Sub DisableButton2(ByRef MyForm as Form1)

MyForm.Button2.Enabled = False

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top