Click here to Skip to main content
16,004,806 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
Using the below code to Open a document on Button click and also on Tree view node click

C#
Process proc = new Process();
proc.StartInfo = new ProcessStartInfo(fileName.doc);
proc.StartInfo.Verb = "Open";
proc.Start();


On Button click: the Document file is shown in front.(has the focus not the application)

But on tree view node click: the document file opens but the application comes in front of it.

How to make the document file come front when opened on treeview node click

Please help me out...its urgent

Best Regards
Sree

[Comment: DaveyM69] Never say "it's urgent" or similar. It's considered very rude and you won't get an answer any quicker, in fact you will be less likely to get an answer! [/Comment]
Posted
Updated 30-Mar-10 17:05pm
v3

There's actually an easier way than using PInvoke. Just put the code to open the file on a BackgroundWorker like:

C#
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
    myWorker = new BackgroundWorker();
    myWorker.DoWork += myWorker_DoWork;
    myWorker.RunWorkerAsync(e.Node.Text);
}

private BackgroundWorker myWorker;

private void myWorker_DoWork(object sender, DoWorkEventArgs e)
{
    Process proc = new Process();
    proc.StartInfo = new ProcessStartInfo(e.Argument.ToString());
    proc.StartInfo.Verb = "Open";
    proc.Start();
}
 
Share this answer
 
v2
Hi,

What about if you set the ProcessStartInfo.WindowStyle Property to Maximized or Normal? Otherwise you can get hold of the process window handle (Process.MainWindowHandle Property) and make use of the following method SetActiveWindow[^] or SetForegroundWindow[^]to set the active window. You will need to PINVOKE:

C#
[DllImport("user32.dll", SetLastError=true)]
static extern IntPtr SetActiveWindow(IntPtr hWnd);



Kind regards,

[From WW: Actually, upon testing, neither of those works. For some reason, the TreeView returns focus to itself upon completion of the AfterSelect or NodeMouseClick method so any attempt to change the focus from the current thread is overridden by the TreeView]
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900