简体   繁体   中英

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();

PS: 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();
    }
}

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();
        }
    }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM