C#TreeNode控件,如何在单击节点时运行程序?

时间:2010-01-29 12:04:52

标签: c# treenode

我正在为我们公司创建一个应用程序启动器,我想使用TreeNode控件(我们有100个需要结构的网络应用程序),当用户点击一个Node(例如:应用程序1)然后我会喜欢自己运行程序,即应用程序启动器不等待它关闭等。

我该怎么做?我目前所拥有的只是AD中的TreeNode结构,除了它之外没有任何代码:

private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{


}

非常感谢

3 个答案:

答案 0 :(得分:4)

您可以使用静态处理方法Start()

private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
    // Starts Internet Explorer
    Process.Start("iexplore.exe");

    // Starts the application with the same name as the TreeNode clicked
    Process.Start(e.Node.Text);
}

如果您也希望传递参数,请查看使用ProcessStartInfo类。

您将获得的唯一延迟是等待进程开始。在程序运行时,您的代码不会被阻止。

答案 1 :(得分:4)

  1. 我建议至少需要双击或Enter按键来启动应用,而不仅仅是选择。否则,当用户单击以获得焦点或使用箭头键导航树时会发生什么?混乱。

  2. 您可以在TreeViewEventArgs中找到受影响的节点:e.Node

  3. Ian已经指出了如何启动流程。

答案 2 :(得分:1)

使用ProcessStartInfo可以让您更好地控制应用

创建TreeView节点时,在每个TreeNode.Tag属性中放置应用程序的完整路径并检索它以运行您的进程

using System.Diagnostics;

private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
    //Retrieving the node data
    TreeNode myClickedNode = (TreeNode)sender;

    //The pointer to your new app
    ProcessStartInfo myAppProcessInfo = new ProcessStartInfo(myClickedNode.Tag);

    //You can set how the window of the new app will start
    myAppProcessInfo.WindowStyle = ProcessWindowStyle.Maximized;

    //Start your new app
    Process myAppProcess = Process.Start(myAppProcessInfo);

    //Using this will put your TreeNode app to sleep, something like System.Threading.Thread.Sleep(int miliseconds) but without the need of telling the app how much it will wait.
    myAppProcess.WaitForExit();
}

对于所有属性,请查看MSDN ProcessStartInfo Class和MSDN Process Class

相关问题