Click here to Skip to main content
15,908,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a user control contains a panel and a button on it.
When I click on the button I need to raise a click event for the control.

I did the following but control never going to the following line

panel_Click(Byval Sender as object, byval e as system.event args)

I wrote following code on usercontrol1.vb
----------------------------------------
VB
public event panelClick(byval sender,byval e)

Public sub New()
  AddHandler button1.click, Addressof panel1_Click
end sub

private sub Button1_Click(Byval....)
  RaiseEvent  Panel1Click(sender,e)
ens sub


Hope you can solve this
Posted
Updated 17-Aug-10 22:43pm
v3
Comments
Dalek Dave 18-Aug-10 4:43am    
Minor Edit for Spelling and Grammar.

Double-click on the button in design view and the event will be created for you. Or select the button in design view and click on the appropriate event in the properties window; again, the event will be created for you.
 
Share this answer
 
Comments
PatilVL 18-Aug-10 5:52am    
Your answer is too simple. Any novice knows how to get event handler by clicking the component in design view. What he wants is to raise event for a component from some other event!
R. Giskard Reventlov 18-Aug-10 6:03am    
Evidently not. In any case I answered his specific question: he didn't ask about raising the event from anywhere else: he wanted to raise it form the button but I guess you were too lazy or stupid to read the question properly so don't be such a rude git - didn't notice you answering the question.
PatilVL 19-Aug-10 3:11am    
He didn't ask about raising from anywhere else?!? I quote the question again "I have a user control contains a panel and a button on it.
When I click on the button I need to raise a click event for the control."
Does it mean to you that he is asking about event hadler for the button? Who is too lazy or stupid etc...
R. Giskard Reventlov 19-Aug-10 3:30am    
In case you didn't know a button is a control. Either answer the question yourself, if you are so clever, or piss off.
I'm currently learning about raising events and remembered your question. This isn't exactly what you asked but it has similar functionality. It's just a test program so is very simple but you could change it to suit your needs.

I have a Windows form (Form1) with a panel (Panel1) and two buttons (Button1 and Button2) placed in the panel. When the form loads I set the panel background colour. Then the colour changes depending on which button is clicked. Of course the colour changes could be handled directly in the Button.Click events, but then we wouldn't learn anything!

VB
Imports System.Drawing

Public Class Form1

    Public WithEvents mColour As tstChangeColour


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Panel1.BackColor = Color.Azure  ' Start off with this colour
        mColour = New tstChangeColour   ' Create new instance
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        mColour.selectColour(Color.DarkTurquoise)  ' Uses delegate to raise event
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        mColour.selectColour(Color.DarkSalmon)     ' Uses delegate to raise event
    End Sub

    Private Sub mColour_toggleColour(ByVal newCol As System.Drawing.Color) Handles mColour.toggleColour  ' Handles our event
        Me.Panel1.BackColor = newCol
    End Sub

End Class


VB
Public Class tstChangeColour

    Public Event toggleColour(ByVal newColour As System.Drawing.Color)

    Public Sub selectColour(ByVal setColour As System.Drawing.Color)
        RaiseEvent toggleColour(setColour)
    End Sub

End Class


Hope this helps.
 
Share this answer
 
Instead of raising another event, why don't you just put the functionality into a common method, and call that common method from both click event handlers.

Sometimes, the simplest approach is - well - the simplest approach.
 
Share this answer
 
Comments
[no name] 20-Aug-10 15:57pm    
John, the reason is, that I would not then learn how to raise events. It's purely an exercise - nothing practical - and I thought it might help the OP.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900