现在,请,我知道如何在我右键单击listView时弹出一个contextMenu,我想要的是当我右键单击时弹出它项目即可。我正在尝试制作聊天服务器和客户端,现在......现在我想在我右键单击连接的客户端项目时查看客户端信息。现在我该怎么做?
谢谢!
答案 0 :(得分:56)
private void listView1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
if (listView1.FocusedItem.Bounds.Contains(e.Location))
{
contextMenuStrip1.Show(Cursor.Position);
}
}
}
您可以将连接的客户端信息放在contextMenuStrip1中。当您右键单击某个项目时,您可以显示该contextMenuStrip1中的信息。
答案 1 :(得分:8)
您将不得不使用ListViews上下文菜单,但根据您右键单击的ListView项目进行更改。
private void listView1_MouseDown(object sender, MouseEventArgs e)
{
bool match = false;
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
foreach (ListViewItem item in listView1.Items)
{
if (item.Bounds.Contains(new Point(e.X, e.Y)))
{
MenuItem[] mi = new MenuItem[] { new MenuItem("Hello"), new MenuItem("World"), new MenuItem(item.Name) };
listView1.ContextMenu = new ContextMenu(mi);
match = true;
break;
}
}
if (match)
{
listView1.ContextMenu.Show(listView1, new Point(e.X, e.Y));
}
else
{
//Show listViews context menu
}
}
}
答案 2 :(得分:3)
您可以触发MouseDown
的{{1}}或MouseUp
事件,如果ListView
,则使用ListView.Hittest
抓取所选项目,并提供相关的上下文菜单选择项目。
有关更详细的信息,您可以浏览 link
答案 3 :(得分:2)
这个话题很老了,但我会留下我的解决方案供参考。
在xaml ListView定义中放置上下文菜单:
<ListView Name="m_list" >
<ListView.ContextMenu>
<ContextMenu>
<MenuItem Header="menuItem1" Click="ContextMenuItem1Clicked" />
<MenuItem Header="menuItem2" Click="ContextMenuItem2Clicked" />
</ContextMenu>
</ListView.ContextMenu>
...
</ListView>
现在,在代码中,定义两个事件处理程序,它们将在单击相应的菜单项时启动:
private void ContextMenuItem1Clicked(object sender, RoutedEventArgs e)
{
// handle the event for the selected ListViewItem accessing it by
ListViewItem selected_lvi = this.m_list.SelectedItem as ListViewItem;
}
private void ContextMenuItem2Clicked(object sender, RoutedEventArgs e)
{
// handle the event for the selected ListViewItem accessing it by
ListViewItem selected_lvi = this.m_list.SelectedItem as ListViewItem;
}
ListView可以容纳对象,这意味着selected_lvi可以是对象的类型。只是施放到正确的类型。
我希望这会有所帮助。
致以最诚挚的问候,
麦克
答案 4 :(得分:0)
我找到了一个不依赖于鼠标事件处理程序的新解决方案。 ContextMenu有一个“Popup”事件处理程序。
在弹出窗口中,我根据我的上下文添加了我需要的相关菜单项。
示例:
private MenuItem[] standardMenuItems;
private MenuItem[] selectedMenuItems;
public SomePanel() {
InitializeComponent();
// These are list of menu items (name / callback) that will be
// chosen depending on some context
standardMenuItems = new MenuItem[] { new MenuItem("New", OnNew) };
selectedMenuItems = new MenuItem[] { new MenuItem("Delete", OnDelete), new MenuItem("Edit", OnEdit) };
ContextMenu contextMenu = new ContextMenu();
// begin with "standard items"
contextMenu.MenuItems.AddRange(standardMenuItems);
listview.ContextMenu = contextMenu;
// add the popup handler
contextMenu.Popup += OnMenuPopup;
}
// Called right before the menu comes up
private void OnMenuPopup(object sender, EventArgs e) {
ContextMenu menu = sender as ContextMenu;
if (menu == null)
return;
// If an item was selected, display Delete and Edit options
if (listview.SelectedItems.Count > 0) {
menu.MenuItems.Clear();
menu.MenuItems.AddRange(selectedMenuItems);
}
// Else display only the New option
else {
menu.MenuItems.Clear();
menu.MenuItems.AddRange(standardMenuItems);
}
}
我在C#和Winforms中不够流利,以确保此技术没有任何缺点,但它是依赖鼠标事件的替代方法(如果上下文菜单出现在其他键盘或鼠标事件上,该怎么办? )
答案 5 :(得分:0)
private void contextMenuStripExport_Opening(object sender, System.ComponentModel.CancelEventArgs e)
{
if (exportview.SelectedItems.Count <= 0)
{
uploadToolStripMenuItem.Visible = false;
exportToolStripMenuItem.Visible = false;
}
else
{
uploadToolStripMenuItem.Visible = true;
exportToolStripMenuItem.Visible = true;
}
}
答案 6 :(得分:0)
完全解决方案
代码:
<ImageView
android:id="@+id/imageView1"
android:layout_width="304dp"
android:layout_height="367dp"
android:layout_marginLeft="25dp"
android:layout_marginTop="25dp"
app:srcCompat="@android:drawable/ic_lock_power_off" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/imageView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:text="Change "
android:textSize="24sp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:text="CHANGE" />
</RelativeLayout>
答案 7 :(得分:-1)
使用 DevExpress 20.2 核心... Winforms。这类似于处理 GridView 中的菜单项。
Private WithEvents _menuViewLabelitem As MenuItem
Private Sub lvShipTracking_MouseClick(sender As Object, e As MouseEventArgs) Handles lvShipTracking.MouseClick
If e.Button = MouseButtons.Right Then
If lvShipTracking.FocusedItem IsNot Nothing AndAlso lvShipTracking.FocusedItem.Bounds.Contains(e.Location) = True Then
Dim m As ContextMenu = New ContextMenu()
_menuViewLabelitem = New MenuItem("View Label")
AddHandler Click, AddressOf Handle_ViewLabel
m.MenuItems.Add(_menuViewLabelitem)
m.Show(lvShipTracking, New Point(e.X, e.Y))
End If
End If
End Sub
Private Sub Handle_ViewLabel(sender As Object, e As EventArgs) Handles _menuViewLabelitem.Click
MsgBox("it worked!")
End Sub