Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Generic event procedure for mutiple controls.

Generic event procedure for mutiple controls.

Scheduled Pinned Locked Moved C#
helptutorialquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    amatbrewer
    wrote on last edited by
    #1

    I want to be able to create generic event procedures that can be used for a number of controls (the same type) on a form. For example (assume the object sender would always be a text box); private void tbCallBlockPct_Click(object sender, EventArgs e) { sender.SelectAll(); } However when I try somehting like this I get a syntax error that the “object sender does not contain a definition for SelectAll”. If the function is being passed a textbox as the variable sender should not all of its methods and properties be accessable?

    David Wilkes

    P 1 Reply Last reply
    0
    • A amatbrewer

      I want to be able to create generic event procedures that can be used for a number of controls (the same type) on a form. For example (assume the object sender would always be a text box); private void tbCallBlockPct_Click(object sender, EventArgs e) { sender.SelectAll(); } However when I try somehting like this I get a syntax error that the “object sender does not contain a definition for SelectAll”. If the function is being passed a textbox as the variable sender should not all of its methods and properties be accessable?

      David Wilkes

      P Offline
      P Offline
      pmarfleet
      wrote on last edited by
      #2

      sender is of type object, which doesn't have a SelectAll() method in its interface. You should cast sender to an object variable of type TextBox in order to invoke properties/methods particular to this class.

      TextBox tb = (TextBox)sender;
      tb.SelectAll();

      Paul Marfleet

      A 1 Reply Last reply
      0
      • P pmarfleet

        sender is of type object, which doesn't have a SelectAll() method in its interface. You should cast sender to an object variable of type TextBox in order to invoke properties/methods particular to this class.

        TextBox tb = (TextBox)sender;
        tb.SelectAll();

        Paul Marfleet

        A Offline
        A Offline
        amatbrewer
        wrote on last edited by
        #3

        Thanks. That is alomost exactly what I ended comming up with not too long after posting the message. It seemed a bit round about and redundant, so i was not sure about it. Would it be correct to assume that since the tb.SelectAll works (selects the text in the original control) means that the tb is a pointer to the actual text box and not a copy of it? It would seem that you should be able to do it more directly, like (TextBox)sender.SelectAll(); or something similar.

        David Wilkes

        S P 2 Replies Last reply
        0
        • A amatbrewer

          Thanks. That is alomost exactly what I ended comming up with not too long after posting the message. It seemed a bit round about and redundant, so i was not sure about it. Would it be correct to assume that since the tb.SelectAll works (selects the text in the original control) means that the tb is a pointer to the actual text box and not a copy of it? It would seem that you should be able to do it more directly, like (TextBox)sender.SelectAll(); or something similar.

          David Wilkes

          S Offline
          S Offline
          Stefan Troschuetz
          wrote on last edited by
          #4

          amatbrewer wrote:

          Would it be correct to assume that since the tb.SelectAll works (selects the text in the original control) means that the tb is a pointer to the actual text box and not a copy of it?

          Yes.

          amatbrewer wrote:

          It would seem that you should be able to do it more directly, like (TextBox)sender.SelectAll(); or something similar.

          Try this one: ((TextBox) sender).SelectAll();


          "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

          www.troschuetz.de

          1 Reply Last reply
          0
          • A amatbrewer

            Thanks. That is alomost exactly what I ended comming up with not too long after posting the message. It seemed a bit round about and redundant, so i was not sure about it. Would it be correct to assume that since the tb.SelectAll works (selects the text in the original control) means that the tb is a pointer to the actual text box and not a copy of it? It would seem that you should be able to do it more directly, like (TextBox)sender.SelectAll(); or something similar.

            David Wilkes

            P Offline
            P Offline
            Phil J Pearson
            wrote on last edited by
            #5

            It would be safer not to assume that the sender is a TextBox (one day it won't be). Code like this:

            TextBox tb = sender as TextBox;
            if (tb != null)
            {
            tb.SelectAll();
            }
            else
            {
            // handle the error. Maybe just assert - it's a programming error
            }

            Phil


            The opinions expressed in this post are not necessarily those of the author, especially if you find them impolite, inaccurate or inflammatory.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups