两个窗体间实现数据传递

来源:互联网 发布:如何使用itudou软件 编辑:程序博客网 时间:2024/06/06 03:51

在多窗体编程中,经常会遇到两个甚至更多窗体间数据的传递。(暂时先写个两个窗体的)。

父窗口控件:ListBox listinfo;(也可以采用ListView控件,但还是有差别的)   Button butEdit ;     Button butAdd ;   Button butDelete; 

子窗口控件:TextBox texName;  ComboBox ComGender;  ComboBox ComTime;  Button butOK ;   Button butCancel;

父窗口 子窗口   提示对话框

要求:1.在父窗口选中选项,点击编辑按钮,弹出子对话框,并把父窗口中的值对应传入信息编辑;

            2.在父窗口中点击添加按钮,弹出子对话框;

           3..在父窗口选中选项,点击删除按钮,弹出是否要删除对话框;

根据要求,理应设计出三个类:1.父窗口:FormMain  2.子窗口:FormEdit   3.people

这篇文章主要放在父子窗口之间数据是怎么传递的:

    在FormMain 类中,我们定义ArrayList  list来存放people类的对象(  list.Add(new people("诺亚","男","旧约"));  .........),通过循环我们可以把list对象的add到ListBox中

    

 foreach (people item in list)            {                listInfo.Items.Add(item.ToString());            }

好了,现在父窗口的界面出来以后,要实现各按钮的事件,也就是数据的传递了。

一:编辑按钮:

思路:要把数据传递给子窗口,首先应该在FormMain 类中可以引用到texName,ComGender, ComTime,但很遗憾这三个变量是private,为了不破坏封装性,自然想到的是分别对这三个变量定义属性。(当然可以,不过如果变量多的话,就太繁琐了。)

那就干脆把这三个变量看成一个整体,也就是一个people对象来定义属性,这样数据传递起来就方便多了。于是在FormEdit类中就有了info属性:

public people info {            get {                 return new people(texName.Text, comGender.Text, comTime.Text);             }            set {                                   texName.Text = value.Name;                    comGender.Text = value.Gender;                    comTime.Text = value.Time;            }
现在思路有了,实现起来就比较有头绪: 想要知道用户要编辑的哪个选项,用 int index=listInfo.SelectedIndex; 获取索引值,再去动态数组中根据索引值找到相应的对象,获取到该对象各个实例域,传给FormEdit类的三个变量。

FormEdit fe=new FormEdit();            int index=listInfo.SelectedIndex;  //获取选中选项的索引值            if (index == -1)            {                MessageBox.Show("请选择要编辑的项");                return;            }                //把值传入信息编辑窗口                people p = (people)list[index];    //这条语句很关键                fe.info = new people(p.Name, p.Gender, p.Time);                fe.ShowDialog();   
这样就实现了单方向的数据传递,“编辑” 就有可能涉及到对对象修改的问题,现在要解决的就是如何从子窗口修改后的值传入父窗口呢?其实道理都一样,只不过一个是set,一个是get罢了。直接贴代码了
            //把信息窗口编辑后的值传入主窗口            people p1=fe.info;                if(fe.DialogResult == DialogResult.OK)   //之前先要把”确定“按钮的DialogResult属性设为OK            {                p.Name = p1.Name;                p.Gender = p1.Gender;                p.Time = p1.Time;                listInfo.Items[index]= p1.ToString();                     }

两个窗体间就可以通过编辑按钮实现信息的传递了,还有两个按钮就不写了,大同小异。


没写技术博客的经验,欢迎交流,拍砖:

整个程序的代码:

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace test5{    public class people    {        private string name;        private string gender;        private string time;        public people(string name, string gender, string time)        {            this.name = name;            this.gender = gender;            this.time = time;        }        public string Name        {            get { return name; }            set { name = value; }        }        public string Gender        {            get { return gender; }            set { gender = value; }        }        public string Time        {            get { return time; }            set { time = value; }        }       override public string ToString()        {            return "姓名:" + name + ";  性别:" + gender + ";  生于:" + time + "时代";        }    }}

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;using System.Collections;namespace test5{    public partial class FormMain : Form    {        private ArrayList list=new ArrayList();        public FormMain()        {               InitializeComponent();            list.Add(new people("诺亚","男","旧约"));            list.Add(new people("路德","女","新约"));            list.Add(new people("亚伯拉罕", "男", "旧约"));            list.Add(new people("提摩太", "男", "新约"));            list.Add(new people("保罗", "男", "新约"));            list.Add(new people("亚当", "男", "旧约"));            list.Add(new people("夏娃", "女", "旧约"));            foreach (people item in list)            {                listInfo.Items.Add(item.ToString());            }        }        //编辑按钮        private void btoEdit_Click(object sender, EventArgs e)        {            FormEdit fe=new FormEdit();            int index=listInfo.SelectedIndex;  //获取选中选项的索引值            if (index == -1)            {                MessageBox.Show("请选择要编辑的项");                return;            }                //把值传入信息编辑窗口                people p = (people)list[index];                fe.info = new people(p.Name, p.Gender, p.Time);                fe.ShowDialog();                    //把信息窗口编辑后的值传入主窗口            people p1=fe.info;                        if(fe.DialogResult == DialogResult.OK)            {                p.Name = p1.Name;                p.Gender = p1.Gender;                p.Time = p1.Time;                listInfo.Items[index]= p1.ToString();                  }                }        //添加按钮        private void btoAdd_Click(object sender, EventArgs e)        {            FormEdit fe = new FormEdit();            fe.ShowDialog();            if (fe.DialogResult == DialogResult.OK)            {                     people p = new people("","","");                people p1 = fe.info;                if(p1.Name==""||p1.Gender==""||p1.Time=="")                {                MessageBox.Show("信息不能为空");                return;                }                if (p1.Gender!= "男" && p1.Gender!= "女")                {                    MessageBox.Show("对不起,性别不在人类范围内");                    return;                }                if (p1.Time != "旧约" && p1.Time != "新约")                {                    MessageBox.Show("对不起,时代不在圣经范围内");                    return;                }                p.Name = p1.Name;                p.Gender = p1.Gender;                p.Time = p1.Time;                list.Add(p);                listInfo.Items.Add( p1.ToString());            }        }        //删除按钮        private void btoDelete_Click(object sender, EventArgs e)        {            if (listInfo.SelectedItem == null)                MessageBox.Show("请选择要删除的项");            else            {               DialogResult result=MessageBox.Show("是否要删除此项", "提示",MessageBoxButtons.OKCancel, MessageBoxIcon.Question);               if (result == DialogResult.OK)               {                   list.RemoveAt(listInfo.SelectedIndex);                   listInfo.Items.Remove(listInfo.SelectedItem);                   MessageBox.Show("已删除选中信息");               }                            }        }    }}

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 test5{    public partial class FormEdit : Form    {        people p;        public FormEdit()        {            InitializeComponent();        }        public people info {            get {                 return new people(texName.Text, comGender.Text, comTime.Text);             }            set {                                   texName.Text = value.Name;                    comGender.Text = value.Gender;                    comTime.Text = value.Time;            }        }    }}




原创粉丝点击