各位达达好,小弟发现listbox比Textbox的效率高很多,于是在程序中开始大量使用Listbox来打Trace,但是有时候需要复制某些Trace出来分析,发现右击无菜单,请问怎么加右击菜单,或者告诉我为什么Textbox有右击菜单,我自己来重写下listbox控件。
public Form1() { InitializeComponent(); ContextMenuStrip listboxMenu = new ContextMenuStrip(); ToolStripMenuItem rightMenu = new ToolStripMenuItem("Copy"); rightMenu.Click += new EventHandler(Copy_Click); listboxMenu.Items.AddRange(new ToolStripItem[] { rightMenu }); listbox1.ContextMenuStrip = listboxMenu; listbox1.Items.Add("1"); listbox1.Items.Add("2"); listbox1.Items.Add("3"); listbox1.Items.Add("4"); } private void Copy_Click(object sender, EventArgs e) { Clipboard.SetText(listbox1.Items[listbox1.SelectedIndex].ToString()); }
Copy_Click这个事件是在哪儿?
@飘雪无痕007: 不是写在那了吗
private void Copy_Click(object sender, EventArgs e) { Clipboard.SetText(listbox1.Items[listbox1.SelectedIndex].ToString()); }
public FrmMain()
{
InitializeComponent();
ContextMenuStrip listboxMenu = new ContextMenuStrip();
ToolStripMenuItem rightMenu = new ToolStripMenuItem("Copy");
rightMenu.Click += new EventHandler(Copy_Click);
listboxMenu.Items.AddRange(new ToolStripItem[] { rightMenu });
lbMessage.ContextMenuStrip = listboxMenu;
}
private void Copy_Click(object sender, EventArgs e)
{
string CopyText = "";
for (int i = 0; i <lbMessage.SelectedItems.Count; i++)
{
CopyText = CopyText + Environment.NewLine + lbMessage.SelectedItems[i].ToString();
}
Clipboard.SetText(CopyText);
}
多项复制!!!