Click here to Skip to main content
Click here to Skip to main content
Go to top

Window force forward

, 8 Jun 2006
Rate this:
Please Sign up or sign in to vote.
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.

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.

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)

Share

About the Author

Steini kallinn
Software Developer
Iceland Iceland
It all started with enthusiasm for creating mods for Quake 2 many many years ago. After graduation from the technical college, I moved to Denmark and got a Bs. degree in software engineering from Aalborg University.

Comments and Discussions

 
GeneralNot always on top Pinmember--Simas--3-Apr-07 21:18 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140905.1 | Last Updated 9 Jun 2006
Article Copyright 2006 by Steini kallinn
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid