Skip to content

Form that reactivates itself

James Kiefer

Asked May 24, 2009, 8:03 PM EDT4 answers
Share
LinkedInX
I'm trying to create a form that will reactivate itself when deactivated. So far I have tried these methods:


private void Form1_Deactivate(object sender, EventArgs e)
{
SetForegroundWindow(this.Handle.ToInt32());
Activate();
BringToFront();
textBox1.Focus();
}

private void Form1_Leave(object sender, EventArgs e)
{
SetForegroundWindow(this.Handle.ToInt32());
Activate();
BringToFront();
textBox1.Focus();
}



So far none of this has worked. Suggestions? Also, what code tag should I use? I had to break all of that myself.

4 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    try to add int the program.cs the following: change: [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } to: [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); while(true) { Application.Run(new Form1()); } }
    0
  • 0
    No luck. Now it just opens itself every time it closes, not every time it loses focus.
    0
  • 0
    ah you need to set it on top. try to create a other thread, with system.threading or with background worker. then use a while true loop to set the focus on Form1 i dont know if it works 100% if you are with crossthreading, then i would reccomend useging background worker then add a update event and in there set the focus back to form1. then make a do work like this: dowork funtion: while (true){ worker.Update(); thread.sleep(1000);//you dont want this looping every ms } i hope that will help ;)
    0
  • 0
    That would work fine, accept none of the code I have that is supposed to reactivate the form works.

    I have tried:
    Focus()
    BringtoFront()
    SetForegroundWindow(this.Handle.ToInt32())
    and Activate();
    0