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. how to continuously write text to a label in C#

how to continuously write text to a label in C#

Scheduled Pinned Locked Moved C#
csharphelptutorial
11 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.
  • U Offline
    U Offline
    User 10650102
    wrote on last edited by
    #1

    hello; I have just started using VS2008 C# for windows mobile. I cannot make my label.text continuously display data, I verify the information is being passed but the form is not displaying the information. I tried this simple piece of code and it only displays the last value. please help.

    namespace datatran
    {
    public partial class frmSend : Form
    {
    int i = 0;
    int value;
    public frmSend()
    {
    InitializeComponent();
    }

        private void btnSend\_Click(object sender, EventArgs e)
        {
    
            value = Convert.ToInt16(txtBoxSend.Text);
            while (i < value)
            {
    
    
    
    
                lblCount.Text = i.ToString();
                lblCount.Show();
    
    
                i++;
            }
    
    
        }
    
    
    }
    

    }

    L H U U 4 Replies Last reply
    0
    • U User 10650102

      hello; I have just started using VS2008 C# for windows mobile. I cannot make my label.text continuously display data, I verify the information is being passed but the form is not displaying the information. I tried this simple piece of code and it only displays the last value. please help.

      namespace datatran
      {
      public partial class frmSend : Form
      {
      int i = 0;
      int value;
      public frmSend()
      {
      InitializeComponent();
      }

          private void btnSend\_Click(object sender, EventArgs e)
          {
      
              value = Convert.ToInt16(txtBoxSend.Text);
              while (i < value)
              {
      
      
      
      
                  lblCount.Text = i.ToString();
                  lblCount.Show();
      
      
                  i++;
              }
      
      
          }
      
      
      }
      

      }

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

      The reason why it's not working as you expect is that the thread executing your event handler method is also responsible for updating the screen - which it can't while it's executing the event handler method. Once it's done executing the event handler it updates the screen - once, and obviously with the last value that was assigned to the label text. One solution for this would be to employ a Timer (MSDN-Doc[^] / Codeproject Tutorial[^]) which provides you a simple way of changing the label text and updating the screen simultaneously. I guess this was a desperate measure:

      lblCount.Show();

      - you don't need to do that each time you update the label text.

      U 1 Reply Last reply
      0
      • L Lost User

        The reason why it's not working as you expect is that the thread executing your event handler method is also responsible for updating the screen - which it can't while it's executing the event handler method. Once it's done executing the event handler it updates the screen - once, and obviously with the last value that was assigned to the label text. One solution for this would be to employ a Timer (MSDN-Doc[^] / Codeproject Tutorial[^]) which provides you a simple way of changing the label text and updating the screen simultaneously. I guess this was a desperate measure:

        lblCount.Show();

        - you don't need to do that each time you update the label text.

        U Offline
        U Offline
        User 10650102
        wrote on last edited by
        #3

        thanks for the idea on the timer, I tried it with the correct time interval but it still did not work. Not sure why.

        1 Reply Last reply
        0
        • U User 10650102

          hello; I have just started using VS2008 C# for windows mobile. I cannot make my label.text continuously display data, I verify the information is being passed but the form is not displaying the information. I tried this simple piece of code and it only displays the last value. please help.

          namespace datatran
          {
          public partial class frmSend : Form
          {
          int i = 0;
          int value;
          public frmSend()
          {
          InitializeComponent();
          }

              private void btnSend\_Click(object sender, EventArgs e)
              {
          
                  value = Convert.ToInt16(txtBoxSend.Text);
                  while (i < value)
                  {
          
          
          
          
                      lblCount.Text = i.ToString();
                      lblCount.Show();
          
          
                      i++;
                  }
          
          
              }
          
          
          }
          

          }

          H Offline
          H Offline
          HKHerron
          wrote on last edited by
          #4

          The following code only updates the text with the current value of i through each step of the While Loop. So once the While loop is done, the text will of course be only the LAST value.

          while (i < value)
          {
          lblCount.Text = i.ToString();
          lblCount.Show();
          i++;
          }

          If you are wanting the textbox to show each step through the While Loop, then try this:

          lblCount.Text.Clear();
          while (i < value)
          {
          lblCount.AppendText = i.ToString() + "\r\n";
          i++;
          }

          U 1 Reply Last reply
          0
          • H HKHerron

            The following code only updates the text with the current value of i through each step of the While Loop. So once the While loop is done, the text will of course be only the LAST value.

            while (i < value)
            {
            lblCount.Text = i.ToString();
            lblCount.Show();
            i++;
            }

            If you are wanting the textbox to show each step through the While Loop, then try this:

            lblCount.Text.Clear();
            while (i < value)
            {
            lblCount.AppendText = i.ToString() + "\r\n";
            i++;
            }

            U Offline
            U Offline
            User 10650102
            wrote on last edited by
            #5

            thanks, but VS2008 do not support Append

            H 1 Reply Last reply
            0
            • U User 10650102

              thanks, but VS2008 do not support Append

              H Offline
              H Offline
              HKHerron
              wrote on last edited by
              #6

              I'm sorry, I just realized it is a label, not a textbox. Not sure but this may work:

              while (i < value)
              {
              lblCount.Text = lblCount.Text + i.ToString() + "<br/>";
              i++;
              }

              U 1 Reply Last reply
              0
              • H HKHerron

                I'm sorry, I just realized it is a label, not a textbox. Not sure but this may work:

                while (i < value)
                {
                lblCount.Text = lblCount.Text + i.ToString() + "<br/>";
                i++;
                }

                U Offline
                U Offline
                User 10650102
                wrote on last edited by
                #7

                nope did not work.

                1 Reply Last reply
                0
                • U User 10650102

                  hello; I have just started using VS2008 C# for windows mobile. I cannot make my label.text continuously display data, I verify the information is being passed but the form is not displaying the information. I tried this simple piece of code and it only displays the last value. please help.

                  namespace datatran
                  {
                  public partial class frmSend : Form
                  {
                  int i = 0;
                  int value;
                  public frmSend()
                  {
                  InitializeComponent();
                  }

                      private void btnSend\_Click(object sender, EventArgs e)
                      {
                  
                          value = Convert.ToInt16(txtBoxSend.Text);
                          while (i < value)
                          {
                  
                  
                  
                  
                              lblCount.Text = i.ToString();
                              lblCount.Show();
                  
                  
                              i++;
                          }
                  
                  
                      }
                  
                  
                  }
                  

                  }

                  U Offline
                  U Offline
                  User 10650102
                  wrote on last edited by
                  #8

                  Thanks all for the help, The answer to my issue is to use a text box instead. This works for me. while (i < value) { textBox1.Text = i.ToString() + "\r\n"; i++; }

                  V 1 Reply Last reply
                  0
                  • U User 10650102

                    Thanks all for the help, The answer to my issue is to use a text box instead. This works for me. while (i < value) { textBox1.Text = i.ToString() + "\r\n"; i++; }

                    V Offline
                    V Offline
                    V 0
                    wrote on last edited by
                    #9

                    that's a workaround. not a solution. Not that, that's a bad thing, but it is important to note the difference. ;)

                    V.
                    (MQOTD rules and previous solutions)

                    1 Reply Last reply
                    0
                    • U User 10650102

                      hello; I have just started using VS2008 C# for windows mobile. I cannot make my label.text continuously display data, I verify the information is being passed but the form is not displaying the information. I tried this simple piece of code and it only displays the last value. please help.

                      namespace datatran
                      {
                      public partial class frmSend : Form
                      {
                      int i = 0;
                      int value;
                      public frmSend()
                      {
                      InitializeComponent();
                      }

                          private void btnSend\_Click(object sender, EventArgs e)
                          {
                      
                              value = Convert.ToInt16(txtBoxSend.Text);
                              while (i < value)
                              {
                      
                      
                      
                      
                                  lblCount.Text = i.ToString();
                                  lblCount.Show();
                      
                      
                                  i++;
                              }
                      
                      
                          }
                      
                      
                      }
                      

                      }

                      U Offline
                      U Offline
                      User 8540299
                      wrote on last edited by
                      #10

                      It seems like it is looping through all the values and making the label display all of them. It is probably doing this so fast that you only see the last set value. If you want to see all the values, maybe try lblCount.Text += i.ToString() + " " to display all the values side by side. If you do this, remember to clear out the label before looping.

                      U 1 Reply Last reply
                      0
                      • U User 8540299

                        It seems like it is looping through all the values and making the label display all of them. It is probably doing this so fast that you only see the last set value. If you want to see all the values, maybe try lblCount.Text += i.ToString() + " " to display all the values side by side. If you do this, remember to clear out the label before looping.

                        U Offline
                        U Offline
                        User 10650102
                        wrote on last edited by
                        #11

                        thanks, I'm using the text boxes instead of labels.

                        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