c# 对话框交换数据

 本例是一个管理联系人信息的小程序,程序有两个窗体,一个主窗体,在listview控件中显示联系人信息列表,一个对话框窗体,用来显示和修改 某个联系人的信息。通过主窗体的菜单命令,可以打开对话框,并把主窗体listview中的当前选中的联系人数据传递给对话框,在对话框中可以对联系人信 息进行修改,修改完毕后,单击确定按钮,主窗体根据用户在对话框中的输入更新listview空间

1.创建一个windows应用程序

2.在项目中添加联系人类。如下

  1.  public class conntactpeople  
  2.    {  
  3.        private String _name;  
  4.   
  5.        public String name  
  6.        {  
  7.            get { return _name; }  
  8.            set { _name = value; }  
  9.        }  
  10.        private bool _isFemale=false;  
  11.   
  12.        public  bool isFemale  
  13.        {  
  14.            get { return _isFemale; }  
  15.            set { _isFemale = value; }  
  16.        }  
  17.        private DateTime _dateOfBirth;  
  18.   
  19. public DateTime dateOfBirth  
  20. {  
  21.     get { return _dateOfBirth;}  
  22.     set { _dateOfBirth = value;}  
  23. }  
  24.    private String _company;  
  25.   
  26.    public String company  
  27.    {  
  28.        get { return _company; }  
  29.        set { _company = value; }  
  30.    }  
  31.    private String _telephone;  
  32.   
  33.    public String telephone  
  34.    {  
  35.        get { return _telephone; }  
  36.        set { _telephone = value; }  
  37.    }  

3.添加一个窗体对话框

4.在contactDialog窗体的load时间中,对窗体进行初始化。代码如下
  1. button1.DialogResult = DialogResult.OK;  
  2. button2.DialogResult = DialogResult.Cancel;  
5.在contactDialog类中,添加一个Contact类型的属性,用来与外界交换数据。代码如下:
  1. private conntactpeople _contact;  
  2.   
  3.  public conntactpeople contact  
  4.  {  
  5.      get   
  6.      {  
  7.          conntactpeople c = new conntactpeople();  
  8.          c.name = textBox1.Text;  
  9.          c.company = textBox2.Text;  
  10.          c.dateOfBirth = dateTimePicker1.Value;  
  11.          c.telephone = maskedTextBox2.Text;  
  12.          c.isFemale = radioButton2.Checked;  
  13.          return c;  
  14.   
  15.      }  
  16.      set   
  17.      {  
  18.          textBox1.Text = value.name;  
  19.          radioButton2.Checked = value.isFemale;  
  20.          radioButton1.Checked = !value.isFemale;  
  21.          dateTimePicker1.Value = value.dateOfBirth;  
  22.          textBox2.Text = value.company;  
  23.          maskedTextBox2.Text = value.telephone;  
  24.      }  
  25.  }  
6.在主船体中添加listview空间,以及ContextMenustrip空间,添加菜单项:添加,删除,修改。并把listview的ContextMenuStrip设置为此菜单。此时,主窗体就可以用Contact类了。
7.在主窗体的load事件添加代码:
  1.  listView1.View = View.Details;  
  2.  listView1.GridLines=true;  
  3.  listView1.Columns.Add("姓名", 80);  
  4.  //listView1.co  
  5.  listView1.Columns.Add("性别", 40);  
  6.  listView1.Columns.Add("出生日期", 100);  
  7.  listView1.Columns.Add("工作单位",160);  
  8.  listView1.Columns.Add("联系电话", 100);  
  9.  listView1.HideSelection = true;  
  10.  listView1.FullRowSelect = true;  
  11. ContactDialog dialog = new ContactDialog();  
  12. dialog.Owner = this;  
8.主窗体中的添加,删除,更新按钮中添加相应代码即可
  1. private void 添加ToolStripMenuItem_Click(object sender, EventArgs e)  
  2. {  
  3.    ContactDialog dialog = new ContactDialog();  
  4.   //  dialog.contact = null;  
  5.     if (dialog.ShowDialog()==DialogResult.OK)  
  6.     {  
  7.         addContact(dialog.contact);  
  8.   
  9.     }  
  10. }  
  11. //添加函数  
  12. private void addContact(conntactpeople c)  
  13. {  
  14.     ListViewItem item = listView1.Items.Add(c.name);  
  15.   
  16.     updateContact(item, c);  
  17. }  
  18.   
  19. //更新函数  
  20. private void updateContact(ListViewItem item,conntactpeople c)  
  21. {  
  22.     item.SubItems.Clear();  
  23.     //item.SubItems.c  
  24.     item.Text = c.name;  
  25.     if (c.isFemale)  
  26.     {  
  27.         item.SubItems.Add("女");  
  28.   
  29.     }  
  30.     else  
  31.     {  
  32.         item.SubItems.Add("男");  
  33.     }  
  34.     item.SubItems.Add(c.dateOfBirth.ToString("yyyy-MM-dd"));  
  35.     item.SubItems.Add(c.company);  
  36.     item.SubItems.Add(c.telephone);  
  37. }  
  38.   
  39. private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)  
  40. {  
  41.     if (listView1.SelectedIndices.Count>0)  
  42.     {  
  43.   
  44.       
  45.       
  46.         if (MessageBox.Show("ni"+listView1.Items[listView1.SelectedIndices[0]].Text+"ma?","dd",MessageBoxButtons.YesNoCancel)==DialogResult.Yes)  
  47.         {  
  48.             listView1.Items.RemoveAt(listView1.SelectedIndices[0]);  
  49.           //  listView1.Items.RemoveAt(listView1.SelectedIndices[])  
  50.         }  
  51.     }  
  52. }  
  53.   
  54.   
  55.   
  56. private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)  
  57. {  
  58.     if (listView1.SelectedIndices.Count==0)  
  59.     {  
  60.         MessageBox.Show("请选择要修改的人");  
  61.         return;  
  62.   
  63.     }  
  64.     conntactpeople c = new conntactpeople();  
  65.     ListViewItem item = listView1.SelectedItems[0];  
  66.     c.name = item.Text;  
  67.     //item.SubItems[];  
  68.     c.isFemale=(item.SubItems[1].Text=="女");  
  69.     try  
  70.     {  
  71.         c.dateOfBirth = DateTime.Parse(item.SubItems[2].Text.ToString());  
  72.     }  
  73.     catch  
  74.     {  
  75.         System.Diagnostics.Trace.Write(c.dateOfBirth);  
  76.         MessageBox.Show(item.SubItems[2].Text);  
  77.           
  78.         //System.Diagnostics.Trace(item.SubItems[1].Text);  
  79.     }  
  80.       
  81.     c.company = item.SubItems[3].Text;  
  82.     c.telephone = item.SubItems[4].Text;  
  83.     ContactDialog dialog = new ContactDialog();  
  84.     dialog.contact = c;  
  85.   
  86.     if (dialog.ShowDialog()==DialogResult.OK)  
  87.     {  
  88.         updateContact(item, dialog.contact);  
  89.     }  
  90. }  
这样就可以达到c#中对话框中相互交换数据了
posted on 2016-09-06 10:55  莫水千流  阅读(383)  评论(0编辑  收藏  举报