在任务栏中将表单最小化时将表单置于前面

时间:2012-01-18 17:40:34

标签: c# winforms

有没有办法将已经最小化的表单带到taksbar前面?我尝试过以下代码但没有成功:

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

P.S:从另一种形式调用此表单,用户在其中执行一些操作,然后将其最小化。我希望一次只打开这个表单的一个实例,所以第二次用户单击按钮显示我正在检查表单是否已经显示的表单,如果显示我希望它在前面:

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

2 个答案:

答案 0 :(得分:8)

您正在泄漏表单实例,最好的办法是在关闭时将其设置为null。然后你也不需要bool。像这样:

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

答案 1 :(得分:6)

之前添加filterForm.WindowState = FormWindowState.Normal;以恢复窗口。如果它最小化你首先必须再次提起它。那么filterForm.Activate()就够了。