Results 1 to 5 of 5

Thread: [RESOLVED] controlling usercontrol from form

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2010
    Posts
    89

    Resolved [RESOLVED] controlling usercontrol from form

    Hi,

    how do you control USERCONTROL button1_click event from form's button2_click event?

    button 1 = hidden button on usercontrol1
    button2 = visible button on form1

    form1.button2 should click usercontrol.button1

    regards, and thanks in advance

  2. #2
    Hyperactive Member
    Join Date
    Mar 2009
    Posts
    462

    Re: controlling usercontrol from form

    i don't really get what you mean but this is what im thought you asked for
    Code:
    usercontrol.Button1.PerformClick()

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2010
    Posts
    89

    Re: controlling usercontrol from form

    Hi thanks for the reply but that does not work.

    it gives error, "Reference to a non-shared member requires an object reference"



    all i want is (buttonA on form1) to click (buttonB on Usercontrol1) the same time.

  4. #4
    Fanatic Member
    Join Date
    Jun 2008
    Location
    Portland, OR, USA
    Posts
    659

    Re: controlling usercontrol from form

    Assume your UserControl1 has a Button on it Named Button1. Then assume that you have a silly MsgBox placed in the Button1_Click Event handler, as follows:
    Code:
    Public Class UserControl1
    
        Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
            MsgBox("UserControl1 Button CLicked")
        End Sub
    
    End Class
    THEN Assume you have Form1, onto which you have placed an instance of UserControl1 using the designer. Also assume that you have placed Button2on Form1 in the designer, and placed the following in the event handling code for Button2:
    Code:
    Public Class Form1
    
        Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
            Me.UserControl11.Button1.PerformClick()
        End Sub
    
    End Class
    When you click Button2 on Form1, it fires the Button2_CLick Event Handler. Within that method, the Me.UserControl1.Button1.Click Statement at once references the local instance of UserControl1, and calls the PerformCLick method. This fires the UserCOntrol1 Button1_CLick event handler code, which pops up the message box.

    If you are getting that "non-shared member" business still, then you might need to explain to us how you have out your form together. It SOUNDS like somewhere you are not referencing an instance of UserControl1.

    Did you add the usercontrol in the designer, or did you add it programmatically?

    This should work. If it does not, then could you please post your current code for Both UserControl and Form?

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jul 2010
    Posts
    89

    Re: controlling usercontrol from form

    Thanks RunsWithScissors

    works perfectly. SOLVED

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