|
-
Apr 25th, 2003, 01:56 PM
#1
Thread Starter
I wonder how many charact
Passing events
Say you built a usercontrol....
and this user control creates a bunch of buttons on it with the names of strings contained in a collection....
So...
dim r as new usercontrol1(myStringCollection)
Now, for as many x amount of strings there are in this collection, you have x amount of buttons...
How would you pass the Click event of each dynamic created button back to the parent form of this usercontrol, to let it know a selection was made?
Last edited by nemaroller; Apr 25th, 2003 at 02:01 PM.
-
Apr 25th, 2003, 02:08 PM
#2
PowerPoster
Create an event in your usercontrol that you raise when a button is clicked. In the arguements you pass the event, you should pass which button was clicked.
-
Apr 25th, 2003, 02:16 PM
#3
Thread Starter
I wonder how many charact
Yea... I got that....
I just can't figure out how to pass the event to the parent form...
You see, the usercontrol, lets call her "Desire" , contains anywhere from 1 to 14 dynamically created buttons.... when I first started, i put the addressed eventhandler in the usercontrol... and it works there.... that's all dandy...
But I also want to pass the event back to the form that contains "Desire"...., because the form dynamically created her also.....
So what I need is this:
Form1 creates Desire, passes a collection of strings to put on her buttons.. Dim r as New Desire(someStringCollection)
Desire gets created... she creates a panel which contains 1-20 buttons on her....
When a button gets pushed... I need Form1 to know which one...
-
Apr 25th, 2003, 02:24 PM
#4
Did you give "Desire" a click event or something of the fashion?
Then you just use RaiseEvent to raise it up to the form. As long as the form is set to recieve events (either AddHandler or withevent and Handles) then it will be notified of the event from "Desire".
-
Apr 25th, 2003, 03:14 PM
#5
Thread Starter
I wonder how many charact
In the Form1 I set a
VB Code:
AddHandler Desire.Click , AddressOf myForm1Click
In Desire, she has a 2 panels (not dynamically created), the second contained within the first....
Its in that second panel Desire dynamically creates a range of buttons....
And I used this in Desire's InitializeComponent :
VB Code:
Dim r As ClassLibrary1.RoundButton
Dim y As Object
'for each string in the passed collection, create a button
'with that strings contents as the Caption...
For Each y In myx
Dim a As String
r = New ClassLibrary1.RoundButton()
r.Text = y.ToString
r.Name = y.ToString
r.Size = New System.Drawing.Size(80, 40)
r.Location = New System.Drawing.Point(10, h)
Panel2.Controls.AddRange(New System.Windows.Forms.Control() {r})
AddHandler r.Click, AddressOf [color=red]myeventhandler_click[/color]
h += 50
Next
And in Desire's myeventhandler_click
VB Code:
Private Sub myeventhandler_click(ByVal sender As Object, ByVal e As System.EventArgs)
If TypeOf sender Is Button Then
' Let the user know what Button was pressed.
MsgBox(CType(sender, Button).Name & " with index " & CType(sender, Button).Tag, _
MsgBoxStyle.OKOnly, Me.Text)
End If
End Sub
She catches the click events from the dynamically created buttons, but how would I pass those events back to Form1, so it knows a button (or selection) has been made. Because obviously, Form1 only receives a click event when Desire is clicked, not her buttons...
Last edited by nemaroller; Apr 25th, 2003 at 03:17 PM.
-
Apr 25th, 2003, 03:25 PM
#6
Desire need to raise its own click or whatever event that you add to it. The form then must have a handler for said event.
VB Code:
'in desire
Public Event Click(sender as object,e as eventargs)
Private Sub myeventhandler_click(ByVal sender As Object, ByVal e As System.EventArgs)
If TypeOf sender Is Button Then
' Let the user know what Button was pressed.
MsgBox(CType(sender, Button).Name & " with index " & CType(sender, Button).Tag, _
MsgBoxStyle.OKOnly, Me.Text)
[b]RaiseEvent Click(sender,e)[/b]
End If
End Sub
Then just follow normal procedures in the form for handling events.
-
Apr 25th, 2003, 03:31 PM
#7
Thread Starter
I wonder how many charact
YOU ROCK ! It works solid!
-
Apr 25th, 2003, 06:29 PM
#8
Fanatic Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|