Results 1 to 17 of 17

Thread: [RESOLVED] AddHandler Question

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,051

    Resolved [RESOLVED] AddHandler Question

    I need some reading material on how to set up an
    event handler using AddHandler.

    I'm having no luck using online help.
    I think I could use an example.

    I've added some button
    controls at run-time but I havent yet
    determined how to dynamically add their associated
    handlers using AddHandler.

    I'm using VB2005.net

    Thanks

  2. #2
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: AddHandler Question

    like so

    VB Code:
    1. Dim Button1 As New Button
    2.             AddHandler Button1.Click, AddressOf Button1_click
    3.  
    4.     Private Sub Button1_click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    5.         'do stuff
    6.     End Sub
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,051

    Re: AddHandler Question

    Well, I got that part during testing with a button of a specific name.
    However, I'm allowing the user to name the buttons.
    So I'm wondering if I can tailor the event name to the name the user gives the button.

    Thanks

  4. #4
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: AddHandler Question

    I am not really sure what you are asking. So you want the user to be able to create a button and then define the text on the button correct? do all the created buttons run through the same handler? If so, you can get which button was pressed.

    VB Code:
    1. Dim btn as Button = CType(sender, Button)

    Not really sure if this is what you are looking for, but it would help if you could post some of your code, so I can geta better idea
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,051

    Re: AddHandler Question

    Ah, I see. Thank you bmahler.
    I can just use the same handler for each button added.
    Then determine within the event which button was clicked.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,051

    Re: AddHandler Question

    Of course, now I must find how to decide which button was clicked.
    Keeping in mind that I won't know the names of
    the buttons because they will be named at run-time.

  7. #7
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: AddHandler Question

    you got it, once you use the line that i posted above, you can get the btn.Text or btn.Tag or whatever you need.

    If I have fully answered your question, please remember to mark this thread resolved.
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,051

    Re: AddHandler Question

    I'll work with the line of code you provided.
    I may have some more questions on how
    to use that line of code.

    Thanks

  9. #9
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: AddHandler Question

    sure thing
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,051

    Re: AddHandler Question

    Ok, that works great.
    Thanks.
    I would like to read up on what was going on
    there. We designated that button as
    being of a type that can send messages?
    Or what?

    Anyway that will work for me.
    Now, I need to name an array after the name
    that the user entered for the button.
    Any suggestions?
    The user enters the button name and from that
    point on when the user clicks on that button,
    I must choose which array will be used to fill
    a list.

  11. #11
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: AddHandler Question

    here is the msdn page that describes th addHandler event
    http://msdn2.microsoft.com/en-us/library/7taxzxka.aspx

    you could try to store the name of the array to be used in the tag of the button. I am not exactly sure how to help you with this part, because I have no idea what your code is doing.
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  12. #12

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,051

    Re: AddHandler Question

    What my code is doing:

    I have a single DataGridView.
    The DataGridView will be used to display the contents
    of an array. Which array depends on the button that is
    clicked (the same buttons the user named).

    The user creates and names the button.
    They then add items to the DataGridView.
    Later they can click on any particular button and
    retrieve the items that they added earlier.

    It seems to me that the logical thing to do is to
    name the arrays after the button name.
    Then when the user clicks a button, I can compare the
    button name with the array names (if possible)
    and retrieve the correct array.

  13. #13
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: AddHandler Question

    I really need to see your code to see what is happening when the user creates these buttons and the arrays.
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  14. #14
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: AddHandler Question

    how bout this, create a structure

    Place these in a module
    VB Code:
    1. Public Structure lists
    2.         Dim name As String
    3.         Dim list() As String
    4.     End Structure
    5.  
    6.     Public myLists() As lists

    Then when a user creates a button you can add and item

    VB Code:
    1. Dim i As Integer = myLists.GetUpperBound(0) + 1
    2.         ReDim Preserve myLists(i)
    3.         myLists(i).name = "name of button user picked"
    4.         ReDim myLists(i).list(1) 'Redim this to the number of items that are going to be entered into the array
    5.         'Add items to the array
    6.         myLists(i).list(0) = "item"
    7.         myLists(i).list(1) = "item"

    Then when the user clicks a button you can retrieve the array

    VB Code:
    1. Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    2.         Dim btn As Button
    3.         btn = CType(sender, Button)
    4.  
    5.         For i As Integer = 0 To myLists.GetUpperBound(0)
    6.             If btn.Text = myLists(i).name Then
    7.                 'you now have you array of data to do whatever with
    8.                 'Array is myLists(i).list()
    9.             End If
    10.         Next
    11. End Sub
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  15. #15

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,051

    Re: AddHandler Question

    I'm studying your code...

  16. #16

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,051

    Re: AddHandler Question

    Thanks bmahler,

    I already posted this once but it disappeared.

    Everything works perfectly.
    It would have taken me days to come up with that.

    Thanks for taking the time.

  17. #17
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: [RESOLVED] AddHandler Question

    No problem
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

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