コード例 #1
0
        private void mnuDeviceAdd_DropDownOpening(object sender, EventArgs e)
        {
            mnuDeviceAdd.DropDownItems.Clear();
            foreach (var driver in Drivers.LoadedDrivers)
            {
                var menuItem = new ToolStripMenuItem(driver.Capabilities.DisplayName);
                menuItem.Click += delegate(object senderClick, EventArgs eClick) {
                    switch (driver.Capabilities.Interface)
                    {
                    case DmmDriverInterface.None: {
                        using (var frm = new NewDeviceNoInterfaceForm(driver)) {
                            if (frm.ShowDialog(this) == DialogResult.OK)
                            {
                                var device = Devices.Add(driver, frm.SelectedDisplayName, "");
                                if (device != null)
                                {
                                    AddDeviceMenu(device);
                                }
                            }
                        }
                    } break;

                    case DmmDriverInterface.SerialPort: {
                        using (var frm = new NewDeviceSerialInterfaceForm(driver)) {
                            if (frm.ShowDialog(this) == DialogResult.OK)
                            {
                                var device = Devices.Add(driver, frm.SelectedDisplayName, frm.SelectedSettings);
                                if (device != null)
                                {
                                    AddDeviceMenu(device);
                                }
                            }
                        }
                    } break;

                    default: throw new NotSupportedException("Unknown interface '" + driver.Capabilities.Interface.ToString() + "'.");
                    }

                    sidebar.Invalidate();
                };
                mnuDeviceAdd.DropDownItems.Add(menuItem);
            }
        }
コード例 #2
0
        private void FillDeviceDropDown(ToolStripItemCollection menu, Device device, SidebarControl sidebarControl)
        {
            if (this.IsConnected == true)
            {
                var mnuDisconnect = new ToolStripMenuItem("Disconnect");
                mnuDisconnect.Click += delegate(object sender, EventArgs e) {
                    sidebarControl.Invalidate();
                    this.Disconnect();
                };
                menu.Add(mnuDisconnect);
            }
            else if (this.IsConnected == false)
            {
                var mnuConnect = new ToolStripMenuItem("Connect");
                mnuConnect.Click += delegate(object sender, EventArgs e) {
                    this.Connect();
                };
                menu.Add(mnuConnect);
            }
            else
            {
                menu.Add(new ToolStripMenuItem("Connecting...")
                {
                    Enabled = false
                });
            }

            menu.Add(new ToolStripSeparator());

            var mnuConfigure = new ToolStripMenuItem("Configure", ManifestResources.GetBitmap("DmmLog.Resources.DmmMenuEdit.png"));

            if (this.IsConnected == false)
            {
                mnuConfigure.Click += delegate(object sender, EventArgs e) {
                    String newDisplayName = this.DisplayName;
                    String newSettings    = this.Settings;
                    switch (this.Driver.Capabilities.Interface)
                    {
                    case DmmDriverInterface.None: {
                        using (var frm = new NewDeviceNoInterfaceForm(this)) {
                            if (frm.ShowDialog(this.Window) == DialogResult.OK)
                            {
                                newDisplayName = frm.SelectedDisplayName;
                                newSettings    = "";
                            }
                        }
                    } break;

                    case DmmDriverInterface.SerialPort: {
                        using (var frm = new NewDeviceSerialInterfaceForm(this)) {
                            if (frm.ShowDialog(this.Window) == DialogResult.OK)
                            {
                                newDisplayName = frm.SelectedDisplayName;
                                newSettings    = frm.SelectedSettings;
                            }
                        }
                    } break;

                    default: throw new NotSupportedException("Unknown interface '" + this.Driver.Capabilities.Interface.ToString() + "'.");
                    }

                    if (!string.Equals(this.DisplayName, newDisplayName, StringComparison.Ordinal))   //change display name
                    {
                        this.DisplayName = newDisplayName;
                        if (this.MenuItem != null)
                        {
                            this.MenuItem.Text = this.DisplayName;
                        }
                    }

                    if (!string.Equals(this.Settings, newSettings, StringComparison.Ordinal))   //change settings
                    {
                        this.Settings = newSettings;
                        this.Instance.Dispose();
                        this.Instance = this.Driver.GetInstance(this.Settings);
                    }
                };
            }
            else
            {
                mnuConfigure.Enabled = false;
            }
            menu.Add(mnuConfigure);

            var mnuRemove = new ToolStripMenuItem("Remove", ManifestResources.GetBitmap("DmmLog.Resources.DmmMenuRemove.png"));

            if (this.IsConnected == false)
            {
                mnuRemove.Click += delegate(object sender, EventArgs e) {
                    if (Medo.MessageBox.ShowQuestion(this.Window, "Do you really want to remove " + this.DisplayName + "?", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        Devices.Remove(this);
                        this.Menu.Items.Remove(this.MenuItem);
                    }
                    sidebarControl.Invalidate();
                };
            }
            else
            {
                mnuRemove.Enabled = false;
            }
            menu.Add(mnuRemove);
        }