简体   繁体   English

如果未在列表视图中选择任何项目,如何禁用右键单击菜单

[英]How can i disable right click menu if there is no item selected in listview

Basicly i have contextmenustrip and listview and i add functions to the context menustrip like delete update etc but i dont want this menu open without click and select any item in listview how can i do it ? 基本上我有contextmenustrip和listview,我向上下文menustrip添加了功能,如delete update等,但是我不希望在没有单击的情况下打开此菜单并选择listview中的任何项目,我该怎么办?

#region listview fonksiyonları
listView1.FullRowSelect = true;
        listView1.View = View.Details;
        listView1.Columns.Add("Versiyon No", 133, HorizontalAlignment.Left);
        listView1.Columns.Add("Açıklama", 200, HorizontalAlignment.Left);
        listView1.Columns.Add("Tarih", 154, HorizontalAlignment.Left);
        #endregion
    #region listviewde txt dosyalarını gösterme
        string[] dosyalar = System.IO.Directory.GetFiles(masaustu + "\\Versiyonlar");
string k = "";
int deger = 0;
       foreach (var item in dosyalar)
       {
           ListViewItem lili = new ListViewItem();
deger=item.LastIndexOf("\\");
         k = item.Remove(0,deger);
         k = k.Remove(0, 1);
        lili.Text = k;
         StreamReader oku = new StreamReader(masaustu + "\\" + "Versiyonlar" + "\\" + k);
string OkunanVeri = oku.ReadToEnd();
string[] dizi = OkunanVeri.Split(new string[] { ";", "$" }, StringSplitOptions.RemoveEmptyEntries);
lili.SubItems.Add(dizi[0]);
         lili.SubItems.Add(dizi[1]);
         listView1.Items.Add(lili);
       }          
       }


#endregion
        #region txt içindekileri textboxda göstermek
        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
    liste frm = new liste();
    try
    {
        string a = "";
        a = "";
        a = listView1.SelectedItems[0].SubItems[0].Text;
        StreamReader oku = new StreamReader(masaustu + "\\" + "Versiyonlar" + "\\" + a);
        string OkunanVeri = oku.ReadToEnd();
        string[] dizi = OkunanVeri.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
        foreach (var item in dizi)
        {
            textBox1.Text = OkunanVeri;
        }
        oku.Close();
    }
    catch
    {
    }
}

this is listview codes i am not sure if that can help you but you might want to check it out 这是列表视图代码,我不确定是否可以帮助您,但您可能需要检查一下

You can subscribe the opening event of ContextMenuStrip and if there is no selection in your listview set e.Cancel to true which will prevent the contextmenu from opening. 您可以订阅ContextMenuStrip的打开事件,如果列表视图中没有选择,请将e.Cancel设置为true,这将阻止打开上下文菜单。

Look at https://msdn.microsoft.com/de-de/library/ms229721(v=vs.110).aspx for more details! 请查看https://msdn.microsoft.com/de-de/library/ms229721(v=vs.110).aspx了解更多详细信息!

You have a ContextMenuStrip cms where you add a eventhandler either in Windows forms designer or in Code 您有一个ContextMenuStrip cms,可在Windows窗体设计器或代码中在其中添加事件处理程序

cms.Opening += new System.ComponentModel.CancelEventHandler(this.cms_Opening);

Inside your eventhandler you check if you got an item in your listview selected to determine if you want your contextmenu open or closed. 在事件处理程序内部,您检查是否在列表视图中选择了一个项目,以确定是否要打开或关闭上下文菜单。

void cms_Opening(object sender, System.ComponentModel.CancelEventArgs e)
{
    // This event handler is invoked when the ContextMenuStrip
    // control's Opening event is raised. 

    // Set Cancel to true to prevent the cms to be opened. 
    e.Cancel = listView1.Selected == null;
}

So if you got an selected item in your listView1 your contextmenu will be opened otherwise it won't show. 因此,如果您在listView1中有一个选定的项目,则将打开上下文菜单,否则它将不会显示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何用鼠标右键单击列表视图项目上的上下文菜单? - How can I add context menu on listview item right click with the mouse? 如何在桌面上的右键菜单中添加项目 - How can I add an item to the right click menu on the desktop 如何以编程方式单击ListView项? - How Can I Click a ListView Item Programatically? 如何限制只在选定的列表视图项上单击鼠标右键,或仅在某个项目上而不是在空格上单击鼠标右键? - how to limit right click on selected listview item only or an item rather than empty spaces? 如何在Windows XP的桌面右键菜单中添加项目 - How can i add an item in the desktop right click menu in Windows XP 使用右键单击上下文菜单打开C#winform,但如何显示所选项? - C# winform opens with right-click context menu, but how do I get the selected item to show up? 我如何在C#WPF中在ListView中添加ContextMenu(想在某人确实单击ListView项目时添加“修改”操作) - How i can add ContextMenu in ListView (want to add “Modify” operation when someone do right click on ListView item ) in C# WPF 禁用listview项目单击 - Disable listview item click 右键单击上下文菜单后,获取选定的项目 - Get Item selected after right click on context menu 如何遍历选定的列表视图项 - How can I loop through the selected listview item
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM