Results 1 to 2 of 2

Thread: UserControl button not behaving as a button in a Windows Form

  1. #1

    Thread Starter
    Lively Member Amerigo's Avatar
    Join Date
    Dec 2008
    Location
    PSR B1620-26 c-1
    Posts
    126

    Question UserControl button not behaving as a button in a Windows Form

    Greetings Earthlings,
    I have created a button in a UserControl. I have added that button to a WindowsForm in another app and added a buttonclick event to the code. When running (debugging) the app and clicking on the button; nothing happens. It doesn't act like a button at all.

    The code for the button click event in my test app is as follows:

    Code:
    Private Sub Element1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Element1.Click
        MsgBox("The button has been clicked")
    End Sub
    Attached Files Attached Files
    Anyone who does not wonder, is either omnipotent or a fool.
    Amerigoware <<<My Projects

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: UserControl button not behaving as a button in a Windows Form

    Do you really mean UserControl or do you actually mean custom control? There is a difference and I'm guessing that that is the issue.

    A custom control is a control that you create yourself with custom functionality. It would either inherit Control directly or else it would inherit Control indirectly via some other class. For example, if you wanted the behaviour of a button but you wanted to add some custom appearance or behaviour too, you would inherit the Button class and then add code to provide your custom appearance or behaviour.

    A UserControl is a special type of control that is specifically designed to contain multiple child controls that you can then manipulate as a group. For instance, let's say that you wanted the user to be able to select a file to open in multiple places in your app. You might add a UserControl to your project and, to that UserControl, add a Label, TextBox, Button and OpenFileDialog. You only have to wire up the Button to the OpenFileDialog to the TextBox in one place and then you can add it as a unit in various places throughout your app.

    My guess is that what you want is a custom control but what you have created is a UserControl. Your UserControl contains a Button but you have not actually handled the Click event of that Button anywhere so nothing happens when you click it. You have handled the Click event of the UserControl in your form but you never actually click the surface of the UserControl itself, so that event is never raised.

    If you really did want a UserControl with a Button in it to raise an event to the form then you would have to handle the Click event of the Button inside the UserControl and then have the UserControl raise an event of its own that the form could handle, something like this:
    vb.net Code:
    1. Public Class UserControl1
    2.  
    3.     Public Event ButtonClick As EventHandler
    4.  
    5.     Private Sub Button1_Click(sender As System.Object, e As EventArgs) Handles Button1.Click
    6.         Me.OnButtonClick(EventArgs.Empty)
    7.     End Sub
    8.  
    9.     Protected Overridable Sub OnButtonClick(e As EventArgs)
    10.         RaiseEvent ButtonClick(Me, e)
    11.     End Sub
    12.  
    13. End Class
    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Private Sub UserControl11_ButtonClick(sender As Object, e As EventArgs) Handles UserControl11.ButtonClick
    4.         MessageBox.Show("The button has been clicked.")
    5.     End Sub
    6.  
    7. End Class

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