地址簿教程

时间:2012-10-15 16:38:01

标签: c# visual-studio-2010

我一直在与a c# tutorial for beginners to create an address book合作。我按照说明操作,遇到了两个问题。

  1. 当我选择Listview框的属性时,SelectedIndexChanged事件丢失。我重新启动并刷新了几次程序。我已直接写入文件,但仍然没有。

  2. 从列表中选择联系人时,文本框不会反映所选内容。我怀疑这与ui中缺少SelectedIndexChanged事件有关。即使我在代码中设置了SelectedIndexChanged事件,这种情况仍然存在。

  3. 非常感谢所有帮助。我的代码在

    之下
    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.IO;
    using System.Web;
    
    
    namespace AddressBook
    {
        public partial class AddressBook : Form
        {
            public AddressBook()
            {
                InitializeComponent();
            }
    
            List<Person> people = new List<Person>();
    
            private void AddressBook_Load(object sender, EventArgs e)
            {
                string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
                if (!Directory.Exists(path + "\\Address Book - Joe"))
                    Directory.CreateDirectory(path + "\\Address Book - Joe");
                if (!File.Exists(path + "\\Address Book - Joe\\settings.xml"))
                    File.Create((path + "\\Address Book - Joe\\settings.xml"));
            }
    
            private void btnAdd_Click(object sender, EventArgs e)
            {
                Person p = new Person();
                p.FirstName = txtFName.Text;
                p.Address = txtAddress.Text;
                p.City = txtCity.Text;
                p.State = comboState.Text;
                p.ZipCode = txtZip.Text;
                p.Email = txtEmail.Text;
                p.PhoneNumber = txtPhone.Text;
                p.Additional = rtxtAdd.Text;
                people.Add(p);
                listView1.Items.Add(p.FirstName);
                txtFName.Text = "";
                txtAddress.Text = "";
                txtCity.Text = "";
                comboState.Text = "";
                txtZip.Text = "";
                txtEmail.Text = "";
                txtPhone.Text = "";
                rtxtAdd.Text = "";
    
            }
    
            private void listView1_SelectedIndexChanged(object sender, EventArgs e)
            {
                txtFName.Text = people[listView1.SelectedItems[0].Index].FirstName;
                txtAddress.Text = people[listView1.SelectedItems[0].Index].Address;
                txtCity.Text = people[listView1.SelectedItems[0].Index].City;
                comboState.Text = people[listView1.SelectedItems[0].Index].State;
                txtZip.Text = people[listView1.SelectedItems[0].Index].ZipCode;
                txtEmail.Text = people[listView1.SelectedItems[0].Index].Email;
                txtPhone.Text = people[listView1.SelectedItems[0].Index].PhoneNumber;
                txtZip.Text = people[listView1.SelectedItems[0].Index].ZipCode;
                rtxtAdd.Text = people[listView1.SelectedItems[0].Index].Additional;
    
            }
    
            private void txtFName_TextChanged(object sender, EventArgs e)
            {
    
            }
        }
    
        public class Person
        {
            public string FirstName { get; set; }
            public string Address { get; set; }
            public string City { get; set; }
            public string State { get; set; }
            public string ZipCode { get; set; }
            public string PhoneNumber { get; set; }
            public string Email { get; set; }
            public string Additional { get; set; }
        }
    }
    

2 个答案:

答案 0 :(得分:5)

您需要单击“属性”窗口中的闪电图标以转到“事件”选项卡 在那里,您将看到SelectedIndexChanged事件(假设您选择了正确的控件。

您需要将处理程序绑定到该事件,以便运行代码。

答案 1 :(得分:0)

SelectedIndexChanged可能会在“属性”窗口中列为 OnSelectedIndexChanged 。 一旦你让事件处理程序工作,就应该按照预期的值填充文本框。