暂停富文本框,然后再次恢复
pause rich text box and then resume again
我在做什么:
我每秒不断从串口接收一串数据。我正在处理它,还在富文本框中显示这个字符串。
问题:
我希望用户浏览旧字符串并复制任何字符串,但用户不能这样做,因为数据每秒都在发生,并且会发生自动滚动。
我想要的解决方案:
我想有一个复选框 'pause'。当用户选中它时,富文本框的更新停止。用户可以进入历史并复制一个字符串。但同时我不想停止从串行端口传入的字符串,因为我正在对传入的字符串做其他事情。
因此,当用户取消选中 'pause' 复选框时,所有在用户选中“暂停”复选框时更早到达的字符串也会与新字符串一起出现在富文本框中。
有办法吗?
假设当您选中暂停按钮时,每个传入的文本都会附加到 StringBuilder 而不是 RichTextBox。当用户取消选中暂停按钮时,您将所有内容从 StringBuilder 复制到 RichTextBox
// Assume that these are somewhere globals of your forms
RichTextBox rtb = new RichTextBox();
CheckBox chkPause = new CheckBox();
StringBuilder sb = new StringBuilder();
protected void chkPause_CheckedChanged(object sender, EventArgs e)
{
if(!chkPause.Checked)
{
rtb.AppendText = sb.ToString();
// Do not forget to clear the buffer to avoid errors
// if the user repeats the stop/go cycle.
sb.Clear();
}
else
{
// Start a timer to resume normal flow after a timer elapses.
System.Windows.Forms.Timer t = new System.Windows.Forms.Timer();
t.Interval = GetSuspensionMilliseconds();
t.Tick += onTick;
t.Start();
}
}
protected void onTick(object sender, EventArgs e)
{
if (chkPause.Checked)
{
// Set to false when the timing elapses thus triggering the CheckedChanged event
chkPause.Checked = false;
System.Windows.Forms.Timer t = sender as System.Windows.Forms.Timer;
t.Stop();
}
}
现在,在将传入数据传递到 RichTextBox 的地方,您可以添加
....
string incomingData = ReceiveDataFromSerialPort();
if(chkPause.Checked)
sb.AppendLine(incomingData);
else
rtb.AppendText = incomingData;
我在做什么:
我每秒不断从串口接收一串数据。我正在处理它,还在富文本框中显示这个字符串。
问题:
我希望用户浏览旧字符串并复制任何字符串,但用户不能这样做,因为数据每秒都在发生,并且会发生自动滚动。
我想要的解决方案:
我想有一个复选框 'pause'。当用户选中它时,富文本框的更新停止。用户可以进入历史并复制一个字符串。但同时我不想停止从串行端口传入的字符串,因为我正在对传入的字符串做其他事情。
因此,当用户取消选中 'pause' 复选框时,所有在用户选中“暂停”复选框时更早到达的字符串也会与新字符串一起出现在富文本框中。
有办法吗?
假设当您选中暂停按钮时,每个传入的文本都会附加到 StringBuilder 而不是 RichTextBox。当用户取消选中暂停按钮时,您将所有内容从 StringBuilder 复制到 RichTextBox
// Assume that these are somewhere globals of your forms
RichTextBox rtb = new RichTextBox();
CheckBox chkPause = new CheckBox();
StringBuilder sb = new StringBuilder();
protected void chkPause_CheckedChanged(object sender, EventArgs e)
{
if(!chkPause.Checked)
{
rtb.AppendText = sb.ToString();
// Do not forget to clear the buffer to avoid errors
// if the user repeats the stop/go cycle.
sb.Clear();
}
else
{
// Start a timer to resume normal flow after a timer elapses.
System.Windows.Forms.Timer t = new System.Windows.Forms.Timer();
t.Interval = GetSuspensionMilliseconds();
t.Tick += onTick;
t.Start();
}
}
protected void onTick(object sender, EventArgs e)
{
if (chkPause.Checked)
{
// Set to false when the timing elapses thus triggering the CheckedChanged event
chkPause.Checked = false;
System.Windows.Forms.Timer t = sender as System.Windows.Forms.Timer;
t.Stop();
}
}
现在,在将传入数据传递到 RichTextBox 的地方,您可以添加
....
string incomingData = ReceiveDataFromSerialPort();
if(chkPause.Checked)
sb.AppendLine(incomingData);
else
rtb.AppendText = incomingData;