如何通过随机函数从10个文本框中选择一个文本框? [英] how can i select one of a textbox from 10 textbox by random function?
本文介绍了如何通过随机函数从10个文本框中选择一个文本框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何通过随机函数从10个文本框中选择一个文本框?
How can I select one of a textbox from 10 textbox by random function?
推荐答案
显而易见的解决方案:
使用 Random.Next [ ^ ],之后按生成的数字和致电焦点 [选择 [ ^ ]方法.
[提示]
如果将TextBox
引用保留在数组中或列出,则switch语句将替换为该数组中的单行索引. :)
[/TIP]
The obvious solution:
Generate a random number between 1 and 10 (or 0 and 9) using Random.Next[^], after that switch by generated number and call Focus[^] or Select[^] method for the chosenTextBox
.
[TIP]
If you keep yourTextBox
references in array or list the switch statement will be replaced with a one-line indexing in that array. :)
[/TIP]
private Random rnd = new Random();
private void SelectTextBox()
{
int i = rnd.Next(1, 10);
foreach (Control c in this.Controls)
{
if (c is TextBox && c.Name == "textBox" + i.ToString())
{
c.Select();
break;
}
}
}
这篇关于如何通过随机函数从10个文本框中选择一个文本框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文