我有这个以编程方式添加到 ToolStripMenuItem 的 DropDownButton。当我单击它时,我希望它触发一个事件,但它不会在 Event 块内执行任何操作。请查看此代码并评论我做错了什么。
谢谢
private void privateChatToolStripMenuItem_Click(object sender, EventArgs e)
{
Icon icon1 = new Icon(Assembly.GetExecutingAssembly().GetManifestResourceStream("Chat_NET_Client.pm.ico"));
Bitmap bmp = icon1.ToBitmap();
ToolStripMenuItem item = new ToolStripMenuItem(listView1.SelectedItems[0].Text, bmp);
item.DropDownItemClicked += item_Click;
chatsToolStripMenuItem.DropDownItems.Add(item);
}
void onClick(object sender, ToolStripItemClickedEventArgs e)
{
inPM = true;
txtLog.Visible = false;
pmBox.Visible = true;
mainToolStripMenuItem1.Enabled = true;
pmBox.Clear();
curPM = e.ClickedItem.Text;
Invoke(new _appendLog(appendLog), "STARTING PM :D");
using (StreamReader r = new StreamReader(appData + "\\" + e.ClickedItem.Text + ".dat"))
{
string line;
while (r.ReadLine() != null)
{
line = r.ReadLine();
pmBox.AppendText(line + Environment.NewLine);
pmBox.ScrollToCaret();
}
}
}
最后说明:DropDown 项目确实被创建和添加,问题是它在单击时不会触发事件!