Display context menu strip only when mouse is over the item and right clicked on it, the code I have for that is this:
private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
if (listView1.SelectedItems.Count == 0)
e.Cancel = true;
}
private void contextMenuStrip1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
Point pt = listView1.PointToScreen(e.Location);
contextMenuStrip1.Show(pt);
}
}
The problem about this code is longest the item is selected it will display the menu strip, what I don't want is the context menu strip shouldn't show up unless directly right clicked on the item.
_____________________________________________________________________________________________________________________________
I have 3 options on the context menu strip, open, open file location and properties. If user right clicks the item and says open, its should ask the user what do you want to open it with using Windows explorer! For the open file location, I came up with this:
Process.Start("explorer.exe", "I need filepath from listview item");
That does open the custom location, however since there is going to be lots of files in the list view, I need the program to get the file location it self.
The implementation I have for the scanning files and displaying them on list view is:
private void button1_Click(object sender, EventArgs e)
{
listView1.Items.Clear();
string[] directories = new string[] {
@"C:\Windows",
@"C:\Users\Emre\AppData\Local\CrashDumps"
};
string[] extensions = new string[] { "*" };
foreach (var dir in directories)
{
var dirInfo = new DirectoryInfo(dir);
foreach (var ext in extensions)
{
foreach (var fileInfo in dirInfo.GetFiles(ext))
{
listView1.Items.Add(new ListViewItem(new string[] { fileInfo.Name, Path.GetDirectoryName(fileInfo.FullName) + @"\", SizeUnit.FileSizeToString(fileInfo.Length) }));
}
}
}
}
Hope that can help you a bit, if you need to know more ask, I also have a code to calculate the size of the file, i didn't insert is because I don't think you will need it really. If anything is not clear plz do ask and thank you very much all helps are appreciated.
Dorababu MekaPosted Mar 19, 2012, 9:22 AM
Emre OzpalamutcuPosted Mar 19, 2012, 9:51 AM
Emre OzpalamutcuPosted Mar 19, 2012, 9:43 AM
After that I have a question about, file access settings.
Emre OzpalamutcuPosted Mar 19, 2012, 9:37 AM
Dorababu MekaPosted Mar 19, 2012, 9:36 AM
Emre OzpalamutcuPosted Mar 19, 2012, 9:35 AM
Thanks again.
Dorababu MekaPosted Mar 19, 2012, 9:32 AM
[DllImport("shell32.dll", CharSet = CharSet.Auto)]
static extern bool ShellExecuteEx(ref SHELLEXECUTEINFO lpExecInfo);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct SHELLEXECUTEINFO
{
public int cbSize;
public uint fMask;
public IntPtr hwnd;
[MarshalAs(UnmanagedType.LPTStr)]
public string lpVerb;
[MarshalAs(UnmanagedType.LPTStr)]
public string lpFile;
[MarshalAs(UnmanagedType.LPTStr)]
public string lpParameters;
[MarshalAs(UnmanagedType.LPTStr)]
public string lpDirectory;
public int nShow;
public IntPtr hInstApp;
public IntPtr lpIDList;
[MarshalAs(UnmanagedType.LPTStr)]
public string lpClass;
public IntPtr hkeyClass;
public uint dwHotKey;
public IntPtr hIcon;
public IntPtr hProcess;
}
private const int SW_SHOW = 5;
private const uint SEE_MASK_INVOKEIDLIST = 12;
private void tool3ToolStripMenuItem_Click(object sender, EventArgs e)
{
ListViewItem LVI = null;
}
Emre OzpalamutcuPosted Mar 19, 2012, 9:26 AM
You know I said I have two problems, the second one is how do I show windows file properties. When the user right click the properties button the context menu strip, the windows file properties associated with that file should show up.
Dorababu MekaPosted Mar 19, 2012, 9:19 AM
this.ContextMenuStrip = contextMenuStrip1;
Emre OzpalamutcuPosted Mar 19, 2012, 9:12 AM
Dorababu MekaPosted Mar 19, 2012, 9:10 AM
Dorababu MekaPosted Mar 19, 2012, 9:07 AM
Emre OzpalamutcuPosted Mar 19, 2012, 8:58 AM
Dorababu MekaPosted Mar 19, 2012, 8:52 AM
Dorababu MekaPosted Mar 19, 2012, 8:47 AM
Emre OzpalamutcuPosted Mar 19, 2012, 8:16 AM
Dorababu MekaPosted Mar 19, 2012, 8:15 AM
Emre OzpalamutcuPosted Mar 19, 2012, 8:11 AM
Dorababu MekaPosted Mar 19, 2012, 7:57 AM
Emre OzpalamutcuPosted Mar 19, 2012, 7:54 AM
Dorababu MekaPosted Mar 19, 2012, 7:48 AM
Dorababu MekaPosted Mar 19, 2012, 7:44 AM
Emre OzpalamutcuPosted Mar 19, 2012, 7:24 AM
Dorababu MekaPosted Mar 19, 2012, 7:14 AM
Emre OzpalamutcuPosted Mar 19, 2012, 6:46 AM
Dorababu MekaPosted Mar 19, 2012, 6:37 AM
if (e.Button == MouseButtons.Right)
{
contextMenuStrip1.Show(listView1, e.Location);
}
Instead of contextMenuStrip1_MouseClick handle it in Mouse Down
Emre OzpalamutcuPosted Mar 19, 2012, 5:44 AM
Dorababu MekaPosted Mar 19, 2012, 4:45 AM
Can you clearly mention with a brief description what you are trying to achieve on Open and Open File Location..
If not make some images as per your requirement for better understanding..