On your form... in your button click event.... you call Test()...
Which then declares a new form, and disables/enables a button on the new form. By default, the new form's button is enabled, so it disables it.
You see.. you have two forms here... one is instantatied by the IDE when you click RUN (Form1)... the other one is being declared and manipulated in your module1 Class. and that one has a problem though...you haven't set the frm.visible=true
------------------
You should have this in your module:
VB Code:
Module Module1
Public Sub Main()
Dim frm As New Form1()
Application.Run(frm)
End Sub
End Module
And this in your button1_click event:
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Button2.Enabled = False Then
Button2.Enabled = True
Else
Button2.Enabled = False
End If
End Sub
And then right-click your project file in the solution explorer window, and set the startup object to Main().
Last edited by nemaroller; Jul 1st, 2003 at 11:13 PM.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Button1.Enabled = Not Button1.Enabled
End Sub
Originally posted by AlvaroF1 You are right. This example is only to show what is not working.
Do you have other idea to control form's controls within a module?
Thanks.
In .net, there is no real need to use modules. They are only supported to help vb6 programmers, who are used to using them. Behind the scenes, .net creates a class which is globally imported throughout the project, and all of the properties, methods become shared.
What is actually wrong with your sample however, is that Form1 loads intially, end when you call Test(), you create another instance of Form1, which you never display.
Try the following and you will see what I mean:
VB Code:
Module Module1
Public Sub Test()
Dim frm As New Form1
frm.Show()
If frm.Button2.Enabled = False Then
frm.Button2.Enabled = True
Else
frm.Button2.Enabled = False
End If
End Sub
End Module
But why would you want to control a form's controls inside a module? I think you should encapsulate the code relating to a particular form whithin the form itself. If you had many forms with same functionality then this contolling the forms common controls within another class (or even a module if you like), but you would need to use the existing instance of each form to control it rather than creating a new instance.
Originally posted by AlvaroF1 Because I'll call this same function from other forms too. And to not write the same code, I'll declare it Public and use it just "calling" it.
What do you sugest?
You see if you drop a button2 on Form1, then the Form1 class gets a Button1 property (Friend by default) which you can access a) From inside the form itself or b) from another class (or module) in the same project IF you have a reference to the particular instance to the form. You could write a shared method in another class (or a method in a module) that takes as an argument objects of type Form1. In this case you could access Form1's button object and make it disabled, but if you do this, the you will not be able to use the same method for Form2.
Can you explain the situation a bit more? Do all of your forms have a button2 which you have to diasable?
Its only one line of code, hardly seems worth globalizing it. It'd be just as easy if not easier to just stick it in every form. Also you couldn't pass a form object to a shared method because the generic form object doesn't have a button so you'd have to pass the button. So again it would take just as many (actually more) lines of code to move this outside of each form.
I see your trouble is that the button you want is on a different form. Well that is a lil different, you need to get an understanding of instances. There are lots of other threads that cover this same thing really. There is an example of different ways of doing this in the following post: http://www.vbforums.com/showthread.p...hreadid=249630
Last edited by Edneeis; Jul 7th, 2003 at 09:47 AM.
Originally posted by AlvaroF1 Thanks for your help.
As I said, this is only a simple example.
What's the best way to work with more than 1 form if they have many controls?
Thanks.
Well if all your forms inherited from the same form, you could be sure that they all had a Button2, and so you could write a generic method that took YourBaseForm as an argument, and then do the processing. You could also achieve something similar if you implemented the same interface on each form. This interface could expose the Button2 property.