您现在的位置是:首页 >

窗体什么 .Net中掌握Windows窗体间数据交互(1)

火烧 2021-09-03 01:06:50 1053
.Net中掌握Wi dow 窗体间数据交互 1   Wi dow 窗体是用于 Micro oft Wi dow 应用程序开发的 基于 NET Framework 的新平台 此框架提供一个有条理的 面向
窗体什么 .Net中掌握Windows窗体间数据交互(1)

.Net中掌握Windows窗体间数据交互(1)  

  Windows 窗体是用于 Microsoft Windows 应用程序开发的 基于 NET Framework 的新平台 此框架提供一个有条理的 面向对象的 可扩展的类集 它使您得以开发丰富的 Windows 应用程序 一个Windows窗体就代表了 NET架构里的System Windows Forms Form类的一个实例     作者在CSDN技术论坛 NET板块下的C#分类经常看到有人问起如何在两个Form间传递数据 访问修改对方窗体里面的值 对于有经验的程序员来说不是什么高深的东西 而对于初学者来说这些基础的东西往往是一个问题 并且存在这种现象 往往比较复杂的东西他们会 要用什么了就去学什么 实际上并没有真正的去理解掌握它 基础不扎实 所以就有了想通过自己对窗体编程积累的经验来写一些这方面的文章 以供学 NET的朋友参考 也借此机会同各位朋友进行交流 写得不合理的地方请各位朋友提宝贵意见 下面我分了三个部分来讲     一.使用带参数的构造函数  我们要做的准备工作就是新建两个窗体 下面是两个窗体的布局 很简单        说明 Form 为主窗体 包含控件 文本框textBoxFrm 多选框checkBoxFrm 和按钮buttonEdit     Form 为子窗体 包含控件 文本框textBoxFrm 多选框checkBoxFrm 和按钮buttonOK buttonCancel     当我们新建一个窗体的时候 设计器会生成默认的构造函数     public Form ()  {   InitializeComponent();  }    它不带参数 既然我们要把Form 中的一些数据传到Form 中去 为什么不在Form 的构造函数里做文章呢?    假设我们要实现使Form 中的文本框显示Form 里textBoxFrm 的值 修改子窗体的构造函数     public Form (string text)  {   InitializeComponent();   this textBoxFrm Text = text;  }    增加Form 中的修改按钮点击事件 处理函数如下     private void buttonEdit_Click(object sender System EventArgs e)  {   Form formChild = new Form (this textBoxFrm Text);   formChild Show();  }    我们把this textBoxFrm Text作为参数传到子窗体构造函数 以非模式方式打开 这样打开的formChild的文本框就显示了 主窗体 文本 是不是很简单 接下来我们传一个boolean数据给子窗体     Public Form (string text bool checkedValue)  {   InitializeComponent();   this textBoxFrm Text = text;   this checkBoxFrm Checked = checkedValue;  }    在主窗体中的修改按钮点击处理 我采用了打开模式窗口的方式 其实在这个例子中看不出有什么分别     private void buttonEdit_Click(object sender System EventArgs e)  {   Form formChild = new Form (this textBoxFrm Text this checkBoxFrm Checked);   formChild ShowDialog();  }    结果在预料之中 但是这里明显存在不足 在子窗体里的数据修改后不能传给主窗体 也就是说主窗体不受子窗体的影响 而在实际的开发过程中我们经常使用子窗体来修改主窗体里面的数据 那怎么解决呢?    在 NET中有两种类型 值类型和引用类型 值类型是从ValueType继承而来 而ValueType又是从Object继承 对于引用类型它直接继承Object类型 这下让我们看看怎样通过Form 来修改Form 里的数据     还是让我们来修改Form 的代码     Private TextBox textBoxFrm ;  private CheckBox checkBoxFrm ;  public Form (TextBox heckbo CheckBox heckbox)  {   InitializeComponent();   this textBoxFrm Text = heckbo Text;   this checkBoxFrm Checked = heckbox Checked;   this textBoxFrm = heckbo;   this checkBoxFrm = heckbox;  }    现在我们传了两个引用类型的数据 TextBox类型 和CheckBox 另外在Form 中增加了两个类数据成员textBoxFrm checkBoxFrm 用来分别保存构造函数传来的变量 不过他们并不属于Form 的Controls容器 修改Form 的确定按钮点击事件函数     private void buttonOK_Click(object sender System EventArgs e)  {   this textBoxFrm Text = this textBoxFrm Text;   this checkBoxFrm Checked = this checkBoxFrm Checked;   this Close();  }    上面的代码我们通过把textBoxFrm 的Text和checkBoxFrm Checked赋给textBoxFrm 和checkBoxFrm 完成了对主窗体中的textBoxFrm 和checkBoxFrm 的修改 因为textBoxFrm 和textBoxFrm 是同一个引用 而checkBoxFrm 和checkBoxFrm 也是     到这里为止功能是实现了 但是总觉得不是很合理 让两个窗体控件传来传去 现在我举一个恰当一点的例子     修改了两个窗体      说明 在这个例子中我们的两个窗体都加了一个ListBox用来显示ArrayList中的内容     主窗体中控件 listBoxFrm buttonEdit   子窗体中控件 listBoxFrm textBoxAdd buttonAdd buttonEdit buttonOK     这次我们用ArrayList来作为传递数据 在Form 中定义类数据成员   private ArrayList listData ;    在构造函数中增加了对listData 进行内存分配 并生成数据最终绑定到listBoxFrm     public Form ()  {   InitializeComponent();   this listData = new ArrayList();   this listData Add( DotNet );   this listData Add( C# );   this listData Add( );   this listData Add( WebService );   this listData Add( XML );   this listBoxFrm DataSource = this listData ;  }    另外 对修改按钮点击事件处理函数的修改如下     private void buttonEdit_Click(object sender System EventArgs e)  {   Form formChild = new Form (this listData );   formChild ShowDialog();   this listBoxFrm DataSource = null;   this listBoxFrm DataSource = this listData ;  }    相对与主窗体 对子窗体作相应修改 也在Form 中增加了类数据成员     private ArrayList listData ;    用来保存对主窗体中listData 的引用     修改构造函数     public Form (ArrayList listData)  {   InitializeComponent();   this listData = listData;   foreach(object o in this listData )   {   this listBoxFrm Items Add(o);   }   }    这里让listData 同listData 指向同一个引用 另外没有对listBoxFrm进行绑定 采用了填充     好了 下面是对数据操作的时候了     添加处理函数代码如下     private void buttonAdd_Click(object sender System EventArgs e)  {   if(this textBoxAdd Text Trim() Length> )   {   this listData Add(this textBoxAdd Text Trim());   this listBoxFrm Items Add(this textBoxAdd Text Trim());   }   else   MessageBox Show( 请输入添加的内容! );  }    删除处理代码如下     private void buttonDel_Click(object sender System EventArgs e)  {   int index = this listBoxFrm SelectedIndex;   if(index!= )   {   this listData RemoveAt(index);   this listBoxFrm Items RemoveAt(index);   }   else   MessageBox Show( 请选择删除项或者没有可删除的项! );   }    退出Form 子窗体     private void buttonOK_Click(object sender System EventArgs e)  {   this Close();  }    编译运行程序 在子窗体中对数据进行修改 关闭后 主窗体就会显示更新后的数据     这里有一点要提醒一下 比较两个例子 我们都传的是引用类型 一个是String 另一个是ArrayList 为什么string类型不能修改主窗体的数据呢?其实在 Net中对string类型的修改并不是修改原来的值 原来的值没有变化 而是重新生成一个新的字符串 下面是一个很好的说明     public class ZZConsole  {   [STAThread]   static void Main(string[] args)   {   string str = abc ;   string str = str ;   str = ;   Console WriteLine(str );   Console WriteLine( );   Console WriteLine(str );   Console WriteLine( );   ArrayList al = new ArrayList();   al Add( abc );   ArrayList al = al ;   al Add( );   foreach(object o in al )  Console WriteLine((string)o);   Console WriteLine( );   foreach(object o in al )  Console WriteLine((string)o);   Console ReadLine(); lishixinzhi/Article/program/Java/hx/201311/26426  
永远跟党走
  • 如果你觉得本站很棒,可以通过扫码支付打赏哦!

    • 微信收款码
    • 支付宝收款码