Results 1 to 4 of 4

Thread: Control form from another form

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2003
    Posts
    2

    Question Control form from another form

    How can we control another form from another form? I mean we control form A from from B. For example, if we click button OK on form A, the action will occur on form B.
    If ya smelll...

  2. #2
    Fanatic Member Mr.No's Avatar
    Join Date
    Sep 2002
    Location
    Mauritius
    Posts
    651
    Gabriell

    You can refer to controls in formB from formA as follows:
    VB Code:
    1. ' cmdUpdate event is in form A
    2.   private sub cmdUpdate_Click()
    3.        frmFormB.txtCustid.Text = 123
    4.   End Sub

    I personally don't like this technique of explicitly refering to a control on another form. You can alternatively add a public method in Form B and then invoke that method from form A

    VB Code:
    1. ' Code in form B
    2. Public sub UpdateCustomerID(byval custid as string)
    3.    txtCustid.Text = custid
    4. End sub
    5.  
    6.  
    7. ' code in form A
    8.  
    9.   ' cmdUpdate event is in form A
    10.   private sub cmdUpdate_Click()
    11.        frmFormB.UpdateCustomerID("123")
    12.   End Sub
    Using VB.NET 2003/.NET 1.1/C# 2.0
    http://del.icio.us/rajoo
    Blow your mind, smoke gunpowder
    Ashes to ashes, dust to dust
    If God won't have you, the devil will. - Author unknown
    Don't follow me, I'm lost too ...

  3. #3
    Registered User
    Join Date
    Nov 2002
    Location
    Växjö, Sweden
    Posts
    314
    Send a instance of the first form to the second form when its opened. To do that rewrite the constructor of the second form to take a form as an argument. Something like this...

    Code:
    Dim baseForm as Form1
    Public Sub New(frm as Form)
    baseform = frm
    
    InitializeComponent()
    
    End Sub
    Then you have a reference to the form.

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

    Re: Control form from another form

    Originally posted by Gabriell
    How can we control another form from another form? I mean we control form A from from B. For example, if we click button OK on form A, the action will occur on form B.
    or you could declare instances of your forms in a module as follow:

    VB Code:
    1. 'in a module
    2. dim frm1 as new form1
    3. dim frm2 as new form2
    4.  
    5. 'in a form :
    6. frm2.button1.name ="blah"

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