如何使用C#验证文本框值是否唯一 [英] How can I Validate Textboxes Values To Be Unique using C#

查看:115
本文介绍了如何使用C#验证文本框值是否唯一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有12个文本框,我试图找到一个策略,允许用户在运行时不允许TextBox中的重复条目。



帮助我是这些,如果我的方法是错误的,请用代码建议我更好的方式:



I have 12 text boxes, and I am trying to find a strategy to not permit duplicate entries in the TextBoxes by the user at run-time.

Help me out is these, IF my approach is wrong please suggest me better way with code :

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

private void Textbox1(object sender, EventArgs e)
{
   lstTextBoxes.Add(Textbox1.Text);
}







public bool lstCheck(List<string> lstTextBoxes,string input)
        {
            if(lstTextBoxes.Contains(input))
            {
                return true;
            }
            else
            {
                return false;
            }

        }

        private void Textbox2(object sender, EventArgs e)
        {
            lstTextBoxes.Add(Textbox2.Text);
            if (lstCheck(lstTextBoxes, Textbox2.Text))
            {
                MessageBox.Show("test");
            }
        }

}

推荐答案

而不是列表,使用 System.Collections.Generic.HashSet< T>

http://msdn.microsoft.com/en-us/library/bb359438%28v=vs.110 %29.aspx [ ^ ]。







每次获得一组值的新元素,将其添加到哈希集的实例中: http://msdn.microsoft.com/en-us/library/bb353005(v = vs.110).aspx [ ^ ]。



但是,在执行此操作之前,您已经添加了此元素的检查,因此您需要将其添加到onl上如果它还没有那么。以下是检查方法: http://msdn.microsoft.com /en-us/library/bb356440(v=vs.110).aspx [ ^ ]。



实际上,您可能需要进行一些冗余并添加如果您需要保留添加元素的顺序(自然历史顺序),则可以在其他任何位置使用元素,例如在列表中。如果没关系,你可以使用哈希集,因为它通过 foreach 循环实现枚举。



[结束编辑]



如果你将时间复杂度用于搜索O(1)的副本(渐近,不取决于数据项数量。)

另请参阅:

http:// en .wikipedia.org / wiki / Big_O_notation [ ^ ] ,

http://en.wikipedia.org/wiki/Asymptotic_analysis [ ^ ],

http://en.wikipedia.org/wiki/Time_complexity [ ^ ]。



-SA
Instead of List, use System.Collections.Generic.HashSet<T>:
http://msdn.microsoft.com/en-us/library/bb359438%28v=vs.110%29.aspx[^].



Each time you get a new element of a set of values, add it to the instance of the hash set: http://msdn.microsoft.com/en-us/library/bb353005(v=vs.110).aspx[^].

But, before doing it, you check up of this element is already added, so you need to add it only if it is not yet there. Here is how you check it up: http://msdn.microsoft.com/en-us/library/bb356440(v=vs.110).aspx[^].

Actually, you may need to make some redundancy and add an element anywhere else, for example in a list, if you need to keep the order in which the elements was added (natural historical order). If it does not matter, you can use hash set, because it implements enumeration, through the foreach loop.

[END EDIT]

If would make time complexity for your search for duplicate of O(1) (asymptotically, not depending on the number of data items).
See also:
http://en.wikipedia.org/wiki/Big_O_notation[^],
http://en.wikipedia.org/wiki/Asymptotic_analysis[^],
http://en.wikipedia.org/wiki/Time_complexity[^].

—SA


首先,谢尔盖的答案非常好!

其次,看看我们对谢尔盖答案的讨论。

第三,我的建议是为每个文本框添加公共事件处理程序。怎么样?请参阅代码:在运行时添加控件(Visual C#) [ ^ ]

首先,在事件处理程序内部使用代码:

First of all, Sergey's answer is very good!
Secondly, see our discussion to the Sergey's answer.
Thirdly, my advice is to add common event handler for each textbox. How? Please, see Code: Adding Controls at Run Time (Visual C#)[^]
Fourtly, inside event handler use code:
TextBox tb = (TextBox)sender;
if (!lstTextBoxes.Contains(tb.Text))
{
    lstTextBoxes.Add(tb.Text);
}
else
{
    MessageBox.Show("Is duplicate value!");
}





应该足够了!



It should be enough!


你可以尝试创建一个所有TextBox的单个StackPanel。例如,



You can try to create a single StackPanel for all of the TextBoxes. For example this way,

// create a stackpanel
StackPanel panel = new StackPanel();
panel.Name = "myPanel";
// run a loop 12 times
for (int i = 0; i < 13; i++ ) {
  // creates a new textBox
  TextBox textBox = new TextBox();
  // adds it to the StackPanel
  panel.Children.Add(textBox);
}





如果您愿意,也可以使用XAML来完成此类工作。



然后你可以使用MainWindow网格的Children属性,确保你给这个网格命名,例如





You can also use the XAML for doing this type of job, if you like so.

You can then use the Children property of the MainWindow's grid, make sure that you've given that grid a name, for example

<Grid name="mainGrid">





程序逻辑就像这样,





The program logic would be something like,

foreach (StackPanel childPanel in mainGrid.Children.OfType<stackpanel>()) {
  // might run once, or multiple times depending on your need.
  if(childPanel.Name == "myPanel") {
    // we want our panel to work on
    int index = 0;
    int index2 = 0;
    foreach (TextBox box in childPanel.Children.OfType<textbox>()) {
      // inside that panel, for each of the TextBox
      // check the value against each textbox

      // this is the first child
      index++;
      foreach (TextBox aBox in childPanel.Children.OfType<textbox>()) {
        // increment the second index
        index2++;
        if(index != index2) {
          // 2 different controls, check their values
          if(box.Text == aBox.Text) {
            // there was a duplicate found,
            MessageBox.Show("Found a duplicate.");
            // break the loop, no need for more execution.
            break;
          }
        }
      }
    }
  }
}





很多头痛,不是吗?



A lot of headache, isn't it?


这篇关于如何使用C#验证文本框值是否唯一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆