C# 在消息框对象中显示加载点
我的windows窗体应用程序中有一个永久消息框。不要与弹出消息框混淆。我使用的框显示基于应用程序中按下的各种按钮的文本,以更新用户正在发生的事情。我成功地使用下面的代码在框中显示文本C# 在消息框对象中显示加载点,c#,winforms,C#,Winforms,我的windows窗体应用程序中有一个永久消息框。不要与弹出消息框混淆。我使用的框显示基于应用程序中按下的各种按钮的文本,以更新用户正在发生的事情。我成功地使用下面的代码在框中显示文本 messageBox.Text += "I stick message in here" + Environment.NewLine; 我感兴趣的是在方框中显示一些闪烁的点,以表示有什么事情即将发生。找东西 我已经找到了这个控制台的代码。 如何在我的消息框中实现此功能 for (int dots = 0; do
messageBox.Text += "I stick message in here" + Environment.NewLine;
我感兴趣的是在方框中显示一些闪烁的点,以表示有什么事情即将发生。找东西
我已经找到了这个控制台的代码。
如何在我的消息框中实现此功能
for (int dots = 0; dots <= 3; ++dots)
{
Console.Write("\rStuff will come{0}", new string('.', dots));
System.Threading.Thread.Sleep(500); // half a sec
}
我用一个简单的方法解决了一个类似的问题。一个任务是做后台工作,另一个任务是更新消息文本。以下是一些示例代码:
private void button1_Click(object sender, EventArgs e)
{
// call the helper to do something
Task.Factory.StartNew(() => { FakeSearch(); });
//Generate the update the waiting text
Task.Factory.StartNew(() => { updateWaiting(); });
}
private void FakeSearch()
{
_externalFlag = false;
Thread.Sleep(5000);
_externalFlag = true; // simulate completing the task
}
private bool _externalFlag = false;
private void updateWaiting()
{
int count = 0;
StringBuilder waitingText = new StringBuilder();
waitingText.Append("Finding stuff");
int baseLen = waitingText.Length;
while (!_externalFlag)
{
Thread.Sleep(1000); // time between adding dots
if (count >= 3) // number of dots
{
waitingText.Remove(baseLen, count);
count = 0;
}
waitingText.Append(".");
count++;
BeginInvoke(new Action( () => { updateText(waitingText.ToString()); }) );
}
BeginInvoke(new Action( () => { updateText("done"); }) );
}
private void updateText(string txt)
{
textBox1.Text = txt;
}
为什么不创建一个可以在整个应用程序中使用的字符串变量,例如公共静态字符串dots=newstring'.'3;然后write Console.Writestring.Format\r将出现{0},点;您可能有兴趣更改此行控制台。Write\rStuff将出现{0},新字符串“.”,点;到Console.WriteStuff将出现{0}\r,新字符串“.”,点;因为您处于循环中,所以实际上不需要此行System.Threading.Thread.Sleep500;是啊,我想得太多了。我更改了控制台。Write\rStuff将出现{0},新字符串“.”,点;到messageBox.Text+=string.Format。;一切都很好。你把我引向了正确的方向DJ KRAZE