C#中的动态TextBoc [英] Dynamic TextBoc in C#
本文介绍了C#中的动态TextBoc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有简单的formApplication脚本,其中包含按钮和一些TextBox。
我想点击按钮时,一个文本框显示一些数字。
如何让这种动态。
private void button1_Click( object sender,EventArgs e)
{
txt3.Text = ;
for ( int i = 0 ; i< 50; i ++)
{
Random random = new Random();
int randomNumber = random.Next( 100 , 150 跨度>);
txt3.Text = randomNumber.ToString();
}
}
现在它等待循环结束并显示最新数字。
我希望它显示TextBox中的所有数字。
问候,
解决方案
尝试:
txt3.Text + = randomNumber.ToString();
你可能想要添加一些间距或换行符(如果这是一个多行文本框)
好的,试试这个:
私人 委托 < span class =code-keyword> void DisplayTextBoxDelegate( int i)
private void button1_Click( object sender,EventArgs e)
{
txt3.Text = ;
var thdDoTextBoxStuff = new 线程(DoTextBoxStuff);
DoTextBoxStuff.IsBackground = true ;
DoTextBoxStuff.Start();
}
private void DoTextBoxStuff()
{
for ( int i = 0 ; i< 50; i ++)
{
Random random = new Random();
int randomNumber = random.Next( 100 , 150 跨度>);
txt3.Invoke( new DisplayTextboxDelegate(UpdateTextBoxValue),randomNumber);
Thread.Sleep( 500 );
}
}
私有 void UpdateTextboxValue( int iValue)
{
txt3.Text = iValue.ToString();
}
再次,我没有尝试过上述内容 - 但它应该让你朝着正确的方向前进。 / blockquote>
需要添加:
Application.DoEvents();
接下来是有效的。
Hi,
I have simple formApplication script that contains button and some TextBoxs.
I want when click on button, one textbox shows some numbers.
How I can make that dynamic.
private void button1_Click(object sender, EventArgs e) { txt3.Text = ""; for (int i = 0; i <50; i++) { Random random = new Random(); int randomNumber = random.Next(100, 150); txt3.Text = randomNumber.ToString(); } }
Now it waits to loop finished and shows latest number.
I want it shows all numbers in TextBox.
Regards,解决方案Try:
txt3.Text += randomNumber.ToString();You may want to add some spacing or newlines (if this is a multiline text box)
OK, try this:
private delegate void DisplayTextBoxDelegate(int i) private void button1_Click(object sender, EventArgs e) { txt3.Text = ""; var thdDoTextBoxStuff = new Thread(DoTextBoxStuff); DoTextBoxStuff.IsBackground = true; DoTextBoxStuff.Start(); } private void DoTextBoxStuff() { for (int i = 0; i <50; i++) { Random random = new Random(); int randomNumber = random.Next(100, 150); txt3.Invoke(new DisplayTextboxDelegate(UpdateTextBoxValue), randomNumber); Thread.Sleep(500); } } private void UpdateTextboxValue(int iValue) { txt3.Text = iValue.ToString(); }
Again, I've not tried the above - but it should get you moving in the right direction.
Need add this:
Application.DoEvents();
Next it works.
这篇关于C#中的动态TextBoc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文