如何以编程方式在文本框中搜索文本 [英] How to search a textbox for text, programmatically
问题描述
我可以使用什么代码在文本框中搜索特定的数字.
在我的应用程序中,我有一个循环,每1秒钟在文本框的新行上生成文本.我正在使用随机生成器生成数字,然后执行算法.生成代码后,它将在新行上将其粘贴到文本框中.有时碰巧相同的数字会出现两次或三次!我不喜欢那个.在将其添加到新行之前,如何在整个文本框中搜索相同的数字(如果存在)?
谢谢.
如果有不清楚的地方,请不要犹豫.
我的生成值的代码:
What code can I use to search a text box for a specific number.
In my application, I have a loop to generate text on a new line of a text box every 1 second. I am using the Random generator to generate a number and then execute an algorithm. Once the code is generated, it pastes it on a new line into the textbox. Sometimes it so happens that the same number will come up twice, or thrice! I do not like that however. what can I do to search the entire textbox for the same number (if it exists) before adding it to a new line?
Thank you.
If something seems to be unclear, please don''t hesitate to ask.
edit:
my code that generates the values:
double year = DateTime.Now.Year;
double month = DateTime.Now.Month;
double day = DateTime.Now.Day;
double hour = DateTime.Now.Hour;
double minute = DateTime.Now.Minute;
double second = DateTime.Now.Second;
Random num = new Random();
num.Next().ToString();
if (!richTextBox1.Text.Contains(num))
{
richTextBox1.Text = richTextBox1.Text + (num.Next(0, 9) * month) + (num.Next(0, 9) * minute) + num.Next(0, 9) + "-" + (num.Next(0, 9) * day) + (num.Next(0, 9) + second) + num.Next(0, 9) + "-" + (num.Next(0, 9) + (year / 10)) + (num.Next(0, 9) + hour) + num.Next(0, 9) + "\r\n";
}
推荐答案
if(!TextBox1.Text.Contains(TextToSearch))
TextBox1.Text += "\r\n" + TextToSearch;
您可以使用上面的语句在文本框中搜索文本.
You can use above statement to search text in the TextBox.
尝试一下..
我觉得这对你来说就足够了.
Try this..
I think this is sufficient for you..
if (TextBox1.Text.IndexOf(dataToSearch)>-1)
{
Console.WriteLine("data found.");
}
else
Console.WriteLine("data not found")
一切顺利.
all the best..
这篇关于如何以编程方式在文本框中搜索文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!