How to stop a form from being automatically activated on the creation

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi

There is a main form. On some user request it creates another modeless form. By the default behavior that new form becomes active. I do not want that. So, I set focus back to main form, executing mainForm.Activate. But it makes forms to flicker a little - and it looks bad
Are there a way around? To stop a new form from being automatically activated on the creating

Thank you in advance
Mikhail Golovnykh
 
Hi Mikhail,

Thanks for posting in the community!

From my understanding now, you are trying to show a form without focusing
off the mainform.

Based on my knowledge, there is no official way to change this default
behavior, however you may try this workaround by P/invoking the win32api
ShowWindow and pass in SW_SHOWNOACTIVE enum as nCmdShow, here is a little
snippet to demonstrate this workaround.
<code>
class NoActivateForm : Form
{
public void ShowNoActivate()
{
ShowWindow(Handle,4);//SW_SHOWNOACTIVATE
}

[DllImport("user32.dll")]
private extern static int ShowWindow(IntPtr hWnd,int nCmdShow);
}
</code>
Call ShowNoActivate instead Show in your code.
Does this way solve your problem?
If you still have problem on this issue, please feel free to reply this
thread.
Thanks!


Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, "online" should be removed before
sending.
 
Hi, Ying-Shen,

Thank you for your reply.
But this workaround did not help. A new created form gets
activated anyway.

So, I modified the form - i made it as simple as
possible. It now basically contains nothing except for
ShowWindow(4) call. But it continues to get activated...

Do you have any other suggestions?

The only possible solve of the problem I now see is to
create a COM dll with a function like ShowTheForm(),
which will use WinAPI to create and show the form. But
this looks ugly...

Thank you,
Mikhail Golovnykh
 
Hi Mikhail,

Thanks for your reply!

My test code works properly on my system (a WinXP box), here is the
complete test code. You may try my test code on your system, On my system
the new form will not get activated (although it is show in front of the
mainform), is it the effect you desired?
If it works, you may compare my test code with yours to see if there is any
difference, If you still have problem or it doesn't work for you, please
send me a simple sample based on your test code, I'd like to do some
research on it. Please remove word "online" before send the sample to my
e-mail box.

Thanks!
<code>
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.ComponentModel;

namespace project1
{
public class test
{
[STAThread]
public static void Main(String[] args)
{
Form f = new MyForm();
Application.Run(f);
}
}
class MyForm : Form
{
private Button btn = new Button();
public MyForm()
{
btn = new Button();
btn.Text = "New Form";
btn.Click += new EventHandler(btn_Click);
Controls.Add(btn);
}

private void btn_Click(object sender, EventArgs e)
{
NoActivateForm f = new NoActivateForm();
f.Show();
}

}
class NoActivateForm : Form
{
Control t;
public NoActivateForm()
{
t = new TreeView();
Controls.Add(t);
t.Show();
}
public void ShowNoActivate()
{
ShowWindow(Handle,4);//SW_SHOWNOACTIVATE
}
protected override void SetVisibleCore(bool value)
{
if (value)
{
ShowWindow(Handle,4);//SW_SHOWNOACTIVATE
}
else base.SetVisibleCore (value);
}

[DllImport("user32.dll")]
private extern static int ShowWindow(IntPtr hWnd,int nCmdShow);
}
}
</code>


Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, "online" should be removed before
sending.
 
Back
Top