c#如何检查某个对象是否已经在我的某个文本框中?如果是,则不要将其添加到文本框中

c#如何检查某个对象是否已经在我的某个文本框中?如果是,则不要将其添加到文本框中,c#,textbox,tags,C#,Textbox,Tags,我有一个循环遍历Form1中的每个文本框,并获取它们的标记,因为我需要比较对象ID。如果某个对象已经存在于我的某个文本框中,我不想允许用户再次添加此对象,但如果任何文本框中都不存在此对象,则用户只能添加此项 我在下面的循环中尝试过,但它似乎不起作用,因为它一直告诉我“对象引用未设置为对象的实例。”如果(resval.types.xan_ID==tbItems.types.xan_ID) 在我得到我想要的消息框后,我如何更改此代码以实现此目标 //

我有一个循环遍历Form1中的每个文本框,并获取它们的标记,因为我需要比较对象ID。如果某个对象已经存在于我的某个文本框中,我不想允许用户再次添加此对象,但如果任何文本框中都不存在此对象,则用户只能添加此项

我在下面的循环中尝试过,但它似乎不起作用,因为它一直告诉我“对象引用未设置为对象的实例。”如果(resval.types.xan_ID==tbItems.types.xan_ID) 在我得到我想要的消息框后,我如何更改此代码以实现此目标

                        // Get the name which will be passed into the textbox
                        var resval = form2result.getValue();

                        //go through each of my textbox
                        foreach (TextBox tb in TextBoxList)
                        {
                            var tbItems = (ReportItems)tb.Tag;
                            if (tb.Text != "")
                            {
                                //if the item returned is the same as an item in the textbox
                                if (resval.types.xan_ID == tbItems.types.xan_ID)
                                {
                                    // display this message and break out of the loop
                                    MessageBox.Show("You have previously selected this report, please chose another");
                                    break;
                                }
                                    // otherwise add the item into the textbox.
                                else
                                {
                                    // otherwise add name to the textbox
                                    _dict[sender].Text = resval.ToString();
                                }
                            }   
                        }
报告项目

public class ReportItems
{
    public DataSet1.xspGetAnalysisTypesRow types { get; set; }

    //Analysis types or Reports
    public ReportItems(DataSet1.xspGetAnalysisTypesRow analysisTypes)
    {
        types = analysisTypes;
    }

    //Return the name of this type. 
    public override string ToString()
    {
        return this.types.xan_Name;
    }
}
getValueFunction(这是另一种形式)


我不确定dict[sender]和TextBoxList之间的链接是什么,但您没有将标记设置为与设置文本相同的点。假设这些引用的是同一个对象,那么下次使用此方法时将导致错误,因为您将有一个没有标记的文本框

                    // Get the name which will be passed into the textbox
                    var resval = form2result.getValue();
                    // The user didn't select anything somehow.
                    if (resval == null)
                    {
                      MessageBox.Show("Nothing Selected"); 
                      return;
                    }
                    // resval hasn't been setup correctly.
                    if (resval.types == null)
                    {
                      MessageBox.Show("Internal Error"); 
                      return;
                    }
                    Boolean alreadyExists = false;
                    //go through each of my textbox
                    foreach (TextBox tb in TextBoxList)
                    {
                        var tbItems = (ReportItems)tb.Tag;
                        //The Textbox must contain text and tbItems must not be null
                        if (tb.Text != "" && tbItems != null)
                        {
                            //The tag has been set, but somehow the types are null?
                            if (tbItems.types == null)
                            {
                                MessageBox.Show("Internal Error");    
                            break;
                            }
                            //if the item returned is the same as an item in the textbox
                            if (resval.types.xan_ID == tbItems.types.xan_ID)
                            {
                                alreadyExists = true;
                                // display this message and break out of the loop
                                MessageBox.Show("You have previously selected this report, please chose another");
                                break;
                            }
                                // otherwise add the item into the textbox.
                        }   
                    }
                    if (!alreadyExists)
                    {
                        // otherwise add name to the textbox
                        _dict[sender].Text = resval.ToString();
                        //set the tag? 
                        _dict[sender].Tag = tbItems;
                    }

您可以使用jquery处理文本框(用户要添加的焦点)的onmouseover或onclick事件,并可以检查用户是否将对象添加到以前的文本框中

尝试
string reversion=(string)(form2result.getValue())。它将是
null
,而不是引发异常。[这是在反转引发异常的情况下发生的。]这一行的错误是
(resval.types.xan_ID==tbItems.types.xan_ID)
它在哪个linie上崩溃?@JamesB错误行在(resval.types.xan_ID==tbItems.types.xan_ID)上。在比较它们之前,您需要检查
tbItems
resval
是否为空。我有12个文本框,textbox1中有一个值。。textbox4也是如此,如果我尝试将一个已经在textbox1中的项目添加到textbox2中,它会显示一个对话框,说这个项目已经被添加,这很好,因为这是我想要的。但是,当我尝试将一个项目添加到textbox2(位于textbox4中)时,它也会显示对话框,但仍会继续将该项目添加到textbox2@ifelabolz我已经更新了我的答案来处理这种情况,在添加到任何文本框之前,你需要检查所有文本框。非常感谢,终于可以工作了。。。我一直在想办法。再次感谢。
// Get the name which will be passed into the textbox
        var resval = form2result.getValue();
        ArrayList arrayList = new ArrayList();

        //go through each of my textbox
        foreach (TextBox tb in TextBoxList)
        {
            var tbItems = (ReportItems) tb.Tag;
            if (tb.Text != "") return;
            //if the item returned is the same as an item in the textbox
            /* Try this if the below line doesnt work
               if(string.IsNullOrEmpty(resval.types.xan_ID) || string.IsNullOrEmpty(tbItems.types.xan_ID) return;

               if (resval.types.xan_ID == tbItems.types.xan_ID) return;

            */
            if ((string)(resval.types.xan_ID) == (string)(tbItems.types.xan_ID)) return;
            // otherwise add the item into the textbox.

            // otherwise add name to the textbox
            arrayList.Add(resval.ToString());
        }

        foreach (var arr in arrayList)
        {
            // something = arr.ToString();

        }
                    // Get the name which will be passed into the textbox
                    var resval = form2result.getValue();
                    // The user didn't select anything somehow.
                    if (resval == null)
                    {
                      MessageBox.Show("Nothing Selected"); 
                      return;
                    }
                    // resval hasn't been setup correctly.
                    if (resval.types == null)
                    {
                      MessageBox.Show("Internal Error"); 
                      return;
                    }
                    Boolean alreadyExists = false;
                    //go through each of my textbox
                    foreach (TextBox tb in TextBoxList)
                    {
                        var tbItems = (ReportItems)tb.Tag;
                        //The Textbox must contain text and tbItems must not be null
                        if (tb.Text != "" && tbItems != null)
                        {
                            //The tag has been set, but somehow the types are null?
                            if (tbItems.types == null)
                            {
                                MessageBox.Show("Internal Error");    
                            break;
                            }
                            //if the item returned is the same as an item in the textbox
                            if (resval.types.xan_ID == tbItems.types.xan_ID)
                            {
                                alreadyExists = true;
                                // display this message and break out of the loop
                                MessageBox.Show("You have previously selected this report, please chose another");
                                break;
                            }
                                // otherwise add the item into the textbox.
                        }   
                    }
                    if (!alreadyExists)
                    {
                        // otherwise add name to the textbox
                        _dict[sender].Text = resval.ToString();
                        //set the tag? 
                        _dict[sender].Tag = tbItems;
                    }