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. simple C# beginner question:

simple C# beginner question:

Scheduled Pinned Locked Moved C#
questioncsharplearning
7 Posts 5 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.
  • S Offline
    S Offline
    Shimmy Weitzhandler
    wrote on last edited by
    #1

    how do I convert from object to control (the same as CType in VB) private void button_click(object sender, EventArgs e) { MessageBox.Show(sender.Text); } I think I should convert the sender to a button. in VB i don't have to it, it converts it automatically...

    Shimi

    M C 2 Replies Last reply
    0
    • S Shimmy Weitzhandler

      how do I convert from object to control (the same as CType in VB) private void button_click(object sender, EventArgs e) { MessageBox.Show(sender.Text); } I think I should convert the sender to a button. in VB i don't have to it, it converts it automatically...

      Shimi

      M Offline
      M Offline
      Michael Sync
      wrote on last edited by
      #2

      What about like that? (Button)sender.Text

      Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)

      V A 2 Replies Last reply
      0
      • M Michael Sync

        What about like that? (Button)sender.Text

        Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)

        V Offline
        V Offline
        Vasudevan Deepak Kumar
        wrote on last edited by
        #3

        Michael Sync wrote:

        (Button)sender.Text

        True. But I would say a line of check by having a call to .GetType() to prevent falling prey to System.InvalidCastException.

        Vasudevan Deepak Kumar Personal Homepage
        Tech Gossips
        A pessimist sees only the dark side of the clouds, and mopes; a philosopher sees both sides, and shrugs; an optimist doesn't see the clouds at all - he's walking on them. --Leonard Louis Levinson

        M 1 Reply Last reply
        0
        • V Vasudevan Deepak Kumar

          Michael Sync wrote:

          (Button)sender.Text

          True. But I would say a line of check by having a call to .GetType() to prevent falling prey to System.InvalidCastException.

          Vasudevan Deepak Kumar Personal Homepage
          Tech Gossips
          A pessimist sees only the dark side of the clouds, and mopes; a philosopher sees both sides, and shrugs; an optimist doesn't see the clouds at all - he's walking on them. --Leonard Louis Levinson

          M Offline
          M Offline
          Michael Sync
          wrote on last edited by
          #4

          Yeah.. You are right. Thanks.

          Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)

          1 Reply Last reply
          0
          • S Shimmy Weitzhandler

            how do I convert from object to control (the same as CType in VB) private void button_click(object sender, EventArgs e) { MessageBox.Show(sender.Text); } I think I should convert the sender to a button. in VB i don't have to it, it converts it automatically...

            Shimi

            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #5

            VB does this because it's rubbish. C# doesn't do this because it's strongly typed, one of the ways that it's NOT rubbish. The correct way to do this is to use the 'as' keyword to see if the object is indeed a control, but if you're sure it is, just cast it MessageBox.Show(((Control)sender).Text); that's what CType does.

            Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

            M 1 Reply Last reply
            0
            • M Michael Sync

              What about like that? (Button)sender.Text

              Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)

              A Offline
              A Offline
              Alaric_
              wrote on last edited by
              #6

              ummm....

              Button button;
              if(sender is Button)
              {
              button = (Button)sender;
              }

              ...granted, "Button" is actually System.Windows.Controls.Button or System.Web.UI.WebControls.Button.....or whatever button is contained in the namespace that you are using.

              "I need build Skynet. Plz send code"

              1 Reply Last reply
              0
              • C Christian Graus

                VB does this because it's rubbish. C# doesn't do this because it's strongly typed, one of the ways that it's NOT rubbish. The correct way to do this is to use the 'as' keyword to see if the object is indeed a control, but if you're sure it is, just cast it MessageBox.Show(((Control)sender).Text); that's what CType does.

                Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

                M Offline
                M Offline
                Michael Sync
                wrote on last edited by
                #7

                I don't think we should insult to VB developer..

                Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)

                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