WinForm 多窗体
多窗体:
一、首先要想到的问题是:
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.Windows.Forms; namespace 多窗体
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
//Form1为登录窗体,Form2为主窗体,
//如何通过Form1的按钮打开Form2,然后让Form1隐藏Form2显示
Form2 f2 = new Form2(this); f2.Show(); this.Hide();//this当前窗体,指的是Form1,然后Hide隐藏 }
}
}
Form2:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; namespace 多窗体
{
public partial class Form2 : Form
{
Form1 F1 = null;
public Form2(Form1 f1)
{
InitializeComponent();
F1 = f1;
} private void Form2_Load(object sender, EventArgs e)
{ } private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
F1.Close();
}
}
}
2、窗体只能打开一个
创建一个全局的泛型集合,为了放置全部打开的窗体
1、在窗体打开之前,判断集合中是否有name一致的窗体,如果有就说明已经打开了,就不要再打开了
问题:当窗体打开了,关闭后,就无法再次打开了
办法:当窗体关闭时,清除Form1中集合中的此窗体对象记录
Form1:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; namespace 多窗体
{
public partial class Form1 : Form
{
//创建一个全局的泛型集合
List<Form> Flist = new List<Form>();
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
bool has = false;
//Form1为登录窗体,Form2为主窗体,
//如何通过Form1的按钮打开Form2,然后让Form1隐藏Form2显示
Form2 f2 = new Form2(this);
foreach (Form F in Flist)//先遍历集合,看里面是否有窗体和Form2窗体一样的内容
{
if (F.Name == f2.Name)//若果有匹配内容
{
has = true;
}
}
if (has)//如果has是true
{ }
else//如果has不是true
{
Flist.Add(f2);
f2.Show();
} }
//问题:点击按钮打开一个窗体,关闭这个窗体后再点击就无法打开窗体了
//办法:在Form2关闭时,把Form1当中的集合清空掉,在Form2中调用Form1中的方法
public void DeleteForm(Form F)
{
Flist.Remove(F);//这里写的是要移除的
}
}
}
Form2:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; namespace 多窗体
{
public partial class Form2 : Form
{
Form1 F1 = null;
public Form2(Form1 f1)
{
InitializeComponent();
F1 = f1;
} private void Form2_Load(object sender, EventArgs e)
{ } private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
//防止报错,判断一下
if (F1 != null)
{
F1.DeleteForm(this);
}
}
}
}
问题:当窗体已经打开,再次点击打开按钮,不会将已打开的窗体重新显示并焦点进入
办法:找到已打开对象,将WindowState属性设置为:
找到已打开窗体的对象,使用Focus方法;
Form1:
private void button1_Click(object sender, EventArgs e)
{
bool has = false;
//Form1为登录窗体,Form2为主窗体,
//如何通过Form1的按钮打开Form2,然后让Form1隐藏Form2显示
Form2 f2 = new Form2(this);
foreach (Form F in Flist)//先遍历集合,看里面是否有窗体和Form2窗体一样的内容
{
if (F.Name == f2.Name)//若果有匹配内容
{
has = true;
F.WindowState = FormWindowState.Normal;//先将WindowState属性设置为:Normal
F.Focus();//焦点进入
}
}
if (has)//如果has是true
{
f2.Close();//把新创建的窗体释放掉 }
else//如果has不是true
{
Flist.Add(f2);
f2.Show();
} }
3、窗体之间的传值和控制
传值:构造函数传值
Form1:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; namespace 窗体之间的传值和控制
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
//判断用户名输入是否正确
if (textBox1.Text == "zhangsan" && textBox2.Text == "")
{
Form2 f2 = new Form2(this,textBox1.Text);
f2.Show();
this.Hide();
}
}
}
}
Form2:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; namespace 窗体之间的传值和控制
{
public partial class Form2 : Form
{
Form1 F1 = null;//全局变量
//构造函数重载一下,调用这个构造函数时,传过来一个Form1对象,和string Uname
public Form2(Form1 f1,string Uname)
{
InitializeComponent(); F1 = f1;
label1.Text = "欢迎回来!" + Uname; }
}
}
控制:第一步,找到窗体对象,
第二步,将窗体对象的控件值更改
注意:要将窗体中的对象访问权限修改
Form1.Designer:
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
public System.Windows.Forms.Button button1;
Form2:
private void button1_Click(object sender, EventArgs e)
{
//把textBox1的值取出来 textBox1.Text;
if (F1 != null)
{
//把Form1.Designer里面button1的private改成public,这样F1就能点出来button1了
F1.button1.Text = textBox1.Text;
}
}
4、提示框类型的窗体
ShowDialog();
作用:当前窗体可进行操作,其他窗体也不能操作
关闭当前窗体,才能操作其他窗体
Form1:
private void button2_Click(object sender, EventArgs e)
{
Form2 f2=new Form2 (this,"");
f2.ShowDialog();
}
WinForm 多窗体的更多相关文章
- Winform子窗体刷新父窗体
调用窗体(父):Form1,被调用窗体(子):Form2方法1: 所有权法//Form1://需要有一个公共的刷新方法public void Refresh_Method(){//...} ...
- Winform跨窗体操作控件(使用委托)
Winform跨窗体操作控件是winform开发中很常见的形式,最常见且简单有效的方式便是使用委托的方式来进行操作,下面我将通过一个小实例来说明如何使用委托跨窗体实现控件操作. 实例介绍:两个窗体,F ...
- WinForm 设置窗体启动位置在活动屏幕右下角
WinForm 设置窗体启动位置在活动屏幕右下角 在多屏幕环境下, 默认使用鼠标所在的屏幕 1. 设置窗体的 StartPosition 为 FormStartPosition.Manual. 2. ...
- WinForm之窗体应用程序
WinForm之窗体应用程序 基本简单数据库操作(增删改查) using System; using System.Collections.Generic; using System.Windows. ...
- WinForm开发,窗体显示和窗体传值相关知识总结
主窗体中代码: public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void b ...
- C# winform中 窗体缩放自适应的方法(不同电脑/不同分辨率)
C# winform中 窗体缩放自适应的方法(不同电脑/不同分辨率) 窗体缩放是一个困扰我多时的问题,为了解决这个问题,我从网上找了很多相关的资料,很多人说用Anchor和Dock属性,但是我试了 ...
- winform圆角窗体实现
winform圆角窗体实现 1.窗体的FormBorderStyle设置成None,不要控制边框 2.TransparencyKey和BackColor颜色设置成相同的,这样,窗体就透明了 3.以此为 ...
- C# WinForm 父窗体 子窗体 传值
C# WinForm 父窗体 子窗体 传值 本次示例效果如下:Form1为父窗体(包含textBox1.button1)Form2为子窗体(包含textBox2.button2) 父窗体给子窗体传值= ...
- c#winform自定义窗体,重绘标题栏,自定义控件学习
c#winform自定义窗体,重绘标题栏 虽然现在都在说winform窗体太丑了,但是我也能尽量让桌面应用程序漂亮那么一点点话不多说,先上图 重绘标题栏先将原生窗体设置成无边框,FormBoderSt ...
- winform基础窗体设置及基础控件
WinForm - 也叫做C/S 客户端 另:B/S是 网页端 客户端应用程序 - 是需要安装在用户电脑上才可以使用的程序 特点: 不需要联网也可以打开使用部分功能,但是现在的情况是许多功能依然需要 ...
随机推荐
- header中Content-Disposition的作用
在servlet3.0中 支持文件上传的注解@MultipartConfig 发现有个例子开头打印的信息中有Content-Disposition,一时好奇,所以了解了一下.顺便学习一下文件上传所需要 ...
- Corn Fields——POJ3254状态压缩Dp
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Description Farmer John has purchased a lush new ...
- Javascript:谈谈JS的全局变量跟局部变量
原文链接:http://blog.csdn.net/zyz511919766/article/details/7276089# 今天公司一个实习小妹子问我两段JS代码的区别: <script t ...
- NSDate和NSDateFormatter 相关应用代码示例
此方法用来计算当前时间与目标时间的先后顺序: -(NSDate *)calculateTimeWithCurrentTime:(NSDate *)currentDate{ //将当前时间转为本地时区 ...
- SuiteScript > Script Queue Monitor (Beta)
Share Note: Installing and Accessing the Script Queue Monitor Script Queue Monitor (Beta) is availab ...
- easyui validatebox 验证类型DEMO
<script> $.extend($.fn.validatebox.defaults.rules, { idcard: {// 验证身份证 validator: function (va ...
- LINUX下查看日志
LINUX的日志都在 /var/log 目录下: 进入此文件查看目录详情: 查看某个日志的命令: 1.cat messages可以查看某个日志文件. 2.要达到实时更新,可以通过tail命令查看 ...
- 开始学习Angular Mobile UI
介绍 Mobile AngularUI 可以让你使用Twitter Booostrap和Angular JS来开发混合移动App和桌面应用程序. 下面是是一些贯穿整个项目的步骤,我强烈的建议你去继续阅 ...
- 浪潮 NF5240M3 ,NP5540M3 服务器安装2008 R2
1,服务器系统的安装会出现错误的地方一般都是在Raid 卡驱动 略过Raid 卡配置, 具体 http://jingyan.baidu.com/article/ca41422fddfd201eae99 ...
- Creating Dialogs
#ifndef DIALOG_H #define DIALOG_H #include <QtWidgets> //#include <QDialog> //#include & ...