Click here to Skip to main content
15,900,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
public static class Class1
    {
       public static void InvokeDialog(Form1 f1)
       {
           Form2 f = new Form2();
           f.Show(f1);
           f.Activate();
           f.Focus();
       }
    }




Form1 Code
C#
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">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
XML
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]
Posted
Updated 12-Jul-11 19:38pm
v5
Comments
Sergey Alexandrovich Kryukov 12-Jul-11 13:05pm    
C++/CLI or C#?!! Certainly not C++. Fix the title or tags or both.
--SA
Sergey Alexandrovich Kryukov 12-Jul-11 13:07pm    
Do you use MDI? Don't, stop suffering from no-good concept.
--SA
Saurabh.abhyankar 13-Jul-11 0:11am    
Can u Please elaborate ?? yes it is a C++/CLI code to invoke a C# code .

1 solution

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.
 
Share this answer
 
Comments
Saurabh.abhyankar 12-Jul-11 8:45am    
int WMain::MyDetails( ) {
// zpb_begin cmdMyDetails

#pragma push_macro("new")
#undef new
wMyDetailsInputForm *pMyDetailsInputForm = new wMyDetailsInputForm();
#pragma pop_macro("new")



if(wMDIFormHelper::MDIMode == false)
{
pMyDetailsInputForm->ShowDialog(WForm::MainWindowHandle);
}
else
{
wMDIFormHelper::SetDialogInParent();
pMyDetailsInputForm->Show(WForm::MainWindowHandle);

}

// zpb_end
return 0;
}

public static void SetDialogInParent()
{
if (msWForm != null)
{

int WIDTH = msMDIDialogParent.Bounds.Width;
int HEIGHT = msMDIDialogParent.Bounds.Height;

msWForm.SetBounds(0, 0, WIDTH, HEIGHT);
}

}
public static void SetDialog(int x, int y)
{
if (msWForm != null)
{
System.Drawing.Rectangle rec = msWForm.Bounds;
rec.X = x;
rec.Y = y+20;
msWForm.Bounds = rec;

}

}

Have posted the frag ment . i know its difficult to understand still do try to respond. the ShowdialogInParent Method is used to get the size of the parent and to resize the dialog to that size and the setdialog method is used to set the dialog at a given top left co-ordinate
Saurabh.abhyankar 12-Jul-11 8:47am    
I use the pMyDetailsInputForm->Show(WForm::MainWindowHandle); to display the dialog non-modally . where MainWindowHandle is the parent
OriginalGriff 12-Jul-11 8:54am    
That is C++ not C# - I'll change the tags for you - but try adding the Activate immediately after the Show:
pMyDetailsInputForm->Show(WForm::MainWindowHandle);
pMyDetailsInputForm->Activate();

In theory, that should do it.
Saurabh.abhyankar 12-Jul-11 9:00am    
Hi thanks, actually the form is designed in C# so placed the question here. Also i tried placing the Activate method just after the show but nothing happens. The form opens but the focus is not shifted to the newly opened form.
Saurabh.abhyankar 12-Jul-11 9:02am    
I use the c++ code just to call the forms designed in C#.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900