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是 网页端 客户端应用程序 - 是需要安装在用户电脑上才可以使用的程序 特点: 不需要联网也可以打开使用部分功能,但是现在的情况是许多功能依然需要 ...
随机推荐
- svn: 期望文件系统格式在“1”到“4”之间;发现格式“6”
svn 客户端浏览的时候出现了这个提示,经查,发现是Subversion 1.7 < TortoiseSVN 1.8导致的,需要更换版本,应该是 Version(Subversion) > ...
- 小结一下前段时间做的rpgdemo
虽然说已经是彻底放弃继续做那个demo了(代码结构混乱,想增加新功能非常的不方便),不过还是花了一点心血在里面的,毕竟这是我开始学习unity游戏制作的初衷,不过果然是学的越多越发现自己的不足... ...
- Python基础之条件和循环
阅读目录 一.if语句 1.1功能 1.2语法 1.2.1:单分支,单重条件判断 1.2.2:单分支,多重条件判断 1.2.3:if + else 1.2.4:多分支if + elif +else 1 ...
- The Same Game-POJ1027模拟
The Same Game Time Limit: 1000MS Memory Limit: 10000K Description The game named "Same" is ...
- PHP性状的使用
<?php trait Geocodable{ /** @var string */ protected $address; /** @var \Geocoder\Geocoder */ pro ...
- 通知Notification
步骤: 1.调用getSystemService()获取NotificationManager:NotificationManager manager = (NotificationManager)g ...
- linux和android博客链接
1.Tracy Mcgrady的专栏冰山一角:linux和Android底层开发,主要是mtk系列点击打开链接 2.郁闷Wednesday:嵌入式linux 单片机 android,点击打开链接 3. ...
- laravel的学习感受
学习laravel也有一段时间了自己感觉还是不怎么难就是自己的代码不熟悉所以很多的不怎么会.
- 第二,C语言示例
#include<stdio.h> int main (void) /*WTF*/ { int num; num=1; printf(" I am ...
- UVA 11210 中国麻将
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...