总结C#中窗体间传递数据的几种方法.docx
- 文档编号:28626309
- 上传时间:2023-07-19
- 格式:DOCX
- 页数:25
- 大小:23.34KB
总结C#中窗体间传递数据的几种方法.docx
《总结C#中窗体间传递数据的几种方法.docx》由会员分享,可在线阅读,更多相关《总结C#中窗体间传递数据的几种方法.docx(25页珍藏版)》请在冰豆网上搜索。
总结C#中窗体间传递数据的几种方法
总结C#中窗体间传递数据的几种方法(由别人的方法整理)
在编写C#windows应用程序的时候我们经常会遇到这种问题,怎么样在两个窗体间传递数据呢?
以下是我整理的网上的各种方法,在遇到一个实际问题:
在form1中打开一个form2窗口作为录入界面,将录入的值经转换后在form1中显示。
采用了委托的方法,可以实现。
(与VC的回调的应用相似)
1.可以通过委托的方法来解决
问题:
通过form1做一个录入界面,将里边通过文本框录入的数值复值给form2中的listview各列,用3个textbox1.text举例吧,分别对应listview的3个列。
可以这么做,如果两个窗体是在同一个命名空间下
定义一个代理,注意这个代理是全局的:
(即同一命名空间下,与Form1,Form2平级的)
publicdelegatevoidMyInvoke(stringItem1,stringItem2,stringItem3);
//在窗体From2中有这么一个回调函数,用于在ListView里添加一个新项的:
privatevoidUpdateListView(stringItem1,stringItem2,stringItem3)
{
ListView1.Items.Add(Item1);
ListView1.Items[ListView1.Items.Count-1].SubItems.Add(Item2);
ListView1.Items[ListView1.Items.Count-1].SubItems.Add(Item3);
}
//比如说点击Form2的一个按钮弹出Form1进行录入,在点击按钮的事件下:
//把委托传过去
Form1frmEdit=newForm1(newMyInvoke(UpdateListView));
frmEdit.ShowDialog(this);
//在Form1里定义一个属性
privateMyInvokemi=null;
在构造函数中接收这个委托:
publicForm1(MyInvokemyInvoke)
{
this.mi=myInvoke;
}
//录入数据后,点击OK按钮,在点击事件下:
//回调
this.mi(this.TextBox1.Text,this.TextBox3.Text,this.TextBox3.Text);
this.Close();//关闭Form1
补充:
如果我要是想再把form2的值给form1,
Form1frmEdit=newForm1(newMyInvoke(UpdateListView),stringpara1,stringpara2...);
frmEdit.ShowDialog(this);
然后将Form1的构造函数改成可以接收几个参数的就行了。
2.假如主框架为Form1,打开的搜索对话框是Form2.直接在Form2类中申明一个Form1实例:
Form1f1=newForm1();然后就可以通过f1来调用Form1中的域和函数了。
其实不是这样的,你申明的新的Form1实例不是原来的那个Form1对象了,这样操作的是新的Form1中的域和函数,和最先打开的Form1是没有关系的。
我们要做的是把当前的Form1实例传递给Form2,如果是这样的话,问题就很好解决了。
方法1:
首先,我们在Form2中定义:
privateForm1mF_Form
我们更改Form2的构造函数为有参数的
publicForm2(Form1myForm)
{
//
//Windows窗体设计器支持所必需的
//
InitializeComponent();
this.mF_Form =myForm; /////这样在Form1中申明Form2的时候就把Form1的实例传递过来了
//
//TODO:
在InitializeComponent调用后添加任何构造函数代码
//
}
在Form1中,我在要用到Form2的地方申明如下:
Form2f2=new Form2(this);////这里的this指的就是Form1当前的实例,也就是把当前Form1的实例通过Form2的构造函数传递给Form2类(其实在网上看到过比较蠢的方式,就是在构造函数里面传递要传递的信息如:
字符串或是数字等,这样做很有局限性,不能传递其他的,所有我们可以直接传递实例,来完成传递更多的信息。
)
这样在Form2中使用myForm就可以对原来的Form1窗口进行操作了。
但是你要把要操作的Form1中的域和函数定义成public形式的(这样可能不安全),此时的myForm就是真正的最开始打开的Form1了,你可以用这个实例来进行两个窗体的通讯了。
()
3.其实C#中提供了窗体间进行通讯的现成的属性,呵呵,我们能想到的,微软也想到了,他们创造的语言其实确实可以说是人性化了。
在Form1类中申明Form2时用如下代码:
Form2f2=newForm2();//////类Form2中的构造函数不改,还是无参的
f2.owner=this;////这里的this指的是类Form1当前的实例。
//也可以使用函数的方法,给当前实例添加一个附属窗口 代码:
this.AddOwnedForm(f2);
在Form2类的定义中写如下代码:
Form1f1=this.owner;
这样f1对应的就是原来的Form1的实例了,也就可以用这个进行通讯了。
但是还是要把不同类之间访问的域和函数定义成public,哎,安全确实是一个问题!
!
4.使用静态类
这个也是我们经常要用到的一种数据交互方法。
下面是定义的一个类:
usingSystem;
usingSystem.Collections;
namespaceZZ
{
publicclassAppDatas
{
privatestaticArrayListlistData;
staticAppDatas()
{
listData=newArrayList();
listData.Add("DotNet");
listData.Add("C#");
listData.Add("A");
listData.Add("WebService");
listData.Add("XML");
}
publicstaticArrayListListData
{
get{returnlistData;}
}
publicstaticArrayListGetListData()
{
returnlistData;
}
}
}
上面包含了一个静态类成员,listData,一个静态构造函数staticAppDatas(),用来初始化listData的数据。
还有一个静态属性ListData和一个静态GetListData()方法,他们实现了同样的功能就是返回listData。
由于前面两篇文章已经讲了很多,这里不细说了,下面是完整的代码:
Form1.cs文件
usingSystem;
usingSystem.Drawing;
usingSystem.Collections;
usingSystem.ComponentModel;
usingSystem.Windows.Forms;
namespaceZZ
{
publicclassForm1:
System.Windows.Forms.Form
{
privateSystem.Windows.Forms.ButtonbuttonEdit;
privateSystem.Windows.Forms.ListBoxlistBoxFrm1;
privateSystem.ComponentModel.Containercomponents=null;
publicForm1()
{
InitializeComponent();
this.listBoxFrm1.DataSource=AppDatas.ListData;
}
protectedoverridevoidDispose(booldisposing)
{
if(disposing)
if(components!
=null)
components.Dispose();
base.Dispose(disposing);
}
[STAThread]
staticvoidMain()
{
Application.Run(newForm1());
}
privatevoidInitializeComponent()
{
this.buttonEdit=newSystem.Windows.Forms.Button();
this.listBoxFrm1=newSystem.Windows.Forms.ListBox();
this.SuspendLayout();
this.buttonEdit.Location=newSystem.Drawing.Point(128,108);
this.buttonEdit.Name="buttonEdit";
this.buttonEdit.TabIndex=1;
this.buttonEdit.Text="修改";
this.buttonEdit.Click+=newSystem.EventHandler(this.buttonEdit_Click);
this.listBoxFrm1.ItemHeight=12;
this.listBoxFrm1.Location=newSystem.Drawing.Point(12,8);
this.listBoxFrm1.Name="listBoxFrm1";
this.listBoxFrm1.Size=newSystem.Drawing.Size(108,124);
this.listBoxFrm1.TabIndex=2;
this.AutoScaleBaseSize=newSystem.Drawing.Size(6,14);
this.ClientSize=newSystem.Drawing.Size(208,141);
this.Controls.Add(this.listBoxFrm1);
this.Controls.Add(this.buttonEdit);
this.Name="Form1";
this.Text="Form1";
this.ResumeLayout(false);
}
privatevoidbuttonEdit_Click(objectsender,System.EventArgse)
{
Form2formChild=newForm2();
formChild.ShowDialog();
this.listBoxFrm1.DataSource=null;
this.listBoxFrm1.DataSource=AppDatas.ListData;
}
}
}
Form2.cs文件
usingSystem.Drawing;
usingSystem.Collections;
usingSystem.ComponentModel;
usingSystem.Windows.Forms;
namespaceZZ
{
publicclassForm2:
System.Windows.Forms.Form
{
privateSystem.Windows.Forms.ButtonbuttonOK;
privateSystem.ComponentModel.Containercomponents=null;
privateSystem.Windows.Forms.ListBoxlistBoxFrm2;
privateSystem.Windows.Forms.ButtonbuttonAdd;
privateSystem.Windows.Forms.ButtonbuttonDel;
privateSystem.Windows.Forms.TextBoxtextBoxAdd;
publicForm2()
{
InitializeComponent();
foreach(objectoinAppDatas.ListData)
this.listBoxFrm2.Items.Add(o);
}
protectedoverridevoidDispose(booldisposing)
{
if(disposing)
if(components!
=null)
components.Dispose();
base.Dispose(disposing);
}
privatevoidInitializeComponent()
{
this.buttonOK=newSystem.Windows.Forms.Button();
this.listBoxFrm2=newSystem.Windows.Forms.ListBox();
this.buttonAdd=newSystem.Windows.Forms.Button();
this.buttonDel=newSystem.Windows.Forms.Button();
this.textBoxAdd=newSystem.Windows.Forms.TextBox();
this.SuspendLayout();
this.buttonOK.Location=newSystem.Drawing.Point(188,108);
this.buttonOK.Name="buttonOK";
this.buttonOK.TabIndex=0;
this.buttonOK.Text="确定";
this.buttonOK.Click+=newSystem.EventHandler(this.buttonOK_Click);
this.listBoxFrm2.ItemHeight=12;
this.listBoxFrm2.Location=newSystem.Drawing.Point(8,8);
this.listBoxFrm2.Name="listBoxFrm2";
this.listBoxFrm2.Size=newSystem.Drawing.Size(168,124);
this.listBoxFrm2.TabIndex=2;
this.buttonAdd.Location=newSystem.Drawing.Point(188,44);
this.buttonAdd.Name="buttonAdd";
this.buttonAdd.TabIndex=3;
this.buttonAdd.Text="增加";
this.buttonAdd.Click+=newSystem.EventHandler(this.buttonAdd_Click);
this.buttonDel.Location=newSystem.Drawing.Point(188,76);
this.buttonDel.Name="buttonDel";
this.buttonDel.TabIndex=4;
this.buttonDel.Text="删除";
this.buttonDel.Click+=newSystem.EventHandler(this.buttonDel_Click);
this.textBoxAdd.Location=newSystem.Drawing.Point(188,12);
this.textBoxAdd.Name="textBoxAdd";
this.textBoxAdd.Size=newSystem.Drawing.Size(76,21);
this.textBoxAdd.TabIndex=5;
this.textBoxAdd.Text="";
this.AutoScaleBaseSize=newSystem.Drawing.Size(6,14);
this.ClientSize=newSystem.Drawing.Size(272,141);
this.Controls.Add(this.textBoxAdd);
this.Controls.Add(this.buttonDel);
this.Controls.Add(this.buttonAdd);
this.Controls.Add(this.listBoxFrm2);
this.Controls.Add(this.buttonOK);
this.Name="Form2";
this.Text="Form2";
this.ResumeLayout(false);
}
privatevoidbuttonOK_Click(objectsender,System.EventArgse)
{
this.Close();
}
privatevoidbuttonAdd_Click(objectsender,System.EventArgse)
{
if(this.textBoxAdd.Text.Trim().Length>0)
{
AppDatas.ListData.Add(this.textBoxAdd.Text.Trim());
this.listBoxFrm2.Items.Add(this.textBoxAdd.Text.Trim());
}
else
MessageBox.Show("请输入添加的内容!
");
}
privatevoidbuttonDel_Click(objectsender,System.EventArgse)
{
intindex=this.listBoxFrm2.SelectedIndex;
if(index!
=-1)
{
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 总结 C# 窗体 传递 数据 方法