Click here to Skip to main content
15,886,787 members
Articles / Desktop Programming / Windows Forms

Window force forward

Rate me:
Please Sign up or sign in to vote.
1.93/5 (7 votes)
8 Jun 2006CPOL1 min read 29K   240   12   1
A small example of how a program window can be brought up when the user input may need it.

Sample Image - ForceForward.jpg

Introduction

After all those times that I have used code examples and code snippets from The Code Project, it is time to contribute something back. This article shows how easy it is to use two pre-programmed properties in the Form class in your own project. It shows how easily the this.WindowState and this.TopMost can be used.

The Code

The force forward function is small and simple, it is called with a bool value that forces the window to be on top, or resets it back to normal. If the window has been minimized to the taskbar, the window state is changed to normal so it will pop up from the taskbar.

C#
private void ForceForward(bool forceForward)
{
    if (forceForward)
    {
        // Force the window up from taskbar if needed...
        if (this.WindowState == FormWindowState.Minimized)
            this.WindowState = FormWindowState.Normal;

        // Top most windows...
        this.TopMost = true;
    }

    // If not force forward, set TopMost back
    // to original state...
    else
        this.TopMost = false;
}

The Sequence

This program uses a timer to demonstrate. The delay is set to five seconds as the timer is started. For each time the timer “ticks”, the delay decreases by one. When the delay has reached down to zero (or somehow below), it executes ForceForward(true) and then ends the sequence.

C#
private void btStart_Click(object sender, EventArgs e)
{
    // Start button set to false!
    btStart.Enabled = false;

    // Run once every second (1000ms = 1sec)...
    timer.Enabled = true;
    timer.Interval = 1000;

    // Delay is set to 5 seconds...
    delay = 5;

    // Show on form in begining of countdown...
    lbCountdown.Text = delay.ToString();
    this.Text = delay.ToString();

    // Start the timer...
    timer.Start();
}

private void timer_Tick(object sender, EventArgs e)
{
    // Decrease delay...
    delay--;

    // Show countdown status...
    lbCountdown.Text = delay.ToString();
    this.Text = delay.ToString();

    // When delay has reached zero, ForceForward...
    if (delay <= 0)
    {
        ForceForward(true);
        EndSequence();
    }
}

private void EndSequence()
{
    // Stop and disable...
    timer.Stop();
    timer.Enabled = false;

    // Reset countdown label...
    lbCountdown.Text = "";
    this.Text = "Force Forward";

    // Start button now enabled for next force...
    btStart.Enabled = true;

    // Now set back to normal... 
    ForceForward(false);
}

When the end sequence has been executed, the timer is stopped and the program turns back to normal; it does not “top most” windows any more.

License

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


Written By
Software Developer
Iceland Iceland
It all started with enthusiasm for creating mods for Quake 2 many many years ago, somehow that turned into mediocre tutorials on this website #Winning

Comments and Discussions

 
GeneralNot always on top Pin
--Simas--3-Apr-07 21:18
--Simas--3-Apr-07 21:18 
You can use Focus() - to get window on top

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.