UserControl click event

pachjo

Well-known member
Joined
Dec 12, 2006
Messages
370
Programming Experience
10+
Hi, I have just created my first usercontrol and added to the toolbox and then dragged to my form.

When I click on the various buttons in the usercontrol the code runs as expected that is embedded in the control itself.

However when I try to get the value from the control via its click event nothing happens.

Even if I break point it nothing happens, it does not halt at the breakpoint?

the usercontrol has a panel and a load of buttons which if I click they react accordingly.

Any advice please?

Thanks
 
However when I try to get the value from the control via its click event nothing happens.
Elaborate. Click event of the Usercontrol itself? And where do you click exactly?
 
vincent Kasyoki

Hello there,

Nb. if your are using if ..then ....else then the statement with the breakpoint may be jumped depending on whether the condition is true or not true causing the execution not halt at run time.Please confirm and let me know
thanks
 
Hi,

Ok the usercontrol consists of a panel and 20 buttons.

when a button is clicked a private variable is set accordingly and is accissible as a property of the user control.

When I drop this usercontrol on my form I can click the buttons and the work as a button should.

I then try to capture the click event of the usercontrol and when caught assign the property to a text box on the main form.

What I have just discovered is if I click just outside of the panel in the control the value is passed to my textbox?

So I guess I am misunderstanding the way one uses the usercontrols?

I want to capture the button click inside the control and receive the controls value.

Confused!!

:confused:
 
OK to simplfy things I removed the panel from the usercontrol which is a keyboard mimic but with reduced keys.

I place the usercontrol on my form and every time I click a button the underlying code for this button which is part of the user code assigns the button text value to a usercontrol property.

This property value should be written to a text box on the main form above where I place the usercontrol

When I click the button, lets say 'A', it does not appear in the text box. But is I click any of the area around the button i.e. the usercontrols surface area the letter 'A' appears in the textbox.

I think I am not understanding correctly the concept of calling the correct event?

But the click event code for the 'A' button on the usercontrol is working as it sets the property accordingly.

What I cant seemt to grasp is the way I catch the buttonA.click event inside of the usercontrol?

Does this make sense or am I not describing the problem correctly?

Thanks
 
The UC is a container, the contained controls are independent, both UC and child controls have independent event just like a panel and controls in a form. When you click a button in UC it is the button click event that is raised. Only if you click the UC surface the UC click event is raised (just like the panel example). Depending on what behaviour you want (which I don't understand) you can either declare your own UC events and raise them when appropriate or call protected base methods that raise UC events. For example internally in UC you can catch the button click event and relay this to UC OnClick method like this: MyBase.OnClick(e), and a consumer of the UC can subscribe to the UC Click event to get notified when this button is clicked, but if you have several "Click events" you should declare appropriate event that the consumer of your control can understand, for example AClick and BClick events if you have a A and B button.
 
Absolutely fantastic! Thanks so much, I have eye ache searching for the answer and although I was on the right track could not suss it.

However you have solved this for me and here is what I have:


VB.NET:
' this is the handler for all my buttons inside the UC 

Private Sub button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles btn0.Click, btn1.Click, btn2.Click, .....

        ' get the button that was clicked and set the property 

        Dim objButton As Control

        objButton = DirectCast(sender, Button)

        strKeyPressed = objButton.Text

        ' call the UC on click event

        MyBase.OnClick(e)

    End Sub

VB.NET:
'  on my form where I palced the UC handle it's on click event and set the forms textbox to reflect which key was pressed inside the UC 

Private Sub KeyBoard1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles KeyBoard1.Click

        Me.txtTest.Text = Me.KeyBoard1.KeyValue

End Sub

I am a happy man :D

Thanks again
 
Back
Top