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. Button.Text has no effect?

Button.Text has no effect?

Scheduled Pinned Locked Moved C#
helpquestion
9 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    Hi I'm struggling with a strange problem here, I can't set the text of a button in some circumstances. Here's my code:

    private void btnGo_Click(object sender, EventArgs e)
    {
    try
    {
    btnGo.Text = "Processing..."; // this one doesn't work

      // blocking operation that might fail
    

    }
    catch (...)
    finally
    {
    btnGo.Text = "Start"; // this one works
    }
    }

    If I remove btnGo.Text = "Start", then the text "Processing" shows up once the long operation has finished. /edit: I solved it by calling btnGo.Refresh() after btnGo.Text. But why is this necessary, will the button repaint itself only after another Paint-Event occured? Does anyone know what might be causing this strange behaviour? regards

    S D M S 4 Replies Last reply
    0
    • L Lost User

      Hi I'm struggling with a strange problem here, I can't set the text of a button in some circumstances. Here's my code:

      private void btnGo_Click(object sender, EventArgs e)
      {
      try
      {
      btnGo.Text = "Processing..."; // this one doesn't work

        // blocking operation that might fail
      

      }
      catch (...)
      finally
      {
      btnGo.Text = "Start"; // this one works
      }
      }

      If I remove btnGo.Text = "Start", then the text "Processing" shows up once the long operation has finished. /edit: I solved it by calling btnGo.Refresh() after btnGo.Text. But why is this necessary, will the button repaint itself only after another Paint-Event occured? Does anyone know what might be causing this strange behaviour? regards

      S Offline
      S Offline
      Sandeep Akhare
      wrote on last edited by
      #2

      You need to use Asynchronous process using threads

      Thanks and Regards Sandeep If you want something you never had, do something you have never done!

      1 Reply Last reply
      0
      • L Lost User

        Hi I'm struggling with a strange problem here, I can't set the text of a button in some circumstances. Here's my code:

        private void btnGo_Click(object sender, EventArgs e)
        {
        try
        {
        btnGo.Text = "Processing..."; // this one doesn't work

          // blocking operation that might fail
        

        }
        catch (...)
        finally
        {
        btnGo.Text = "Start"; // this one works
        }
        }

        If I remove btnGo.Text = "Start", then the text "Processing" shows up once the long operation has finished. /edit: I solved it by calling btnGo.Refresh() after btnGo.Text. But why is this necessary, will the button repaint itself only after another Paint-Event occured? Does anyone know what might be causing this strange behaviour? regards

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        Immediately after settings the button text, just call Application.DoEvents(), so your application gets and processes the message to repaint itself.

        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
             2006, 2007

        S 1 Reply Last reply
        0
        • D Dave Kreskowiak

          Immediately after settings the button text, just call Application.DoEvents(), so your application gets and processes the message to repaint itself.

          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007

          S Offline
          S Offline
          Sandeep Akhare
          wrote on last edited by
          #4

          There are many ways to do it if any one want to make user wait untill the process completes he can use asyncronous process as back ground thread . There are n number of articles available on CP . i know it is very tidious but more effective than Application.DoEvents()

          Thanks and Regards Sandeep If you want something you never had, do something you have never done!

          D 1 Reply Last reply
          0
          • S Sandeep Akhare

            There are many ways to do it if any one want to make user wait untill the process completes he can use asyncronous process as back ground thread . There are n number of articles available on CP . i know it is very tidious but more effective than Application.DoEvents()

            Thanks and Regards Sandeep If you want something you never had, do something you have never done!

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            There are also some people who are not ready to start throwing threads around. There are also situations where threading a process isn't acceptable, or would be too much work to keep the user from waiting for 1 or 2 seconds on a simpler process. He really didn't give any kind of information about what this process was doing. So, to keep his code simple, DoEvents is a very easy fix to the immediate problem. If he wanted to move this process to a background thread, he can ask about that all he wants and we'll help with it.

            Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                 2006, 2007

            L 1 Reply Last reply
            0
            • D Dave Kreskowiak

              There are also some people who are not ready to start throwing threads around. There are also situations where threading a process isn't acceptable, or would be too much work to keep the user from waiting for 1 or 2 seconds on a simpler process. He really didn't give any kind of information about what this process was doing. So, to keep his code simple, DoEvents is a very easy fix to the immediate problem. If he wanted to move this process to a background thread, he can ask about that all he wants and we'll help with it.

              Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                   2006, 2007

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              I'm absolutely familiar with Threads and right after I posted this question I found the solution myself :) I may switch my app to use threads in the future, but it's just a simple app for myself which querys a small database and displays some data, so button.Refresh() will do the job here. regards

              D 1 Reply Last reply
              0
              • L Lost User

                Hi I'm struggling with a strange problem here, I can't set the text of a button in some circumstances. Here's my code:

                private void btnGo_Click(object sender, EventArgs e)
                {
                try
                {
                btnGo.Text = "Processing..."; // this one doesn't work

                  // blocking operation that might fail
                

                }
                catch (...)
                finally
                {
                btnGo.Text = "Start"; // this one works
                }
                }

                If I remove btnGo.Text = "Start", then the text "Processing" shows up once the long operation has finished. /edit: I solved it by calling btnGo.Refresh() after btnGo.Text. But why is this necessary, will the button repaint itself only after another Paint-Event occured? Does anyone know what might be causing this strange behaviour? regards

                M Offline
                M Offline
                MoustafaS
                wrote on last edited by
                #7

                May be the process is too fast, so the processing happens in less than second, and the finally line anyway will make it = "Start".


                About : Islam
                About : Me

                1 Reply Last reply
                0
                • L Lost User

                  I'm absolutely familiar with Threads and right after I posted this question I found the solution myself :) I may switch my app to use threads in the future, but it's just a simple app for myself which querys a small database and displays some data, so button.Refresh() will do the job here. regards

                  D Offline
                  D Offline
                  Dave Kreskowiak
                  wrote on last edited by
                  #8

                  Greeeg wrote:

                  I'm absolutely familiar with Threads and right after I posted this question I found the solution myself

                  Great!

                  Greeeg wrote:

                  it's just a simple app for myself which querys a small database and displays some data,

                  Hence, the "simple" suggestion. :laugh:

                  Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                       2006, 2007

                  1 Reply Last reply
                  0
                  • L Lost User

                    Hi I'm struggling with a strange problem here, I can't set the text of a button in some circumstances. Here's my code:

                    private void btnGo_Click(object sender, EventArgs e)
                    {
                    try
                    {
                    btnGo.Text = "Processing..."; // this one doesn't work

                      // blocking operation that might fail
                    

                    }
                    catch (...)
                    finally
                    {
                    btnGo.Text = "Start"; // this one works
                    }
                    }

                    If I remove btnGo.Text = "Start", then the text "Processing" shows up once the long operation has finished. /edit: I solved it by calling btnGo.Refresh() after btnGo.Text. But why is this necessary, will the button repaint itself only after another Paint-Event occured? Does anyone know what might be causing this strange behaviour? regards

                    S Offline
                    S Offline
                    Stefan Prodan
                    wrote on last edited by
                    #9

                    This is not a strange behavior, your should start a new thread to do the time consuming work and on the finish event of the tread set the btnGo.Text = "Start".

                    http://stefanprodan.wordpress.com

                    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