a way to make just one event handler to handle multiple events from diff. objects ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Shenno
    New Member
    • Sep 2010
    • 59

    a way to make just one event handler to handle multiple events from diff. objects ?

    hi, how is all ??

    i have just got a problem, i have a form with about 30 text boxes.

    i have made these event hundlers to handle got focus and lose focus events
    Code:
            private void gf(object sender, EventArgs e)
            {
                name.BackColor=System.Drawing.SystemColors.Info;
            }
            private void lf(object sender, EventArgs e)
            {
                name.BackColor = System.Drawing.SystemColors.Window;
            }
    and applied it for "name" textbox


    is there a way to apply the same event hundler to all form textboxes without making event handler for every one of my 30 textboxes ??

    thx in advance.
    Last edited by Frinavale; Sep 9 '10, 02:30 PM. Reason: Please do not post your question in bold. It is regarded as yelling online. Removed bold-font style from post.
  • MartijnHoekstra
    New Member
    • Sep 2010
    • 39

    #2
    Yes that is possible..
    Let all textboxes refer to the same eventhandler.

    Within the handler, the 'sender' will refer to the Textbox sending the event.
    Casting the sender being of type 'object' back to type 'Textbox', will allow you to query its name so you can see which one it was.
    You could also make use of the 'Tag' member, place some number in there for your reference

    Comment

    • Shenno
      New Member
      • Sep 2010
      • 59

      #3
      thx ,but i didn't understand very much, can u give me code sample ?

      Comment

      • MartijnHoekstra
        New Member
        • Sep 2010
        • 39

        #4
        What I believe you want to do can be done like this:

        Code:
        private void textBox_GetFocus( object sender,  EventArgs e)
        {
           TextBox tb = sender as TextBox;
           if (tb) tb.BackColor=System.Drawing.SystemColors.Info;
        }
        
        private void textBox_LoseFocus( object sender,  EventArgs e)
        {
           TextBox tb = sender as TextBox;
           if(tb) tb.BackColor = System.Drawing.SystemColors.Window;
        }
        if sender is unable to be casted to a TextBox, tb will be 'null', hence the test.

        now you need to set the same event handler to each textbox you wish.
        Last edited by MartijnHoekstra; Sep 9 '10, 10:40 AM. Reason: Should be 'as' instead of 'As'

        Comment

        • Shenno
          New Member
          • Sep 2010
          • 59

          #5
          Error ^_^

          needs "As" to be ";"

          Comment

          • Shenno
            New Member
            • Sep 2010
            • 59

            #6
            thx, it's solved like this

            Code:
                    private void lf(object sender, EventArgs e)
                    {
                        TextBox tb = (TextBox)sender; 
                         tb.BackColor=System.Drawing.SystemColors.Window; 
                    }

            u still the one <3

            Comment

            Working...