服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

.net中掌握windows窗体间数据交互(3)


  在第一篇和第二篇文章中我们使用带参数的构造函数、属性以及方法实现了数据的交互,接下来要讲的是使用静态类来完成窗体间的数据交互。这个也是我们经常要用到的一种数据交互方法。
  
  三.使用静态类
  
  下面是定义的一个类:
  
  using system;
  using system.collections;
  namespace zz
  {
  public class appdatas
  {
    private static arraylist listdata;
    static appdatas()
    {
    listdata = new arraylist();
    listdata.add("dotnet");
    listdata.add("c#");
    listdata.add("asp.net");
    listdata.add("webservice");
    listdata.add("xml");
    }
    public static arraylist listdata
    {
    get{return listdata;}
    }
    public static arraylist getlistdata()
    {
    return listdata;
    }
  }
  }
  
  上面包含了一个静态类成员,listdata,一个静态构造函数static appdatas(),用来初始化listdata的数据。还有一个静态属性listdata和一个静态getlistdata()方法,他们实现了同样的功能就是返回listdata。
  
  由于前面两篇文章已经讲了很多,这里不细说了,下面是完整的代码:
  
  form1.cs文件
  
  using system;
  using system.drawing;
  using system.collections;
  using system.componentmodel;
  using system.windows.forms;
  namespace zz
  {
  public class form1 : system.windows.forms.form
  {
    private system.windows.forms.button buttonedit;
    private system.windows.forms.listbox listboxfrm1;
    private system.componentmodel.container components = null;
    public form1()
    {
    initializecomponent();
    this.listboxfrm1.datasource = appdatas.listdata;
    }
    protected override void dispose( bool disposing )
    {
    if( disposing )
    if(components != null)
    components.dispose();
    base.dispose( disposing );
    }
    [stathread]
    static void main()
    {
    application.run(new form1());
    }
    private void initializecomponent()
    {
    this.buttonedit = new system.windows.forms.button();
    this.listboxfrm1 = new system.windows.forms.listbox();
    this.suspendlayout();
    this.buttonedit.location = new system.drawing.point(128, 108);
    this.buttonedit.name = "buttonedit";
    this.buttonedit.tabindex = 1;
    this.buttonedit.text = "修改";
    this.buttonedit.click += new system.eventhandler(this.buttonedit_click);
    this.listboxfrm1.itemheight = 12;
    this.listboxfrm1.location = new system.drawing.point(12, 8);
    this.listboxfrm1.name = "listboxfrm1";
    this.listboxfrm1.size = new system.drawing.size(108, 124);
    this.listboxfrm1.tabindex = 2;
    this.autoscalebasesize = new system.drawing.size(6, 14);
    this.clientsize = new system.drawing.size(208, 141);
    this.controls.add(this.listboxfrm1);
    this.controls.add(this.buttonedit);
    this.name = "form1";
    this.text = "form1";
    this.resumelayout(false);
    }
    private void buttonedit_click(object sender, system.eventargs e)
    {
    form2 formchild = new form2();
    formchild.showdialog();
    this.listboxfrm1.datasource = null;
    this.listboxfrm1.datasource = appdatas.listdata;
    }
  }
  }
  
  form2.cs文件
  
  using system.drawing;
  using system.collections;
  using system.componentmodel;
  using system.windows.forms;
  namespace zz
  {
  public class form2 : system.windows.forms.form
  {
    private system.windows.forms.button buttonok;
    private system.componentmodel.container components = null;
    private system.windows.forms.listbox listboxfrm2;
    private system.windows.forms.button buttonadd;
    private system.windows.forms.button buttondel;
    private system.windows.forms.textbox textboxadd;
    public form2()
    {
    initializecomponent();
    foreach(object o in appdatas.listdata)
    this.listboxfrm2.items.add(o);
    }
    protected override void dispose( bool disposing )
    {
    if( disposing )
    if(components != null)
    components.dispose();
    base.dispose( disposing );
    }
    private void initializecomponent()
    {
    this.buttonok = new system.windows.forms.button();
    this.listboxfrm2 = new system.windows.forms.listbox();
    this.buttonadd = new system.windows.forms.button();
    this.buttondel = new system.windows.forms.button();
    this.textboxadd = new system.windows.forms.textbox();
    this.suspendlayout();
    this.buttonok.location = new system.drawing.point(188, 108);
    this.buttonok.name = "buttonok";
    this.buttonok.tabindex = 0;
    this.buttonok.text = "确定";
    this.buttonok.click += new system.eventhandler(this.buttonok_click);
    this.listboxfrm2.itemheight = 12;
    this.listboxfrm2.location = new system.drawing.point(8, 8);
    this.listboxfrm2.name = "listboxfrm2";
    this.listboxfrm2.size = new system.drawing.size(168, 124);
    this.listboxfrm2.tabindex = 2;
    this.buttonadd.location = new system.drawing.point(188, 44);
    this.buttonadd.name = "buttonadd";
    this.buttonadd.tabindex = 3;
    this.buttonadd.text = "增加";
    this.buttonadd.click += new system.eventhandler(this.buttonadd_click);
    this.buttondel.location = new system.drawing.point(188, 76);
    this.buttondel.name = "buttondel";
    this.buttondel.tabindex = 4;
    this.buttondel.text = "删除";
    this.buttondel.click += new system.eventhandler(this.buttondel_click);
    this.textboxadd.location = new system.drawing.point(188, 12);
    this.textboxadd.name = "textboxadd";
    this.textboxadd.size = new system.drawing.size(76, 21);
    this.textboxadd.tabindex = 5;
    this.textboxadd.text = "";
    this.autoscalebasesize = new system.drawing.size(6, 14);
    this.clientsize = new system.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);
    }
    private void buttonok_click(object sender, system.eventargs e)
    {
    this.close();
    }
    private void buttonadd_click(object sender, system.eventargs e)
    {
    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("请输入添加的内容!");
    }
    private void buttondel_click(object sender, system.eventargs e)
    {
    int index = this.listboxfrm2.selectedindex;
    if(index!=-1)
    {
  appdatas.listdata.removeat(index);
    this.listboxfrm2.items.removeat(index);
    }
    else
    messagebox.show("请选择删除项!");
    }
  }
  }
  
  总结,

扫描关注微信公众号