单击treeview节点,打开一个新的MDI表单,将焦点放在第一个表单上 [英] Click on treeview node open a new MDI form, focus left on first form

查看:69
本文介绍了单击treeview节点,打开一个新的MDI表单,将焦点放在第一个表单上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在树形视图中单击一个节点后,我试图打开一个新表单.

I'm trying to open a new form after clicking a node in a treeview.

在第一个MDI表单中,我有一个树状视图,当我单击树形视图中的一个节点时,将打开第二个MDI表单,但第一个表单将保持焦点.我希望新表格具有重点.

In the first MDI form I have a treeview, when I click on a node in the tree view a 2nd MDI form is opened, but the first form keeps the focus. I want the new form to have the focus.

我注意到第一个表单的_Enter事件正在触发,好像是将焦点重新设置为第一个表单一样.

I have noticed the first form's _Enter event is firing as if something is setting focus back to the first form.

第一个表单上还有一个按钮,它具有相同的功能,并且效果很好.我感觉到树视图具有一些特殊的属性设置,可以使焦点回到第一种形式.

There is also a button on the first form that does the same function and it works great. I have a feeling the treeview has some special attribute set to cause the focus to come back to the first form.

这是打开表格的代码

    private void tvClientsAccounts_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
    {
        OpenClientOrAccount(e.Node);
    }

    private void OpenClientOrAccount(TreeNode Node)
    {
        if (Node.Tag is Client)
        {
            frmClients frmC = new frmClients();
            Client Client = (Client)Node.Tag;
            frmC.Id = Client.Id;
            frmC.HouseholdId = Client.Household.Id;
            frmC.MdiParent = Program.frmMain;
            frmC.Show();
        }
        else if (Node.Tag is Account)
        {
            frmAccounts frmA = new frmAccounts();
            Account Account = (Account)Node.Tag;
            frmA.Id = Account.Id;
            frmA.ClientId = Account.Client.Id;
            frmA.MdiParent = Program.frmMain;
            frmA.Show();
        }
    }

这是定义树视图的设计师代码

Here is the designer code defining the treeview

        // 
        // tvClientsAccounts
        // 
        this.tvClientsAccounts.BackColor = System.Drawing.SystemColors.Control;
        this.tvClientsAccounts.Indent = 15;
        this.tvClientsAccounts.LineColor = System.Drawing.Color.DarkGray;
        this.tvClientsAccounts.Location = new System.Drawing.Point(228, 193);
        this.tvClientsAccounts.Name = "tvClientsAccounts";
        this.tvClientsAccounts.Nodes.AddRange(new System.Windows.Forms.TreeNode[] {
        treeNode9});
        this.tvClientsAccounts.PathSeparator = "";
        this.tvClientsAccounts.ShowNodeToolTips = true;
        this.tvClientsAccounts.Size = new System.Drawing.Size(411, 213);
        this.tvClientsAccounts.TabIndex = 23;
        this.tvClientsAccounts.BeforeExpand += new System.Windows.Forms.TreeViewCancelEventHandler(this.tvClientsAccounts_BeforeExpand);
        this.tvClientsAccounts.AfterExpand += new System.Windows.Forms.TreeViewEventHandler(this.tvClientsAccounts_AfterExpand);
        this.tvClientsAccounts.BeforeSelect += new System.Windows.Forms.TreeViewCancelEventHandler(this.tvClientsAccounts_BeforeSelect);
        this.tvClientsAccounts.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.tvClientsAccounts_AfterSelect);
        this.tvClientsAccounts.NodeMouseClick += new System.Windows.Forms.TreeNodeMouseClickEventHandler(this.tvClientsAccounts_NodeMouseClick);

感谢您的帮助 拉斯

推荐答案

是的,TreeView有点麻烦,它可以将焦点恢复到自身.这就是为什么它具有AfterXxx事件,但是没有AfterNodeMouseClick事件的原因.解决该问题的方法是延迟执行该方法,直到完成所有事件副作用之后.通过使用Control.BeginInvoke()方法可以很好地完成此操作,当UI线程再次变为空闲状态时,其委托目标将运行.像这样:

Yes, TreeView is a bit of a pain that way, it restores the focus to itself. That's why it has the AfterXxx events, but there's no AfterNodeMouseClick event. The way to solve it is to delay executing the method, until after all event side-effects are completed. That's elegantly done by using the Control.BeginInvoke() method, its delegate target runs when the UI thread goes idle again. Like this:

  private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) {
      this.BeginInvoke(new Action(() => OpenClientOrAccount(e.Node)));
  }

这篇关于单击treeview节点,打开一个新的MDI表单,将焦点放在第一个表单上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆