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. Can't change a button't text at runtime anymore??????

Can't change a button't text at runtime anymore??????

Scheduled Pinned Locked Moved C#
questioncsharpvisual-studio
19 Posts 8 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.
  • OriginalGriffO OriginalGriff

    The other thing to check is that the click event is hooked to the event handler: put a breakpoint on the first line to make sure it is being executed, and then single step through.

    Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

    F Offline
    F Offline
    fiaolle
    wrote on last edited by
    #7

    Hi Thanks for your answers. I have already stepped through the code and it's running as it should, but the button's Text still doesn't change. I don't understand what to do????. Many Thanks Fia

    OriginalGriffO 1 Reply Last reply
    0
    • F fiaolle

      Hi Thanks for your answers. I have already stepped through the code and it's running as it should, but the button's Text still doesn't change. I don't understand what to do????. Many Thanks Fia

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #8

      OK: I took an existing form, and dropped a button on it. I double clicked the button and in the handler method that came up:

          private void button1\_Click(object sender, EventArgs e)
              {
              button1.Text = button1.Text == "Hello" ? "Goodbye" : "Hello";
              }
      

      When I ran the app, the button caption was "button1". When I pressed it the first time, it changed to "Goodbye" A second click, it became "Hello". Third click, back to "Goodbye", and round and round. What am I doing that is different to you, or you doing that is different to me?

      Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      F 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        OK: I took an existing form, and dropped a button on it. I double clicked the button and in the handler method that came up:

            private void button1\_Click(object sender, EventArgs e)
                {
                button1.Text = button1.Text == "Hello" ? "Goodbye" : "Hello";
                }
        

        When I ran the app, the button caption was "button1". When I pressed it the first time, it changed to "Goodbye" A second click, it became "Hello". Third click, back to "Goodbye", and round and round. What am I doing that is different to you, or you doing that is different to me?

        Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

        F Offline
        F Offline
        fiaolle
        wrote on last edited by
        #9

        Hi I can't see any difference. Here is my code.

        private void btn_Click(object sender, EventArgs e)
        {
        if (folder.Visible==true )
        {
        btn.Text = "Hi";
        }
        else
        {
        btn.Text = "By";
        }
        }

        The variable folder is just a bool variable. When I debug my code I can see that it runs as it should, but still the button's Text doesn't change. I don't know if it has anything to do with the problem, but I have a FileLListbox and a FolderListBox from Alva on the form. Should that be the problem. Many Thanks Fia

        OriginalGriffO 1 Reply Last reply
        0
        • F fiaolle

          Hi I can't see any difference. Here is my code.

          private void btn_Click(object sender, EventArgs e)
          {
          if (folder.Visible==true )
          {
          btn.Text = "Hi";
          }
          else
          {
          btn.Text = "By";
          }
          }

          The variable folder is just a bool variable. When I debug my code I can see that it runs as it should, but still the button's Text doesn't change. I don't know if it has anything to do with the problem, but I have a FileLListbox and a FolderListBox from Alva on the form. Should that be the problem. Many Thanks Fia

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #10

          Try something quick: Put a breakpoint on the if line of the handler, and change teh button text in the designer to "TEST". Run your app, and press the button. Look at btn.Text in the debugger - is it "TEST"?

          Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          F 1 Reply Last reply
          0
          • OriginalGriffO OriginalGriff

            Try something quick: Put a breakpoint on the if line of the handler, and change teh button text in the designer to "TEST". Run your app, and press the button. Look at btn.Text in the debugger - is it "TEST"?

            Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

            F Offline
            F Offline
            fiaolle
            wrote on last edited by
            #11

            Hi I tried that and the text is "Test". I tried also to remove all Alva controls, but I still can't change the text on the button. :( Thanks Fia

            OriginalGriffO 1 Reply Last reply
            0
            • F fiaolle

              Hi I tried that and the text is "Test". I tried also to remove all Alva controls, but I still can't change the text on the button. :( Thanks Fia

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #12

              So at least we can be absolutely sure that the right button is hooked up - if you replace the handler content with

              btn.Text = "WTF!";

              and single step over that one instruction, is the text still "Test" or "WTF!" - I'm just wondering if the text is being changed back somewhere else (which would be a weird thing to do automatically)

              Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              F 1 Reply Last reply
              0
              • OriginalGriffO OriginalGriff

                So at least we can be absolutely sure that the right button is hooked up - if you replace the handler content with

                btn.Text = "WTF!";

                and single step over that one instruction, is the text still "Test" or "WTF!" - I'm just wondering if the text is being changed back somewhere else (which would be a weird thing to do automatically)

                Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

                F Offline
                F Offline
                fiaolle
                wrote on last edited by
                #13

                Hi It changes to "WTF" in the code, but after the event. The buttons text is "Test"????? Thanks Fia

                OriginalGriffO P 2 Replies Last reply
                0
                • F fiaolle

                  Hi It changes to "WTF" in the code, but after the event. The buttons text is "Test"????? Thanks Fia

                  OriginalGriffO Offline
                  OriginalGriffO Offline
                  OriginalGriff
                  wrote on last edited by
                  #14

                  :wtf: Something has got to be changing it back! Delete it. Rebuild. That should give you any errors that refer to it. If there aren't any, put it back and repeat the above, see if it works. Either that or you have a PC full of malevolent pixies... :laugh:

                  Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                  "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                  1 Reply Last reply
                  0
                  • F fiaolle

                    Hi When I could change the text at runtime, I think the property was called Capture. How do you do in Visual Studio 2010? If I set the button btn's property Text to "By" and try the below

                    btn.Text="Hi"

                    nothing happens. How do I do it nowadays :confused: Many Thanks Fia

                    O Offline
                    O Offline
                    OOPSEP
                    wrote on last edited by
                    #15

                    private void btn_Click(object sender, EventArgs e)
                    {
                    if (folder.Visible==true )
                    {
                    btn.Text = "Hi";
                    }
                    else
                    {
                    btn.Text = "By";
                    }

                    }

                    Make sure your property folder.Visible is change when click state btn.Text = folder.Visible?"Hi":By; folder.Visible=!folder.Visible; // reset state here

                    TU

                    F 1 Reply Last reply
                    0
                    • O OOPSEP

                      private void btn_Click(object sender, EventArgs e)
                      {
                      if (folder.Visible==true )
                      {
                      btn.Text = "Hi";
                      }
                      else
                      {
                      btn.Text = "By";
                      }

                      }

                      Make sure your property folder.Visible is change when click state btn.Text = folder.Visible?"Hi":By; folder.Visible=!folder.Visible; // reset state here

                      TU

                      F Offline
                      F Offline
                      fiaolle
                      wrote on last edited by
                      #16

                      Hi I am changing the folder to visible or not visible. But nothing is helping me. Thanks Fia

                      1 Reply Last reply
                      0
                      • F fiaolle

                        Hi When I could change the text at runtime, I think the property was called Capture. How do you do in Visual Studio 2010? If I set the button btn's property Text to "By" and try the below

                        btn.Text="Hi"

                        nothing happens. How do I do it nowadays :confused: Many Thanks Fia

                        B Offline
                        B Offline
                        BillWoodruff
                        wrote on last edited by
                        #17

                        Given all the responses above, clearly something very weird is happening here. Are you talking about a standard WinForms Button object here ? Also: what is "folder," exactly: that's an odd name for a Control. And, if "folder" has a boolean "visible" property, you can use it directly : if (folder.Visible) ... You mention: "I don't know if it has anything to do with the problem, but I have a FileLListbox and a FolderListBox from Alva on the form. Should that be the problem." Is "folder" by chance an "Alva" control ? What are the "Alva" controls, and, is it possible your use of them has introduced some kind of modification that is indirectly changing the way other controls behave ? good luck, Bill

                        "Science is facts; just as houses are made of stones: so, is science made of facts. But, a pile of stones is not a house, and a collection of facts is not, necessarily, science." Henri Poincare

                        F 1 Reply Last reply
                        0
                        • F fiaolle

                          Hi It changes to "WTF" in the code, but after the event. The buttons text is "Test"????? Thanks Fia

                          P Offline
                          P Offline
                          Pete OHanlon
                          wrote on last edited by
                          #18

                          Put a breakpoint in your code in this routine. Search through your code for all instances of the word Test and put breakpoints on them all - each and everyone. Run the program again and note each breakpoint you hit - you must be resetting the text back to Test after you have left this routine.

                          Forgive your enemies - it messes with their heads

                          "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                          My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                          1 Reply Last reply
                          0
                          • B BillWoodruff

                            Given all the responses above, clearly something very weird is happening here. Are you talking about a standard WinForms Button object here ? Also: what is "folder," exactly: that's an odd name for a Control. And, if "folder" has a boolean "visible" property, you can use it directly : if (folder.Visible) ... You mention: "I don't know if it has anything to do with the problem, but I have a FileLListbox and a FolderListBox from Alva on the form. Should that be the problem." Is "folder" by chance an "Alva" control ? What are the "Alva" controls, and, is it possible your use of them has introduced some kind of modification that is indirectly changing the way other controls behave ? good luck, Bill

                            "Science is facts; just as houses are made of stones: so, is science made of facts. But, a pile of stones is not a house, and a collection of facts is not, necessarily, science." Henri Poincare

                            F Offline
                            F Offline
                            fiaolle
                            wrote on last edited by
                            #19

                            Hi Thanks for all your replies. I have found my error. I used InitializeComponent(); both in the constructor Form1() and in Form1_Load(object sender, EventArgs e) I am so sorry for my misstake, but know it's working as it should :-D Many thanks Fia

                            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