Treeview is used in the window application. net friends may all have this problem, that is, they add the event treeviewmediaafterselect to the Treeview. In this event, open a non-mode form, which will appear behind the form where the Treeview is located, I think everyone knows the reason. I will not say much about it. The question is, how can we make the form display normally in the front of the activated window. I have the following code to achieve this. You can refer to it and tell me what problems you have:
Private delegate void dlgproc (string path); // declares a proxy to execute an asynchronous operation.
Private void treeviewappsafterselect (Object sender, system. Windows. Forms. treevieweventargs E)
{
String Path = NULL; // used to display the title of the opened form
Treenode node = This. treeview1.selectednode;
If (node! = NULL)
{
Path = node. text;
While (node. parent! = NULL)
{
Node = node. parent;
Path = node. Text + "//" + path;
}
Dlgproc dp = new dlgproc (this. thproc); // declare the proxy instance
DP. begininvoke (path, null, null); // start asynchronous operations
}
}
Private void thproc (string path) // method executed during asynchronous calling
{
Dlgproc dp = new dlgproc (opensubform); // declare a proxy to execute the method in the original thread
This. Invoke (DP, new object [] {path}); // use invoke to call the method of opening a form in the original thread.
}
Private void opensubform (string path)
{
Formsub F = new formsub (); // form to be opened
F. Text = path; // pass a value
This. addownedform (f); // you can skip this sentence. here you just want to add a subform.
F. Show (); // The displayed form is normal :)
}
In fact, the principle is very simple, that is, to open the form with an asynchronous method that is irrelevant to the treeviewmediaafterselect statement. The form is naturally displayed normally and is no longer affected by the treeviewmediaafterselect event.