Dumbo
Dumbo

Reputation: 14112

Bringing a form to front when it is minimized in taskbar

Is there a way to bring a form that is already minimized to taksbar to front? I have tried the codes below but no success:

        filterForm.Show();
        filterForm.Activate();
        filterForm.BringToFront();

P.S: This form is called from another form, and user do some stuff in it and then may minimize it. I want only a single instance of this form to be open at a time, so the second time user clicks the button for showing the form I am checking if the form is already shown or not, if shown I want it to be in front:

public FilterForm filterForm;
public bool IsFilterFormActive;

private void tsOpenFilerForm_Click(object sender, EventArgs e)
{
    if (!IsFilterFormActive)
    {
        filterForm = new FilterForm();
        filterForm.FormClosing += delegate {
                                               IsFilterFormActive = false;
                                            };
        IsFilterFormActive = true;
        filterForm.Show();
    }
    else
    {
        filterForm.Show();
        filterForm.Activate();
        filterForm.BringToFront();
    }
}

Upvotes: 1

Views: 10794

Answers (2)

Hans Passant
Hans Passant

Reputation: 941455

You are leaking the form instance, best thing to do is setting it back to null when it closes. You then don't need the bool either. Like this:

    FilterForm filterForm;

    private void tsFilterForm_Click(object sender, EventArgs e) {
        if (filterForm == null) {
            filterForm = new FilterForm();
            filterForm.FormClosed += delegate { filterForm = null; };
            filterForm.Show();
        }
        else {
            filterForm.WindowState = FormWindowState.Normal;
            filterForm.Focus();
        }
    }

Upvotes: 9

InBetween
InBetween

Reputation: 32750

Add filterForm.WindowState = FormWindowState.Normal; before in order to restore the window. If its minimized you first have to bring it up again. Then filterForm.Activate() should be enough.

Upvotes: 6

Related Questions