Cenk Arslan
Cenk Arslan

Reputation: 21

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 ?

#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

Upvotes: 0

Views: 1008

Answers (1)

SomeCoder
SomeCoder

Reputation: 126

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.

Look at https://msdn.microsoft.com/de-de/library/ms229721(v=vs.110).aspx for more details!

You have a ContextMenuStrip cms where you add a eventhandler either in Windows forms designer or in Code

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.

Upvotes: 1

Related Questions