我有一個列表框,我想爲列表中的每個項目添加一個上下文菜單。我已經看到了「解決方案」,如果在空白區域右鍵單擊選擇一個項目並禁用上下文菜單,但此解決方案感覺很髒。如何將上下文菜單添加到ListBoxItem?
有誰知道更好的方法?
我有一個列表框,我想爲列表中的每個項目添加一個上下文菜單。我已經看到了「解決方案」,如果在空白區域右鍵單擊選擇一個項目並禁用上下文菜單,但此解決方案感覺很髒。如何將上下文菜單添加到ListBoxItem?
有誰知道更好的方法?
這樣的菜單旁邊彈出鼠標
private string _selectedMenuItem;
private readonly ContextMenuStrip collectionRoundMenuStrip;
public Form1()
{
var toolStripMenuItem1 = new ToolStripMenuItem {Text = "Copy CR Name"};
toolStripMenuItem1.Click += toolStripMenuItem1_Click;
var toolStripMenuItem2 = new ToolStripMenuItem {Text = "Get information on CR"};
toolStripMenuItem2.Click += toolStripMenuItem2_Click;
collectionRoundMenuStrip = new ContextMenuStrip();
collectionRoundMenuStrip.Items.AddRange(new ToolStripItem[] {toolStripMenuItem1, toolStripMenuItem2 });
listBoxCollectionRounds.MouseDown += listBoxCollectionRounds_MouseDown;
}
private void toolStripMenuItem2_Click(object sender, EventArgs e)
{
var info = GetInfoByName(_selectedMenuItem);
MessageBox.Show(info.Name + Environment.NewLine + info.Date);
}
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
Clipboard.SetText(_selectedMenuItem);
}
private void myListBox_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Right) return;
var index = myListBox.IndexFromPoint(e.Location);
if (index != ListBox.NoMatches)
{
_selectedMenuItem = listBoxCollectionRounds.Items[index].ToString();
collectionRoundMenuStrip.Show(Cursor.Position);
collectionRoundMenuStrip.Visible = true;
}
else
{
collectionRoundMenuStrip.Visible = false;
}
}
沒有其他方法:上下文菜單不屬於列表框中的項目,而是由列表框本身擁有。它與樹視圖控件類似,它也擁有上下文菜單而不是treenode。因此,無論何時選擇列表框中的項目,都應根據所選項目設置列表框的上下文菜單。
只是爲了詳細闡述一下Frans的說法......即使ListBox擁有ContextMenuStrip,您仍然可以在打開時自定義菜單條中的項目。因此基於列表框中的鼠標位置來定製它的內容。
下面的示例基於右鍵單擊選擇列表框中的項目,然後根據用戶右鍵單擊的項目自定義上下文菜單條。這是一個簡單的例子,但應該讓你去:一個列表框添加到窗體,並添加以下代碼:
print(" #region Private Members
private ContextMenuStrip listboxContextMenu;
#endregion
private void Form1_Load(object sender, EventArgs e)
{
//assign a contextmenustrip
listboxContextMenu = new ContextMenuStrip();
listboxContextMenu.Opening +=new CancelEventHandler(listboxContextMenu_Opening);
listBox1.ContextMenuStrip = listboxContextMenu;
//load a listbox
for (int i = 0; i < 100; i++)
{
listBox1.Items.Add("Item: " + i);
}
}
private void listBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
//select the item under the mouse pointer
listBox1.SelectedIndex = listBox1.IndexFromPoint(e.Location);
if (listBox1.SelectedIndex != -1)
{
listboxContextMenu.Show();
}
}
}
private void listboxContextMenu_Opening(object sender, CancelEventArgs e)
{
//clear the menu and add custom items
listboxContextMenu.Items.Clear();
listboxContextMenu.Items.Add(string.Format("Edit - {0}", listBox1.SelectedItem.ToString()));
} ");
希望可以幫助。 -Mike
如果它只是啓用或禁用上下文菜單項的問題,它可能是更有效的只有做時,上下文菜單中啓動,而不是每次都在列表框中選擇的變化:
myListBox.ContextMenu.Popup += new EventHandler(myContextPopupHandler);
private void myContextPopupHandler(Object sender, System.EventArgs e)
{
if (SelectedItem != null)
{
ContextMenu.MenuItems[1].Enabled = true;
ContextMenu.MenuItems[2].Enabled = true;
}
else
{
ContextMenu.MenuItems[1].Enabled = false;
ContextMenu.MenuItems[2].Enabled = false;
}
}
在XAML是這樣表示:
<ListBox> <ListBox.ContextMenu> <ContextMenu> <MenuItem Header="Add User..."/> <MenuItem Header="Edit..."/> <MenuItem Header="Disable"/> </ContextMenu> </ListBox.ContextMenu> </ListBox>
這一個是最好的...
using System.Windows.Forms;
ContextMenuStrip menu;
this.menu.Items.AddRange(new ToolStripItem[] { this.menuItem });
this.listBox.MouseUp += new MouseEventHandler(this.mouse_RightClick);
private void mouse_RightClick(object sender, MouseEventArgs e)
{
int index = this.listBox.IndexFromPoint(e.Location);
if (index != ListBox.NoMatches)
{
menu.Visible = true;
}
else
{
menu.Visible = false;
}
}
//Create and Initialize the contextMenuStrip component
contextMenuStrip_ListaAulas = new ContextMenuStrip();
//Adding an Item
contextMenuStrip_ListaAulas.Items.Add("Modificar");
//Binding the contextMenuStrip with the ListBox
listBox_Aulas.ContextMenuStrip = contextMenuStrip_ListaAulas;
//The solution below
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
//select the item under the mouse pointer
listBox_Aulas.SelectedIndex = listBox_Aulas.IndexFromPoint(e.Location);
//if the selected index is an item, binding the context MenuStrip with the listBox
if (listBox_Aulas.SelectedIndex != -1)
{
listBox_Aulas.ContextMenuStrip = contextMenuStrip_ListaAulas;
}
//else, untie the contextMenuStrip to the listBox
else
{
listBox_Aulas.ContextMenuStrip = null;
}
}
而且這是我的解決方案:
listBox_Usernames.ContextMenuStrip = contextMenuStripRemove;
listBox_Usernames.MouseUp += new MouseEventHandler(listBox_Usernames_MouseUp);
void listBox_Usernames_MouseUp(object sender, MouseEventArgs e)
{
int index = listBox_Usernames.IndexFromPoint(e.Location);
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
if (index != ListBox.NoMatches)
{
if (listBox_Usernames.SelectedIndex == index)
{
listBox_Usernames.ContextMenuStrip.Visible = true;
}
else
{
listBox_Usernames.ContextMenuStrip.Visible = false;
}
}
else
{
listBox_Usernames.ContextMenuStrip.Visible = false;
}
}
else
{
listBox_Usernames.ContextMenuStrip.Visible = false;
}
}
我喜歡這個,這個作品對我來說是偉大而快速的。
private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
if (Listbox.SelectedItem == null)
e.Cancel = true;
}
在代碼(多爲綁定)一個行:
var datasource = new BindingList<string>(new List<string>(new string[] { "item1" }));
listbox.DataSource = datasource ;
listbox.ContextMenu = new ContextMenu(
new MenuItem[] {
new MenuItem("Delete",
new EventHandler((s,ev) =>
datasource.Remove(listbox.SelectedItem.ToString())
)
)
});
private void buttonAdd_Click(object sender, EventArgs e)
{
datasource.Add(textBox.Text);
}
你能不能設計任何其他方式?我想不出任何這樣的用戶界面......我從來沒有想過在列表框項目上單擊右鍵。 – Gishu 2008-12-18 04:57:21
你也在Winforms或WPF?答案可能因此而異。 – Gishu 2008-12-18 04:57:56