0

我有一个循环遍历 Form1 中的每个文本框并获取它们的标签,因为我需要比较对象 ID。在我的一个文本框中已经存在一个对象的情况下,我不想让用户再次添加这个对象,但是如果这个对象在任何一个文本框中都不存在,那么用户只能添加这个项目。

我已经在下面的这个循环中尝试过了,但它似乎不起作用,因为它一直告诉我“对象引用未设置为对象的实例。” 在这条线上 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();
                                }
                            }   
                        }

报告项目

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

3 回答 3

1

我不确定 _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;
                    }
于 2012-09-26T11:01:18.000 回答
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();

        }
于 2012-09-26T10:35:30.980 回答
0

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

于 2012-09-26T11:03:57.167 回答