I have a loop going through each of my textboxes in Form1 and getting the tag for them as i will need to compare object IDs. in situations where an object already exist in one of my textboxes, i do not want to allow the user to add this object again, but if this object does not exist in any of the textboxes, only then can the user add this item.
I have tried it with this loop below but it doesnt seem to work as it keeps telling me “Object reference not set to an instance of an object.” on this line if (resval.types.xan_ID == tbItems.types.xan_ID)
after i get the message box which i want, how can I change this code to achieve this goal.
// 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 (This is in a different form)
public ReportItems getValue()
{
ReportItems selection = (ReportItems)reportListBx.SelectedItem;
// if user has selected a value
return selection;
}
I’m not sure what the link is between _dict[sender] and TextBoxList but you haven’t set the Tag as the same point as setting the text. Assuming these are referring to the same object this will cause an error the next time you use this method as you’ll have a text box with no tag.