When we click the "Click me" button, it just enable/disable the other button. But it isn't working! :(
How to fix it?
Thanks.
Printable View
When we click the "Click me" button, it just enable/disable the other button. But it isn't working! :(
How to fix it?
Thanks.
The problem is simple...
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:
And then right-click your project file in the solution explorer window, and set the startup object to Main().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
Is there any other way else?
On VB6 it works fine because we do not need to write:
We just write:VB Code:
Dim frm As New Form1()
Is there a way to do it on .NET?VB Code:
Form1.Button1.Enabled = True
Thanks.
Why are using a module then? Just put it in the Form1 code..
I agree! Use:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Button1.Enabled = Not Button1.Enabled
End Sub
You are right. This example is only to show what is not working.Quote:
Originally posted by nemaroller
Why are using a module then? Just put it in the Form1 code..
Do you have other idea to control form's controls within a module?
Thanks.
Quote:
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.
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.Quote:
Originally posted by PeteD
But why would you want to control a form's controls inside a module?
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.Quote:
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?
Can you explain the situation a bit more? Do all of your forms have a button2 which you have to diasable?
All I need to do is to get this working...
I don't mind if it will or won't have a module, etc.
Thank you again.
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.
VB Code:
Me.Button2.Enabled=Not Me.Button2.Enabled
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
Here is your example back with yet another way to handle this.
(You'll need to convert if from 2003)
I havn't looked at Endeeis's sample but you could do it like this:
VB Code:
Module Module1 Public Sub ChangeButtonEnabledState(ByVal btn As Button) btn.Enabled = Not btn.Enabled End Sub End Module 'And then on the in the form code Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ChangeButtonEnabledState(Button2) End Sub
Seems like an odd thing to want to do though!! :D
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.Quote:
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.