问题描述
所以问题是我开发了一个程序,将在一个文本框中输入的文本复制到另一个文本框(如聊天框)...........但问题是我想写一个如果程序将响应的条件,但问题是我已经创建了延迟,如使用system.threading.thread.sleep(1000)所以这里出现问题.......程序立即进入匹配没有执行任何其他事情的条件,所以文本也会在一秒后被复制.........响应也是如此.........但我想要的是先复制文本然后再回复.........提前致谢....
问候,
Ahsan Naveed。
so here is the problem i have developed a program that copies the text entered in one textbox to another one(like a chatbox)...........but the problem is that I want to write a group of if conditions on which the program will respond but the problem is that i have created delay like using system.threading.thread.sleep(1000)so here comes the problem.......the program imediately goes to the matching condiition without executing anything else before so the text also gets copied after one second.........and so does response.........but what I want is to copy the text first and response later.........Thanks in advance....
Regards,
Ahsan Naveed.
推荐答案
if (textBox1.Text != "")
{
textBox2.Text += textBox1.Text + "\r\n";
}
对用户来说,它与你的代码做同样的事情。
然后,让我们做一些非常愚蠢的事情......让我们抛弃你的代码.. 。:笑:
相反,让我们创建几个类级变量:
Visually to the user it does the same thing as your code.
Then, let's do something really silly...let's throw your code away...:laugh:
Instead, let's create a couple of class level variables:
private int waitTimer = 0;
private string waitMessage = "";
然后让我们整理你的其他代码并改用它们。
and then let's tidy up your other code and use these instead.
if (textBox1.Text.ToLower() == "what are you?")
{
waitMessage = "I am a computerized robot and my name is Dr. A";
waitTimer = 4;
}
textBox1.Clear();
这一点非常明显,除了第一部分 - 它将文本框中的所有大写字符转换为小写然后检查他们。这意味着您不必键入字符串加载次数,偶尔会出错,如果用户生气,用户可以用大写字母输入!
但这对显示没有任何作用 - 所以请使用我之前给你的Timer代码,并把它放在Tick事件处理程序中:
What this does is pretty obvious, except for the first part - which converts all the uppercase characters in the textbox to lower case and then checks them. This means that you don't have to type the string loads of times, and get it wrong occasionally, and the user can type it all in uppercase if he is feeling angry!
But that does nothing to the display - so use the Timer code I gave you before, and put this in the Tick event handler:
if (waitTimer > 0)
{
if (--waitTimer == 0)
{
textBox2.Text += waitMessage + "\r\n";
}
}
waitTimer中的值4应该与我放入的Interval属性中的250匹配定时器,给大约一秒钟。较大的数字会使延迟更长,较小的数字会使延迟更短。
现在尝试一下!
The value "4" in the waitTimer is supposed to match with the "250" I put in the Interval property of the Timer, to give about one second. Larger numbers will make the delay longer, smaller will make it shorter.
Now try it!
这篇关于如何在其他代码之后执行条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!