Results 1 to 8 of 8

Thread: Passing events

  1. #1

    Thread Starter
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704

    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.

  2. #2
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    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.

  3. #3

    Thread Starter
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    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...

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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".

  5. #5

    Thread Starter
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    In the Form1 I set a
    VB Code:
    1. 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:
    1. Dim r As ClassLibrary1.RoundButton
    2.  
    3.         Dim y As Object
    4.      'for each string in the passed collection, create a button
    5.      'with that strings contents as the Caption...  
    6.       For Each y In myx
    7.             Dim a As String
    8.             r = New ClassLibrary1.RoundButton()
    9.             r.Text = y.ToString
    10.             r.Name = y.ToString
    11.             r.Size = New System.Drawing.Size(80, 40)
    12.             r.Location = New System.Drawing.Point(10, h)
    13.             Panel2.Controls.AddRange(New System.Windows.Forms.Control() {r})
    14.             AddHandler r.Click, AddressOf [color=red]myeventhandler_click[/color]
    15.  
    16.             h += 50
    17.         Next

    And in Desire's myeventhandler_click
    VB Code:
    1. Private Sub myeventhandler_click(ByVal sender As Object, ByVal e As System.EventArgs)
    2.         If TypeOf sender Is Button Then
    3.             ' Let the user know what Button was pressed.
    4.             MsgBox(CType(sender, Button).Name & " with index " & CType(sender, Button).Tag, _
    5.            MsgBoxStyle.OKOnly, Me.Text)
    6.         End If
    7.  
    8. 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.

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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:
    1. 'in desire
    2.  
    3. Public Event Click(sender as object,e as eventargs)
    4.  
    5.  
    6. Private Sub myeventhandler_click(ByVal sender As Object, ByVal e As System.EventArgs)
    7.         If TypeOf sender Is Button Then
    8.             ' Let the user know what Button was pressed.
    9.             MsgBox(CType(sender, Button).Name & " with index " & CType(sender, Button).Tag, _
    10.            MsgBoxStyle.OKOnly, Me.Text)
    11.             [b]RaiseEvent Click(sender,e)[/b]
    12.         End If
    13.  
    14. End Sub

    Then just follow normal procedures in the form for handling events.

  7. #7

    Thread Starter
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    YOU ROCK ! It works solid!

  8. #8
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    Ed's da man.

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