I am adding existing COM ports available on the system to the drop-down list. So far I have this:
private void Form1_Load(object sender, EventArgs e) { foreach (string port in System.IO.Ports.SerialPort.GetPortNames()) { ToolStripMenuItem t = new ToolStripMenuItem(); t.Text = port; t.Checked = port == notifier.COMPort; t.Click += t_Click; setPortToolStripMenuItem.DropDownItems.Add(t); } }
This works when adding ports initially, however I want to check again for COM ports that have been added or removed before the user clicks on my drop-down box to display the ports.
I see that I can use setPortToolStripMenuItem.DropDOwnItems.ContainsKey() , but I have no idea what keys it uses when I add them.
This does not work:
private void setPortToolStripMenuItem_DropDownOpening(object sender, EventArgs e) { string[] ports = System.IO.Ports.SerialPort.GetPortNames(); foreach (string s in ports) { if (!setPortToolStripMenuItem.DropDownItems.ContainsKey(s)) { ToolStripMenuItem t = new ToolStripMenuItem(); t.Text = s; t.Checked = s == notifier.COMPort; t.Click += t_Click; setPortToolStripMenuItem.DropDownItems.Add(t); } } }
Is it possible to indicate which key it uses when I add an item, or is there another way to check for existing items?
Logan source share