Results 1 to 16 of 16

Thread: How to get this working?

  1. #1

    Thread Starter
    Addicted Member AlvaroF1's Avatar
    Join Date
    Sep 2002
    Location
    SP - Brazil
    Posts
    200

    Unhappy How to get this working?

    When we click the "Click me" button, it just enable/disable the other button. But it isn't working!

    How to fix it?


    Thanks.
    Attached Files Attached Files

  2. #2
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    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:
    1. Module Module1
    2.     Public Sub Main()
    3.         Dim frm As New Form1()
    4.         Application.Run(frm)
    5.  
    6.     End Sub
    7. End Module

    And this in your button1_click event:
    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         If Button2.Enabled = False Then
    3.             Button2.Enabled = True
    4.         Else
    5.             Button2.Enabled = False
    6.         End If
    7.     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.

  3. #3

    Thread Starter
    Addicted Member AlvaroF1's Avatar
    Join Date
    Sep 2002
    Location
    SP - Brazil
    Posts
    200
    Is there any other way else?

    On VB6 it works fine because we do not need to write:

    VB Code:
    1. Dim frm As New Form1()
    We just write:

    VB Code:
    1. Form1.Button1.Enabled = True
    Is there a way to do it on .NET?


    Thanks.

  4. #4
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Why are using a module then? Just put it in the Form1 code..

  5. #5
    Addicted Member PeteD's Avatar
    Join Date
    Jun 2003
    Location
    Sydney
    Posts
    158
    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

  6. #6

    Thread Starter
    Addicted Member AlvaroF1's Avatar
    Join Date
    Sep 2002
    Location
    SP - Brazil
    Posts
    200
    Originally posted by nemaroller
    Why are using a module then? Just put it in the Form1 code..
    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.

  7. #7
    Addicted Member PeteD's Avatar
    Join Date
    Jun 2003
    Location
    Sydney
    Posts
    158
    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:
    1. Module Module1
    2.     Public Sub Test()
    3.       Dim frm As New Form1
    4.       frm.Show()
    5.         If frm.Button2.Enabled = False Then
    6.             frm.Button2.Enabled = True
    7.         Else
    8.             frm.Button2.Enabled = False
    9.         End If
    10.     End Sub
    11. 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.

  8. #8

    Thread Starter
    Addicted Member AlvaroF1's Avatar
    Join Date
    Sep 2002
    Location
    SP - Brazil
    Posts
    200
    Originally posted by PeteD
    But why would you want to control a form's controls inside a module?
    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?

  9. #9
    Addicted Member PeteD's Avatar
    Join Date
    Jun 2003
    Location
    Sydney
    Posts
    158
    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?
    Last edited by PeteD; Jul 5th, 2003 at 09:00 AM.

  10. #10

    Thread Starter
    Addicted Member AlvaroF1's Avatar
    Join Date
    Sep 2002
    Location
    SP - Brazil
    Posts
    200
    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.
    Attached Files Attached Files

  11. #11
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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:
    1. Me.Button2.Enabled=Not Me.Button2.Enabled

  12. #12
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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.

  13. #13
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Here is your example back with yet another way to handle this.
    (You'll need to convert if from 2003)

  14. #14
    Addicted Member PeteD's Avatar
    Join Date
    Jun 2003
    Location
    Sydney
    Posts
    158
    I havn't looked at Endeeis's sample but you could do it like this:


    VB Code:
    1. Module Module1
    2.    Public Sub ChangeButtonEnabledState(ByVal btn As Button)
    3.       btn.Enabled = Not btn.Enabled
    4.    End Sub
    5. End Module
    6.  
    7. 'And then on the in the form code
    8.  
    9.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    10.       ChangeButtonEnabledState(Button2)
    11.     End Sub

    Seems like an odd thing to want to do though!!

  15. #15

    Thread Starter
    Addicted Member AlvaroF1's Avatar
    Join Date
    Sep 2002
    Location
    SP - Brazil
    Posts
    200
    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.

  16. #16
    Addicted Member PeteD's Avatar
    Join Date
    Jun 2003
    Location
    Sydney
    Posts
    158
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width