winform怎么实现选项卡似的窗体效果,IE7那样.
mdi窗体中的子窗体
C# Winform
答案:4 悬赏:20 手机版
解决时间 2021-03-18 07:49
- 提问者网友:我一贱你就笑
- 2021-03-17 16:57
最佳答案
- 五星知识达人网友:归鹤鸣
- 2021-03-17 17:28
创建一个MDI窗口,并添加一个ToolStrip控件。
创建一个窗体的基类
public class BaseForm: Form{
protected ToolStrip toolStrip;
public static List toolStripItemList = new Lis();
protected void AddToolStrip(ToolStripItem toolStripItem){
toolStripItemList.add(toolStripItem);
if(toolStrip != null){
toolStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[]{ toolStripItem });
}
}
protected void SubToolStrip(string formName){
if(toolStrip != null){
foreach (ToolStripItem i in toolStripItemList){
if (i.Name == formName){
toolStrip1.Items.remove(i);
}
}
}
}
}
创建一个父窗体基类
public class MdiForm: BaseForm{
//在Mdi窗体中创建一个ToolStrip控件 toolStrip1,并赋值给基类中的toolStrip
protected override void OnLoad(EventArgs e) {
base.OnLoad(e);
toolStrip = this.toolStrip1;
}
}
创建一个子窗体基类
public class ChildForm: BaseForm{
protected override void OnLoad(EventArgs e) {
base.OnLoad(e);
ToolStripItem toolStripItem = new ToolStripItem();
toolStripItem.Name = this.Name;
toolStripItem.Text = "xxxxxx";
AddToolStrip(toolStripItem);
}
protected override void OnFormClosed(FormClosedEventArgs e) {
base.OnFormClosed(e);
SubToolStrip(this.Name);
}
}
只是一个简单思路,所有窗体同时继承一个基类,在这个基类里创建连个对象ToolStrip(在Mdi窗口上添加按钮的控件),List(这里为了简单期间,用了一个简单的List,如果要加上其他效果,比如某个窗体里字段改变,让ToolStrip上的按钮有不同的现实效果,图片等等,你需要创建一个ToolStripButtonCollection)每次加载一个新的子窗体时,就往ToolStripButtonCollection增加一项,ToolStripButtonCollection会自动的再ToolStrip上增加一个按钮,同样ToolStripButtonCollection减少一项,ToolStrip会自动减少一个按钮,这里你还可以检测ToolStripButtonCollection中的一些值,比如说判断字段是否改变,是否需要保存等等。
之后创建Mdi窗体,初始化基类里的toolStrip;
之后创建子窗体,只要是从ChildForm继承的子窗体,就会自动在Mdi窗口上创建一个以子窗体名称命名的按钮,非常类似你要的效果,还有以上代码只是演示思路,重要的还是ToolStripButtonCollection这个类的创建。
创建一个窗体的基类
public class BaseForm: Form{
protected ToolStrip toolStrip;
public static List
protected void AddToolStrip(ToolStripItem toolStripItem){
toolStripItemList.add(toolStripItem);
if(toolStrip != null){
toolStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[]{ toolStripItem });
}
}
protected void SubToolStrip(string formName){
if(toolStrip != null){
foreach (ToolStripItem i in toolStripItemList){
if (i.Name == formName){
toolStrip1.Items.remove(i);
}
}
}
}
}
创建一个父窗体基类
public class MdiForm: BaseForm{
//在Mdi窗体中创建一个ToolStrip控件 toolStrip1,并赋值给基类中的toolStrip
protected override void OnLoad(EventArgs e) {
base.OnLoad(e);
toolStrip = this.toolStrip1;
}
}
创建一个子窗体基类
public class ChildForm: BaseForm{
protected override void OnLoad(EventArgs e) {
base.OnLoad(e);
ToolStripItem toolStripItem = new ToolStripItem();
toolStripItem.Name = this.Name;
toolStripItem.Text = "xxxxxx";
AddToolStrip(toolStripItem);
}
protected override void OnFormClosed(FormClosedEventArgs e) {
base.OnFormClosed(e);
SubToolStrip(this.Name);
}
}
只是一个简单思路,所有窗体同时继承一个基类,在这个基类里创建连个对象ToolStrip(在Mdi窗口上添加按钮的控件),List
之后创建Mdi窗体,初始化基类里的toolStrip;
之后创建子窗体,只要是从ChildForm继承的子窗体,就会自动在Mdi窗口上创建一个以子窗体名称命名的按钮,非常类似你要的效果,还有以上代码只是演示思路,重要的还是ToolStripButtonCollection这个类的创建。
全部回答
- 1楼网友:十鸦
- 2021-03-17 20:49
关注~
- 2楼网友:北城痞子
- 2021-03-17 19:18
this.IsMdiContainer = true;//设置当前窗体为主窗体 这个在属性里面可以设置的 你在右边找找
Form2 f = new Form2();
f.MdiParent = this;//设置调用窗体的父窗体为当前的
f.Show();
- 3楼网友:孤老序
- 2021-03-17 18:37
这个相当于一个全局的属性,需要用公共类来实现。
// 定义静态公共类
// 既可以定义公用属性,也可以定义公用方法
public static class publicdata{
// 定义变量
private static string username;
// 定义属性
public static string username{
// 根据情况需要,通过 get,set 访问器设置访问规则
get { return username; }
set { username = value; }
}
}
// 调用
// 功能执行类
class myfunction{
// ...
// 保存用户名
publicdata.username = "myuser";
// ...
// 获取当前用户名
string curuser = publicdata.username;
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯