C#无模式应用 [英] C# Modeless Application

查看:53
本文介绍了C#无模式应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我有一个无模式的应用程序,其中有一个表单的树状视图.单击树节点后,我将打开一个非模态表单.问题在于,该表格会弹出,但是直到我手动单击该表格后,该表格才获得焦点.我已经尝试过Focus(),Activate().我使用Show(Parent)方法使对话框变为非模态.
请帮忙.
问候苏拉卜.

请大家发表评论

这是代码演示片段,当我单击按钮时,表单将获得焦点并被激活.但是,当我单击树视图的节点时,打开的表单没有获得焦点.

静态类

Hi guys ,
I have a modeless application where i have a tree view in a form . On clicking on the tree node i make a non - modal form to open up. The problem is that ,the form pops-up but this form doesnt get the focus untill i manually click on the form . I have already tried the Focus(), Activate(). I use Show(Parent) method tho make the dialog non-modal.
Please help.
regards Saurabh.

Please Guys comment

Here is the code demo fragment now when i click on the button the form gets focus and is activated just fine . But when i click on the node of the tree view no focus is obtained on the form that opens up.

Static Class

public static class Class1
    {
       public static void InvokeDialog(Form1 f1)
       {
           Form2 f = new Form2();
           f.Show(f1);
           f.Activate();
           f.Focus();
       }
    }





Form1代码





Form1 Code

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            Class1.InvokeDialog(this);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Class1.InvokeDialog(this);
        }
        private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
           // Class1.InvokeDialog(this);
        }




Form1Designer
< pre lang ="cs">局部类Form1
{

私有System.ComponentModel.IContainer组件= null;

受保护的重写void Dispose(布尔处置)
{
if(处理&&(组件!= null))
{
components.Dispose();
}
base.Dispose(处置);
}

#region Windows窗体设计器生成的代码

///< summary>
///支持Designer的必需方法-请勿修改
///使用代码编辑器将此方法的内容.
///</summary>
私有void InitializeComponent()
{
System.Windows.Forms.TreeNode treeNode1 =新的System.Windows.Forms.TreeNode("Tree1");
System.Windows.Forms.TreeNode treeNode2 =新的System.Windows.Forms.TreeNode("Tree2");
System.Windows.Forms.TreeNode treeNode3 =新的System.Windows.Forms.TreeNode("Tree3");
this.splitContainer1 =新的System.Windows.Forms.SplitContainer();
this.treeView1 =新的System.Windows.Forms.TreeView();
this.button1 =新的System.Windows.Forms.Button();
this.splitContainer1.Panel1.SuspendLayout();
this.splitContainer1.SuspendLayout();
this.SuspendLayout();
//
//splitContainer1
//
this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
this.splitContainer1.Location =新的System.Drawing.Point(0,0);
this.splitContainer1.Name ="splitContainer1";
//
//splitContainer1.Panel1
//
this.splitContainer1.Panel1.Controls.Add(this.button1);
this.splitContainer1.Panel1.Controls.Add(this.treeView1);
this.splitContainer1.Size =新的System.Drawing.Size(292,273);
this.splitContainer1.SplitterDistance = 114;
this.splitContainer1.TabIndex = 0;
//
//treeView1
//
this.treeView1.Location =新的System.Drawing.Point(0,0);
this.treeView1.Name ="treeView1";
treeNode1.Name ="Node1";
treeNode1.Text ="Tree1";
treeNode2.Name ="Node2";
treeNode2.Text ="Tree2";
treeNode3.Name ="Node3";
treeNode3.Text ="Tree3";
this.treeView1.Nodes.AddRange(new System.Windows.Forms.TreeNode [] {
treeNode1,
treeNode2,
treeNode3});
this.treeView1.Size = new System.Drawing.Size(114,163);
this.treeView1.TabIndex = 0;
this.treeView1.NodeMouseDoubleClick + =新的System.Windows.Forms.TreeNodeMouseClickEventHandler(this.treeView1_NodeMouseDoubleClick);
this.treeView1.NodeMouseClick + =新的System.Windows.Forms.TreeNodeMouseClickEventHandler(this.treeView1_NodeMouseClick);
//
//button1
//
this.button1.Location =新的System.Drawing.Point(12,189);
this.button1.Name ="button1";
this.button1.Size =新的System.Drawing.Size(75,23);
this.button1.TabIndex = 1;
this.button1.Text ="button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click + =新的System.EventHandler(this.button1_Click);
//
//Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F,13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292,273);
this.Controls.Add(this.splitContainer1);
this.Name ="Form1";
this.Text ="Form1";
this.splitContainer1.Panel1.ResumeLayout(false);
this.splitContainer1.ResumeLayout(false);
this.ResumeLayout(false);

}

#endregion

私有System.Windows.Forms.SplitContainer splitContainer1;
私有System.Windows.Forms.TreeView treeView1;
私人System.Windows.Forms.Button button1;

}
</pre>
Form2 Designer




Form1Designer
<pre lang="cs">partial class Form1
{

private System.ComponentModel.IContainer components = null;

protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.Windows.Forms.TreeNode treeNode1 = new System.Windows.Forms.TreeNode("Tree1");
System.Windows.Forms.TreeNode treeNode2 = new System.Windows.Forms.TreeNode("Tree2");
System.Windows.Forms.TreeNode treeNode3 = new System.Windows.Forms.TreeNode("Tree3");
this.splitContainer1 = new System.Windows.Forms.SplitContainer();
this.treeView1 = new System.Windows.Forms.TreeView();
this.button1 = new System.Windows.Forms.Button();
this.splitContainer1.Panel1.SuspendLayout();
this.splitContainer1.SuspendLayout();
this.SuspendLayout();
//
// splitContainer1
//
this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
this.splitContainer1.Location = new System.Drawing.Point(0, 0);
this.splitContainer1.Name = "splitContainer1";
//
// splitContainer1.Panel1
//
this.splitContainer1.Panel1.Controls.Add(this.button1);
this.splitContainer1.Panel1.Controls.Add(this.treeView1);
this.splitContainer1.Size = new System.Drawing.Size(292, 273);
this.splitContainer1.SplitterDistance = 114;
this.splitContainer1.TabIndex = 0;
//
// treeView1
//
this.treeView1.Location = new System.Drawing.Point(0, 0);
this.treeView1.Name = "treeView1";
treeNode1.Name = "Node1";
treeNode1.Text = "Tree1";
treeNode2.Name = "Node2";
treeNode2.Text = "Tree2";
treeNode3.Name = "Node3";
treeNode3.Text = "Tree3";
this.treeView1.Nodes.AddRange(new System.Windows.Forms.TreeNode[] {
treeNode1,
treeNode2,
treeNode3});
this.treeView1.Size = new System.Drawing.Size(114, 163);
this.treeView1.TabIndex = 0;
this.treeView1.NodeMouseDoubleClick += new System.Windows.Forms.TreeNodeMouseClickEventHandler(this.treeView1_NodeMouseDoubleClick);
this.treeView1.NodeMouseClick += new System.Windows.Forms.TreeNodeMouseClickEventHandler(this.treeView1_NodeMouseClick);
//
// button1
//
this.button1.Location = new System.Drawing.Point(12, 189);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.splitContainer1);
this.Name = "Form1";
this.Text = "Form1";
this.splitContainer1.Panel1.ResumeLayout(false);
this.splitContainer1.ResumeLayout(false);
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.SplitContainer splitContainer1;
private System.Windows.Forms.TreeView treeView1;
private System.Windows.Forms.Button button1;

}
</pre>
Form2 designer

partial class Form2
  {
      /// <summary>
      /// Required designer variable.
      /// </summary>
      private System.ComponentModel.IContainer components = null;
      /// <summary>
      /// Clean up any resources being used.
      /// </summary>
      /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
      protected override void Dispose(bool disposing)
      {
          if (disposing && (components != null))
          {
              components.Dispose();
          }
          base.Dispose(disposing);
      }
      #region Windows Form Designer generated code
      /// <summary>
      /// Required method for Designer support - do not modify
      /// the contents of this method with the code editor.
      /// </summary>
      private void InitializeComponent()
      {
          this.components = new System.ComponentModel.Container();
          this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
          this.Text = "Form2";
      }
      #endregion
  }



[edit]仅标记-Saurabh [/edit]



[edit]Tags only - Saurabh[/edit]

推荐答案

Activate方法肯定是正确的,当我将其编码为按下按钮.
您究竟在做什么来显示表单并为其赋予焦点?请发布相关的代码片段.
The Activate method is certainly the correct one, and it works for me when I code it into a button press.
What exactly are you doing to show the form and give it the focus? Post a relevant code fragment, please.


这篇关于C#无模式应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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