本例是一个管理联系人信息的小程序,程序有两个窗体,一个主窗体,在listview控件中显示联系人信息列表,一个对话框窗体,用来显示和修改 某个联系人的信息。通过主窗体的菜单命令,可以打开对话框,并把主窗体listview中的当前选中的联系人数据传递给对话框,在对话框中可以对联系人信 息进行修改,修改完毕后,单击确定按钮,主窗体根据用户在对话框中的输入更新listview空间
1.创建一个windows应用程序
2.在项目中添加联系人类。如下
- public class conntactpeople
- {
- private String _name;
- public String name
- {
- get { return _name; }
- set { _name = value; }
- }
- private bool _isFemale=false;
- public bool isFemale
- {
- get { return _isFemale; }
- set { _isFemale = value; }
- }
- private DateTime _dateOfBirth;
- public DateTime dateOfBirth
- {
- get { return _dateOfBirth;}
- set { _dateOfBirth = value;}
- }
- private String _company;
- public String company
- {
- get { return _company; }
- set { _company = value; }
- }
- private String _telephone;
- public String telephone
- {
- get { return _telephone; }
- set { _telephone = value; }
- }
3.添加一个窗体对话框
4.在contactDialog窗体的load时间中,对窗体进行初始化。代码如下
- button1.DialogResult = DialogResult.OK;
- button2.DialogResult = DialogResult.Cancel;
5.在contactDialog类中,添加一个Contact类型的属性,用来与外界交换数据。代码如下:
- private conntactpeople _contact;
- public conntactpeople contact
- {
- get
- {
- conntactpeople c = new conntactpeople();
- c.name = textBox1.Text;
- c.company = textBox2.Text;
- c.dateOfBirth = dateTimePicker1.Value;
- c.telephone = maskedTextBox2.Text;
- c.isFemale = radioButton2.Checked;
- return c;
- }
- set
- {
- textBox1.Text = value.name;
- radioButton2.Checked = value.isFemale;
- radioButton1.Checked = !value.isFemale;
- dateTimePicker1.Value = value.dateOfBirth;
- textBox2.Text = value.company;
- maskedTextBox2.Text = value.telephone;
- }
- }
6.在主船体中添加listview空间,以及ContextMenustrip空间,添加菜单项:添加,删除,修改。并把listview的ContextMenuStrip设置为此菜单。此时,主窗体就可以用Contact类了。
7.在主窗体的load事件添加代码:
- listView1.View = View.Details;
- listView1.GridLines=true;
- listView1.Columns.Add("姓名", 80);
- //listView1.co
- listView1.Columns.Add("性别", 40);
- listView1.Columns.Add("出生日期", 100);
- listView1.Columns.Add("工作单位",160);
- listView1.Columns.Add("联系电话", 100);
- listView1.HideSelection = true;
- listView1.FullRowSelect = true;
- ContactDialog dialog = new ContactDialog();
- dialog.Owner = this;
8.主窗体中的添加,删除,更新按钮中添加相应代码即可
- private void 添加ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- ContactDialog dialog = new ContactDialog();
- // dialog.contact = null;
- if (dialog.ShowDialog()==DialogResult.OK)
- {
- addContact(dialog.contact);
- }
- }
- //添加函数
- private void addContact(conntactpeople c)
- {
- ListViewItem item = listView1.Items.Add(c.name);
- updateContact(item, c);
- }
- //更新函数
- private void updateContact(ListViewItem item,conntactpeople c)
- {
- item.SubItems.Clear();
- //item.SubItems.c
- item.Text = c.name;
- if (c.isFemale)
- {
- item.SubItems.Add("女");
- }
- else
- {
- item.SubItems.Add("男");
- }
- item.SubItems.Add(c.dateOfBirth.ToString("yyyy-MM-dd"));
- item.SubItems.Add(c.company);
- item.SubItems.Add(c.telephone);
- }
- private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- if (listView1.SelectedIndices.Count>0)
- {
- if (MessageBox.Show("ni"+listView1.Items[listView1.SelectedIndices[0]].Text+"ma?","dd",MessageBoxButtons.YesNoCancel)==DialogResult.Yes)
- {
- listView1.Items.RemoveAt(listView1.SelectedIndices[0]);
- // listView1.Items.RemoveAt(listView1.SelectedIndices[])
- }
- }
- }
- private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- if (listView1.SelectedIndices.Count==0)
- {
- MessageBox.Show("请选择要修改的人");
- return;
- }
- conntactpeople c = new conntactpeople();
- ListViewItem item = listView1.SelectedItems[0];
- c.name = item.Text;
- //item.SubItems[];
- c.isFemale=(item.SubItems[1].Text=="女");
- try
- {
- c.dateOfBirth = DateTime.Parse(item.SubItems[2].Text.ToString());
- }
- catch
- {
- System.Diagnostics.Trace.Write(c.dateOfBirth);
- MessageBox.Show(item.SubItems[2].Text);
- //System.Diagnostics.Trace(item.SubItems[1].Text);
- }
- c.company = item.SubItems[3].Text;
- c.telephone = item.SubItems[4].Text;
- ContactDialog dialog = new ContactDialog();
- dialog.contact = c;
- if (dialog.ShowDialog()==DialogResult.OK)
- {
- updateContact(item, dialog.contact);
- }
- }
这样就可以达到c#中对话框中相互交换数据了
本文转自莫水千流博客园博客,原文链接:http://www.cnblogs.com/zhoug2020/p/5844901.html,如需转载请自行联系原作者