Hii everyone
I try this code. The menu itself is working but it's showing no matter where I right-click in the ListView. how can i hide the contextmenu when the row is empty?
Thanks
private void listViewCalendar_MouseDown(object sender, MouseEventArgs e){
Point p = new Point(e.X, e.Y);
if (e.Button == MouseButtons.Right )
{
ListViewItem lvi = this.listViewCalendar.GetItemAt(p.X, p.Y);
if (lvi != null && listViewCalendar.SelectedItems.Count ==1)
{
this.myContext.Show(this.listViewCalendar, p);
}
}
}
maliPosted Oct 10, 2008, 8:55 AM
Hi Siddhartha
I'm sorry, I dont understand you.
i'm dealing with row not column so perhaps this is the problem?
anyway, whene i'm clicking, its only on the listview.
Siddhartha SarkarPosted Oct 10, 2008, 8:27 AM
Hi Mali,
The code is fine. the problem that you are having is when you richt click on an item outside the realms of its item rectangle. I mean the clickis happenning on a portion where there is no colum defined for your item, though the item may be there.
Am I right??
maliPosted Oct 10, 2008, 7:32 AM
hi
thank you for repaly.
about the code... it dosn't work. at the begining its give me false-enabled on an empty row , but after a few clicking its give me false-enabled even on a row wite data.
mali
Siddhartha SarkarPosted Oct 10, 2008, 4:57 AM
Hi Mali,
Do you wish that the contextMenu be disabled when the clicked item is empty or the list view is empty?
How about modifying the code like this:
In the Form_Load event write this:
listViewCalendar.ContextMenuStrip = myContext;
private
void listViewCalendar_MouseDown(object sender, MouseEventArgs e){
myContext.Enabled = true;
Point p = new Point(e.X, e.Y);
if (e.Button == MouseButtons.Right)
{
ListViewItem lvi = this.listViewCalendar.GetItemAt(p.X, p.Y);
if (lvi == null || listViewCalendar.SelectedItems.Count == 1) //I assume the second condition given here is redundant. Therefore "if (lvi== null)" will do as well if you set the MultiSelect property of the ListView to false. Less the number of conditions, better is your code.
{
this.myContext.Enabled = false;
}
}
}
Do care to let know the outcome as I haven't tested the code before posting.
Happy coding!!
Regards,