开发者

Typing only non null fields?

string[] lines = { textBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text, textBox5.Text, textBox6.Text };
Random RandString = new Random();
string text = lines[RandString.Next(0, lines.Length)];
SendKeys.SendWait(text);

How can I select textBox's开发者_StackOverflow中文版 with text inside of them? I was trying to sort the data string[] lines into a new string[] hasText but I'm unsure how to go about checking if the textBox has text inside of it.

If the textBox field is null it will still process through RandString and SendKeys will try to type it out. How can I fix this?

Thanks.


You could filter the array with LINQ:

string[] hasText = lines.Where(s => !String.IsNullOrEmpty(s)).ToArray();


Using the IsNullOrEmpty (here)

TextBox[] textboxes = { textBox1, textBox2, textBox3, textBox4, textBox5, textBox6};

List<string> lines = new List<string>();

foreach(Textbox tb in textboxes)
{
    if (!tb.text.IsNullOrEmpty()
    {
        lines.Add(tb);
    }
}

Random RandString = new Random();
string text = lines[RandString.Next(0, lines.Length)];
SendKeys.SendWait(text);

Note: I haven't compiled it

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜