When I run the following code through on the first round, my sub-menu
(branched off mnuOpenPorts menuitem) shows a nice list of ports.
When it gets run through on the 2nd pass, the sub menu indicates that it
should be there but there's no items appearing in it. (ie a small indicator
arrow is there showing there should be a sub menu but nothing pops up)
When I step through the code, I have 5 items in Comports. After adding all
the sub menu items, all 5 appear to be listed in mnuOpenPorts.MenuItems
Any ideas what I'm doing wrong?
thanks
/// Adds a submenu item for each available serial port showing current
status
private void TestComports()
{
MenuItem item;
string str;
int idx = 0;
// Clear old sub menu
mnuOpenPort.MenuItems.Clear();
// Show items in the list box
for (int nCount = 0; nCount < Comports.Count; nCount++)
{
Sax.Communications.SerialOptions Options = new
Sax.Communications.SerialOptions(SerConn.Options);
Options.PortName = Comports[nCount];
// Set the options
SerConn.SetOptions(Options);
if ((m_CommunicationManager.SerialConnected)
&& (m_CommunicationManager.SerialPortName == Comports[nCount]))
{
// add menu item saying this is the connected port
str = string.Format("{0} (Open)", (string)Comports[nCount]);
lstPorts.Items.Add(str);
//Create new menu item
item = new MenuItem(str);
item.Checked = true;
item.Click += new EventHandler(cmdConnect_Click);
//Add item to the submenu
mnuOpenPort.MenuItems.Add(item);
}
else
{
str = "";
// Test ports will open. If they open OK then close them again
try
{
SerConn.Open();
str = string.Format("{0} (Closed)", (string)Comports[nCount]);
}
catch(System.IO.IOException)
{
str = string.Format("{0} (Not available)", (string)Comports[nCount]);
}
finally
{
try
{
if (SerConn.IsOpen)SerConn.Close();
}
catch
{
}
}
// Add new item to submenu
lstPorts.Items.Add(str);
item = new MenuItem(str);
item.Click += new EventHandler(cmdConnect_Click);
mnuOpenPort.MenuItems.Add(item);
}
}
}