private SortedList<ToolStripMenuItem, Form> forms = new SortedList<ToolStripMenuItem, Form>();
private void MainForm_Load(object sender, EventArgs e)
{
formsAdd(menuCommandPrompt, new CommandPrompt());
formsAdd(menuLogScreen, new LogScreen()); //Error
}
private void formsAdd(ToolStripMenuItem item, Form form)
{
forms.Add(item, form); //Failed to compare two elements in the array.
form.Tag = this;
form.Owner = this;
}
I can't get that why it throws error. Error occurs on second line of form load event.
formsAdd method simply adds form and toolstip element to the array(forms) and sets tag and owner of form to this. On second call of function, it throws an error.
CommandPrompt, LogScreen /* are */ Form //s
menuCommandPrompt, menuLogScreen /* are */ ToolStripMenuItem //s
You have a SortedList
, but ToolStripMenuItem
does not implement IComparable
, so the list does not know how to sort them.
If you don't need to have the items sorted, you can use a list of Tuple
s or a Dictionary
, depending on what exactly do you want to do.
If you want to have them sorted, you need use the overload of SortedLists
's constructor that takes IComparer
. That means you have to implement that interface in some way.
Do both your object types implement IComparable? This is a must for the sorted list to compare the objects it is adding to the array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With