473,396 Members | 1,871 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,396 software developers and data experts.

Referencing Form from a module

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


Nov 20 '05 #1
2 926
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

"Ron Dahl" <rd***@ghp.com> wrote in message
news:On**************@TK2MSFTNGP12.phx.gbl...
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

Nov 20 '05 #2
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
"Ron Dahl" <rd***@ghp.com> wrote in message
news:On**************@TK2MSFTNGP12.phx.gbl...
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

Nov 20 '05 #3

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

Similar topics

4
by: mplogue | last post by:
I have a form (frmMain) with a subform (frmSub), each with enumerated fields of the same name (txt1, txt2, etc). I'm trying to make a function that will take the values for each field in frmMain,...
6
by: Ken Breit | last post by:
I have an ActiveX control that I would like to make available from any form in my application. I figured the best way was to create a reference to it, and then have a module that will create the...
17
by: Paul Helmuth | last post by:
All, (here's an easy one)... This is probably a stupid question - please bare with me as I am new to dotNet. How does one reference objects on a form from a module? In 6.0 you could simply...
21
by: cmd | last post by:
I have code in the OnExit event of a control on a subform. The code works properly in this instance. If, however, I put the same code in the OnExit event of a control on a Tab Control of a main...
3
by: Peter | last post by:
Hi, I have a project with an MDI parent form (Main), which can contain any number of MDI children (Document). The Document form contains several user controls, designed as separate projects and...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.