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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. How do I cast a string to type System.Windows.Forms.MessageBoxButtons

How do I cast a string to type System.Windows.Forms.MessageBoxButtons

Scheduled Pinned Locked Moved C#
questioncsharphelp
5 Posts 3 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.
  • V Offline
    V Offline
    Vernware
    wrote on last edited by
    #1

    In C#: I am building the MessageBox parameter: MessageBoxButtons.OK from a string. The string is defined this way: string buttonSelected = “MessageBoxButtons.” + selectedButton; selectedButton is defined elsewhere in the program, lets say as “OK”. When I call MessageBox, I try to cast the parameter MessageBoxButtons like this: MessageBox.Show(“test”,”test”, (System.Windows.Forms.MessageBoxButtons) buttonSelected); I get an error that says I cannot convert type string to System.Windows.Forms.MessageBoxButtons. Why can’t I do this? Vern

    L C 2 Replies Last reply
    0
    • V Vernware

      In C#: I am building the MessageBox parameter: MessageBoxButtons.OK from a string. The string is defined this way: string buttonSelected = “MessageBoxButtons.” + selectedButton; selectedButton is defined elsewhere in the program, lets say as “OK”. When I call MessageBox, I try to cast the parameter MessageBoxButtons like this: MessageBox.Show(“test”,”test”, (System.Windows.Forms.MessageBoxButtons) buttonSelected); I get an error that says I cannot convert type string to System.Windows.Forms.MessageBoxButtons. Why can’t I do this? Vern

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      In general you can not cast a string to just any kind of object you are dreaming of. You can do the opposite, that is each kind of object has a ToString() method (inherited from Object) to give you a string representation (although the default implementation returns simply the type name, not always very useful; but you can and should override the ToString method). If you want to communicate about message buttons between different pieces of code, you should use a variable of type MessageBoxButtons. Example:

      MessageBoxButtons myButtons;
      bool withCancel=false;
      if(withCancel) myButtons=MessageBoxButtons.OKCancel;
      else myButtons=MessageBoxButtons.OK;
      ... whatever other things you want to do
      MessageBox.Show("text", "caption", myButtons);
      

      :)

      Luc Pattyn

      V 1 Reply Last reply
      0
      • V Vernware

        In C#: I am building the MessageBox parameter: MessageBoxButtons.OK from a string. The string is defined this way: string buttonSelected = “MessageBoxButtons.” + selectedButton; selectedButton is defined elsewhere in the program, lets say as “OK”. When I call MessageBox, I try to cast the parameter MessageBoxButtons like this: MessageBox.Show(“test”,”test”, (System.Windows.Forms.MessageBoxButtons) buttonSelected); I get an error that says I cannot convert type string to System.Windows.Forms.MessageBoxButtons. Why can’t I do this? Vern

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

        You can use enum.Parse to turn a string into an enum instance

        Christian Graus - C++ MVP 'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert

        V 1 Reply Last reply
        0
        • C Christian Graus

          You can use enum.Parse to turn a string into an enum instance

          Christian Graus - C++ MVP 'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert

          V Offline
          V Offline
          Vernware
          wrote on last edited by
          #4

          Christian: Thanks for you response. The enum.Parse was a good tip. Thanks again. Vern

          1 Reply Last reply
          0
          • L Luc Pattyn

            In general you can not cast a string to just any kind of object you are dreaming of. You can do the opposite, that is each kind of object has a ToString() method (inherited from Object) to give you a string representation (although the default implementation returns simply the type name, not always very useful; but you can and should override the ToString method). If you want to communicate about message buttons between different pieces of code, you should use a variable of type MessageBoxButtons. Example:

            MessageBoxButtons myButtons;
            bool withCancel=false;
            if(withCancel) myButtons=MessageBoxButtons.OKCancel;
            else myButtons=MessageBoxButtons.OK;
            ... whatever other things you want to do
            MessageBox.Show("text", "caption", myButtons);
            

            :)

            Luc Pattyn

            V Offline
            V Offline
            Vernware
            wrote on last edited by
            #5

            Luc: Thanks for the response. I did finally end up using some code that used MessageBoxButtons and MessageBoxIcon variables. Thanks again. Vern

            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