private void BtnDeviceListDel_Click(object sender, EventArgs e) { DeviceItem d = (DeviceItem)listBox1.SelectedItem; if (d == null) return; if (MessageBox.Show("删除设备:\n" + d.name + " (" + d.mac + ")", "删除设备", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.OK) { int index = listBox1.SelectedIndex; if (listBox1.Items.Count == 1) index = -1; else if (index > 0) index--; listBox1.Items.RemoveAt(listBox1.SelectedIndex); listBox1.SelectedIndex = index; } }
private void ListBox1_DrawItem(object sender, DrawItemEventArgs e) { if (e.Index == -1) return; e.DrawBackground(); e.DrawFocusRectangle(); // prefixes are drawed bold DeviceItem device = (DeviceItem)((ListBox)sender).Items[e.Index]; //draw the ICON Rectangle rectangle = new Rectangle(e.Bounds.Location, new Size(DEVICE_LIST_ITEM_HEIGHT, DEVICE_LIST_ITEM_HEIGHT)); //e.Graphics.DrawImage(Properties.Resources.device_item_0, e.Bounds.Location); e.Graphics.DrawImage(Properties.Resources.device_item_0, rectangle); //draw the txt Brush fontColor = new SolidBrush(Color.Black); Font nameFont = new Font(e.Font.FontFamily, 12, FontStyle.Bold); Rectangle newBounds = new Rectangle(e.Bounds.Location, e.Bounds.Size); // draw the name string e.Graphics.DrawString(device.name, nameFont, fontColor, newBounds.X + DEVICE_LIST_ITEM_HEIGHT, newBounds.Y + 5); // calculate the new rectangle // draw the mac string e.Graphics.DrawString(device.mac, e.Font, fontColor, newBounds.X + DEVICE_LIST_ITEM_HEIGHT + 2, newBounds.Y + 26); // draw the focus e.DrawFocusRectangle(); }