【问题标题】:Click on treeview node open a new MDI form, focus left on first form单击树视图节点打开一个新的 MDI 表单,将注意力集中在第一个表单上
【发布时间】:2012-09-10 22:39:55
【问题描述】:

单击树视图中的节点后,我正在尝试打开一个新表单。

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

我注意到第一个表单的 _Enter 事件正在触发,好像某些东西正在将焦点设置回第一个表单。

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

这是打开表单的代码

    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();
        }
    }

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

        // 
        // 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);

感谢您的帮助 拉斯

【问题讨论】:

    标签: winforms treeview mdichild


    【解决方案1】:

    是的,TreeView 有点痛苦,它会将焦点恢复到自身。这就是为什么它有 AfterXxx 事件,但没有 AfterNodeMouseClick 事件。解决方法是延迟执行该方法,直到所有事件副作用完成。这是通过使用 Control.BeginInvoke() 方法优雅地完成的,它的委托目标在 UI 线程再次空闲时运行。像这样:

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

    【讨论】:

    • 谢谢汉斯,我知道它必须是这样的。感谢您的确认和解决方法。
    • 汉斯,如果可以的话,我会加倍你的正确答案。我终于有时间尝试了。非常光滑,没有问题并且每次都有效。再次感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-08
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 2013-02-04
    • 2018-03-02
    相关资源
    最近更新 更多