Opening active item in a listview with notepad.exe

1.2k Views Asked by At

I was wondering if anybody knew how to open the active (highlighted) item in notepad using a button

I've got this right now (laugh at me.)

Process.Start("notepad.exe", listView1.ItemActivate);

Obviously this doesn't work, does anybody know what to do :x

http://pastie.org/3241590 source for people to lol @

3

There are 3 best solutions below

3
On

ItemActivate is actually an event. You will need to handle that event and place the Process.Start code in there.

Something like:

private void listView1_ItemActivate(Object sender, EventArgs e)
{
    // You'll want to use index 0 for the first item (or only item) selected.
    //
    // You'll need to dig down into the SelectedItem to get the string for
    // the file to launch.
    //
    Process.Start("notepad.exe", listView.SelectedItem(0), ...);
}
1
On

set tag of ListViewItem as full of file

 FItems.Tag = fileFullPath;

then you can open file using tag

 Process.Start("notepad.exe", listView1.SelectedItems[0].Tag.ToString());

in your code update as below

 private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                TreeNode current = e.Node;
                string path = current.FullPath;
                string[] Files = Directory.GetFiles(path);
                string[] Directories = Directory.GetDirectories(path);
                string[] subinfo = new string[3];
                listView1.Clear();
                listView1.Columns.Add("Name", 255);
                listView1.Columns.Add("Size", 100);
                listView1.Columns.Add("Type", 80);
                foreach (string Dname in Directories)
                {
                    subinfo[0] = GetFileName(Dname);
                    subinfo[1] = "";
                    subinfo[2] = "FOLDER";
                    ListViewItem DItems = new ListViewItem(subinfo);
                    listView1.Items.Add(DItems);
                }
                foreach (string Fname in Files)
                {
                    subinfo[0] = GetFileName(Fname);
                    subinfo[1] = GetSizeinfo(Fname);
                    subinfo[2] = GetTypeinfo(Fname);
                    ListViewItem FItems = new ListViewItem(subinfo);
                    FItems.Tag = Fname; // set the tag here
                    listView1.Items.Add(FItems);

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error!!");
            }

        }

and Click event as below

   private void button9_Click(object sender, EventArgs e)
    {
        Process.Start("notepad.exe", listView1.SelectedItems[0].Tag.ToString()); 
    }
1
On

Try this:

Code to open notepad with the content in textbox

Clipboard.SetDataObject(textBox1.Text, true);
Process.Start("notepad"); 
System.Threading.Thread.Sleep(500); 
SendKeys.SendWait("^v");