c#如何检查某个对象是否已存在于我的某个文本框中?如果它然后不添加到文本框

时间:2012-09-26 10:28:10

标签: c# textbox tags

我有一个循环遍历Form1中的每个文本框并获取它们的标记,因为我需要比较对象ID。在我的一个文本框中已经存在对象的情况下,我不希望允许用户再次添加该对象,但如果该对象在任何文本框中都不存在,则只有这样用户才能添加该项。 / p>

我已经尝试过这个循环,但它似乎没有工作,因为它一直告诉我“对象引用未设置为对象的实例。” 在此行上if(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();
                                }
                            }   
                        }

ReportItems

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(这是另一种形式)

    public ReportItems getValue()
    {
        ReportItems selection = (ReportItems)reportListBx.SelectedItem;

        // if user has selected a value             
            return selection;           
    }

3 个答案:

答案 0 :(得分:1)

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

                    // 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;
                    }

答案 1 :(得分:0)

// 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();

        }

答案 2 :(得分:0)

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