[英]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.