How can I do random name picker in multiple textboxes in c# framwork? [closed]

我的未来我决定 提交于 2021-02-17 07:14:17

问题


I need help with random name picker in multiple textboxes in c# framwork. Can someone help me with how I can get started? How to think? I would appreciate the help!


回答1:


You can call Enumerable.OfType<TResult>(IEnumerable) Method to get the list of all textboxes.

And use Random Class to generate a random index.

private void btnrandompicker_Click(object sender, EventArgs e)
{
    var random = new Random();
    // get textboxes list
    var tblist = this.Controls.OfType<TextBox>().ToList();
    var randomtb = tblist[random.Next(tblist.Count)];

    Console.WriteLine(randomtb.Text);
}



回答2:


I will give you an example for winforms in C# language.

List<int> numberList = new List<int>();
foreach (Control ctr in this.Controls)
{
    if (ctr is TextBox)
    {
       GenerateRandom:
       Random random = new Random();
       int number = random.Next(125, 900);
       var findNumber = numberList.Find(a => a == number);
       if (findNumber == 0)
       {
          ctr.Name = "txt" + number;
          numberList.Add(number);
       }
       else
          goto GenerateRandom;
   }
}


来源:https://stackoverflow.com/questions/65946553/how-can-i-do-random-name-picker-in-multiple-textboxes-in-c-sharp-framwork

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!