基本上我有contextmenustrip和listview,我添加功能到上下文menustrip像删除更新等但我不希望这个菜单打开没有点击并选择列表视图中的任何项目我该怎么办?
#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
{
}
}
这是列表视图代码我不确定这是否可以帮助您,但您可能想要查看
答案 0 :(得分:0)
您可以订阅ContextMenuStrip的开启事件,如果您的列表视图中没有选择,请将e.Cancel设置为true,这将阻止上下文菜单打开。
请查看https://msdn.microsoft.com/de-de/library/ms229721(v=vs.110).aspx了解更多详情!
您有一个ContextMenuStrip cms,您可以在Windows窗体设计器或代码中添加事件处理程序
cms.Opening += new System.ComponentModel.CancelEventHandler(this.cms_Opening);
在您的事件处理程序中,您可以检查是否在列表视图中选择了一个项目,以确定您是否希望打开或关闭上下文菜单。
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;
}
因此,如果您在listView1中选择了一个项目,那么您的上下文菜单将被打开,否则将无法显示。