C#多行的textbox如何逐行显示数据?
- C# code
public partial class Form1 : Form { string[] arrName = new string[7]{"aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg"}; public Form1() { InitializeComponent(); textBox1.Text = "";//多行 } private void button1_Click(object sender, EventArgs e) { //每隔一秒textBox1增加一行(各行为数组的各元素) //textBox1上当前行显示最后追加的一行。 } private void button2_Click(object sender, EventArgs e) { //停止增加 } }
大家帮忙看看:两个button里面应该怎么写?
[解决办法]
你定义一个 Timer Timer 的事件中写 增加一行
button1 触发Timer
button2 停止Timer
[解决办法]
public partial class Form1 : Form
{
Timer timer1 = new Timer();
string[] arrName = new string[7] { "aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg" };
int stepFlag = 0;
public Form1()
{
InitializeComponent();
textBox1.Text = "";//多行
timer1.Interval = 1000;
timer1.Tick += new System.EventHandler(this.timer1_Tick);
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Start();
}
private void button2_Click(object sender, EventArgs e)
{
timer1.Stop();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (stepFlag < arrName.Length)
{
textBox1.AppendText(arrName[stepFlag]);
textBox1.AppendText("\r\n");
stepFlag++;
}
else
{
timer1.Stop();
}
}
}
[解决办法]
增加一个全局变量
int NowLine = 0;
Timer 事件内
textBox1.text = textBox1.text + arrName[NowLine] + "\n";
NowLine++;
[解决办法]
计时器!
[解决办法]
- C# code
Timer timer = new Timer(); timer1.Interval = 1000; private void button1_Click(object sender, EventArgs e) { //每隔一秒textBox1增加一行(各行为数组的各元素) timer.Start(); //textBox1上当前行显示最后追加的一行。 textBox1.AppendText(".........") textBox1.AppendText("\r\n"); } private void button2_Click(object sender, EventArgs e) { //停止增加 timer1.Stop(); }
[解决办法]
2,5楼代码不错
学习了!
[解决办法]
使用TextBoxBase.ScrollToCaret 方法 可以自动的定位于当前的光标点。
当添加一行的时候光标认在最后。如果不确定的话,可以使用TextBoxBase.SelectionStart 属性设置到TextBoxBase.SelectionLength 属性的位置。然后再调用ScrollToCaret 方法。
[解决办法]
用Timer 控件 或 线程
[解决办法]
用timer事件