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 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)
{
}
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]Tags only - Saurabh[/edit]