how to continuously write text to a label in C#
-
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++; } } }
}
-
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++; } } }
}
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.
-
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.
thanks for the idea on the timer, I tried it with the correct time interval but it still did not work. Not sure why.
-
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++; } } }
}
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++;
} -
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++;
}thanks, but VS2008 do not support Append
-
thanks, but VS2008 do not support Append
-
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++;
}nope did not work.
-
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++; } } }
}
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++; }
-
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++; }
-
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++; } } }
}
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.
-
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.
thanks, I'm using the text boxes instead of labels.