adding eventhandler programatically

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hmkaddoura
    New Member
    • Feb 2008
    • 18

    adding eventhandler programatically

    Hi all,

    I have a question...

    I'm creating tabs programmaticall y using vb.net . in each tab that is created there is listbox, and 4buttons (also added by coding).

    when i change the listbox selected item, an event should be called, lets say a messagebox will appear with the selected index of that tab.

    I want to know how can I make this handler work for all the listbox that I create in different tabs?

    What do you think? any advice?

    Regards,
    HK
  • dzenanz
    New Member
    • Feb 2008
    • 45

    #2
    1. make handler as target for SelectedIndexCh anged event of all listboxes.
    2. in handler, cast sender to listbox, and then use it's parent property (parent should be TabControl), which has TabIndex property, c#:
    ((TabControl)(( (ListBox)sender ).Parent)).TabI ndex

    HTH, Dženan

    Comment

    • hmkaddoura
      New Member
      • Feb 2008
      • 18

      #3
      Originally posted by dzenanz
      1. make handler as target for SelectedIndexCh anged event of all listboxes.
      2. in handler, cast sender to listbox, and then use it's parent property (parent should be TabControl), which has TabIndex property, c#:
      ((TabControl)(( (ListBox)sender ).Parent)).TabI ndex

      HTH, Dženan
      how can I make 1? (do u have code)

      Comment

      • dzenanz
        New Member
        • Feb 2008
        • 45

        #4
        doubleclicking on one of the listboxes will make a handler named something like listBox1_Select edIndexChanged. click on second listbox, open it's property page, go to it's events, find SelectedIndexCh anged, and in dropdown list next to it choose listBox1_Select edIndexChanged. Repeat procedure for as many listboxes as you like.
        If you want to programatically add event handlers, use syntax (copied from form1.desinger. cs):
        Code:
        listBox2.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
        Finally, you should have handler that looks like this:
        Code:
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.Text /*form title*/= ((TabControl)(((TabPage)(((ListBox)sender).Parent)).Parent)).SelectedIndex.ToString();
        }
        and sets forms's title to 0, 1, 2, ... depending on tab's index in which listbox was nested.
        note:
        sender=listbox
        sender.parent=t abpage
        sender.parent.p arent=tabcontro l
        tabcontrol has property selectedindex, and that is what you need

        ps. This is C# synthax.

        Comment

        • hmkaddoura
          New Member
          • Feb 2008
          • 18

          #5
          Thanks for the reply..

          the first one didn't work for me. but your hint helped me to make it working after doing some search where i found the solution. it is:

          [CODE=VBNET]AddHandler ListBox1new.Sel ectedIndexChang ed, AddressOf ListBox1_Select edIndexChanged
          [/CODE]

          and there is no need to remove the [CODE=VBNET]Handles ListBox1.Select edIndexChanged[/CODE] from [CODE=VBNET]Private Sub ListBox1_Select edIndexChanged( ByVal sender As System.Object, ByVal e As System.EventArg s) Handles ListBox1.Select edIndexChanged[/CODE]
          because when I removed it, the original ListBox1 didn't work ;)

          and this worked very nice :)

          Many thanks
          Last edited by Plater; Feb 18 '08, 03:43 PM. Reason: Added type tag to [CODE] so people understood VBNET

          Comment

          • dzenanz
            New Member
            • Feb 2008
            • 45

            #6
            I knew this is more the question of syntax, and less of algorithm, but C# code helps more than no code at all :D

            Comment

            Working...