[转]C#怎样保证弹出窗体是唯一并居中显示
原网页:http://www.cnblogs.com/xielong/p/6252067.html
Winform窗体中,假如我从Form1窗体要弹出Form2窗体,写法是这样的:
Form2 f2 = new Form2();
f2.Show();
1、如何使窗体打开时居中显示
//初始化默认窗体居中显示
Form2 f2 = new Form2();
f2.StartPosition = FormStartPosition.CenterScreen;
f2.Show();
2、如何实现弹出子窗口的不关闭时,其它的窗口无法操作
Form2 f2 = new Form2();
f2.StartPosition = FormStartPosition.CenterScreen;
f2.ShowDialog();
3、如何保证打开窗体是唯一的(是通过show()打开)
例子如下:
1、窗体页面
1)Form1页面
2)Form2页面
2、页面源码
1)Form1页面源码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TestWinForm
{
public partial class Form1 : Form
{
//创建全局变量,用来存放已show出的窗体对象
List<Form> allforms = new List<Form>();
public Form1()
{
InitializeComponent();
//初始化默认居中显示
this.StartPosition = FormStartPosition.CenterScreen;
}
/// <summary>
/// 普通方式(通过直接show出来)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnOpen_Click(object sender, EventArgs e)
{
if (this.txtAddMsg.Text == "") {
MessageBox.Show("输入信息文本不能为空");
return;
}
string msg = this.txtAddMsg.Text;
//实例化对象
Form2 f2 = new Form2(this);
//居中显示
f2.StartPosition = FormStartPosition.CenterScreen;
//打开窗体
f2.Show();
}
/// <summary>
/// 特殊方式(通过onlyoneForm()方法处理)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnOpen1_Click(object sender, EventArgs e)
{
if (this.txtAddMsg.Text == "")
{
MessageBox.Show("输入信息文本不能为空");
return;
}
string msg = this.txtAddMsg.Text;
//实例化对象
Form2 f2 = new Form2(this,msg);
//设置居中显示
f2.StartPosition = FormStartPosition.CenterScreen;
//验证并保证窗体唯一
onlyoneForm(f2);
}
#region 保证窗体唯一方法
/// <summary>
/// 显示唯一的窗体
/// </summary>
/// <param name="FF"></param>
private void onlyoneForm(Form FF)
{
//判断窗体是否已经弹出,默认false
bool hasform = false;
//遍历所有窗体对象
foreach (Form f in allforms)
{
//判断弹出的窗体是否重复
if (f.Name == FF.Name)
{
//重复,修改为true
hasform = true;
f.WindowState = FormWindowState.Normal;
//获取焦点
f.Focus();
}
}
if (hasform)
{
FF.Close();
}
else
{
//添加到所有窗体中
allforms.Add(FF);
//并打开该窗体
FF.Show();
}
}
/// <summary>
/// 移除窗体对象
/// </summary>
/// <param name="f"></param>
public void RemoveForm(Form f)
{
if (allforms.Contains(f))
{
allforms.Remove(f);
}
}
#endregion
}
}
2)Form2页面源码
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace TestWinForm{ public partial class Form2 : Form { public Form2() { InitializeComponent(); } //保存传递的窗体对象 Form1 form1 = null; public Form2(Form1 f1) { InitializeComponent(); form1 = f1; } public Form2(Form1 f1, string msg) { InitializeComponent(); form1 = f1; this.txtShowMsg.Text = msg; } /// <summary> /// 移除窗体对象 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Form2_FormClosing(object sender, FormClosingEventArgs e) { form1.RemoveForm(this); } }} |
3、结果视图页面
1)普通方式结果
2)特殊方式结果
参考来源:
http://www.mamicode.com/info-detail-1375089.html
http://www.cnblogs.com/sunshuping/p/5850799.html


浙公网安备 33010602011771号