C# 地址簿教程

C# 地址簿教程,c#,visual-studio-2010,C#,Visual Studio 2010,我一直在和你一起工作。我按照指示进行操作,遇到了两个问题 当我选择Listview框的属性时,事件丢失。我已多次重新启动并刷新程序。我已经直接写信给该文件,但它仍然不存在 从列表中选择联系人时,文本框不会反映所选内容。我怀疑这与ui中没有事件有关。即使我在代码中设置了事件,这种情况仍然存在 我们将不胜感激。我的代码在下面 using System; using System.Collections.Generic; using System.ComponentModel; using Syste

我一直在和你一起工作。我按照指示进行操作,遇到了两个问题

  • 当我选择Listview框的属性时,事件丢失。我已多次重新启动并刷新程序。我已经直接写信给该文件,但它仍然不存在

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

  • 我们将不胜感激。我的代码在下面

    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; }
        }
    }
    
    使用系统;
    使用System.Collections.Generic;
    使用系统组件模型;
    使用系统数据;
    使用系统图;
    使用System.Linq;
    使用系统文本;
    使用System.Windows.Forms;
    使用System.IO;
    使用System.Web;
    名称空间地址簿
    {
    公共部分类通讯簿:表单
    {
    公共通讯录()
    {
    初始化组件();
    }
    列表人员=新列表();
    私有无效地址簿加载(对象发送方,事件参数e)
    {
    字符串路径=Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
    如果(!Directory.Exists(路径+“\\Address Book-Joe”))
    目录.CreateDirectory(路径+“\\Address Book-Joe”);
    如果(!File.Exists(path+“\\Address Book-Joe\\settings.xml”))
    创建((路径+“\\Address Book-Joe\\settings.xml”);
    }
    私有void btnAdd_单击(对象发送者,事件参数e)
    {
    人员p=新人员();
    p、 FirstName=txtFName.Text;
    p、 地址=txtAddress.Text;
    p、 City=txtCity.Text;
    p、 State=comboState.Text;
    p、 ZipCode=txtZip.Text;
    p、 Email=txtEmail.Text;
    p、 PhoneNumber=txtPhone.Text;
    p、 附加=rtxtAdd.Text;
    新增(p);
    listView1.Items.Add(p.FirstName);
    txtFName.Text=“”;
    txtAddress.Text=“”;
    txtCity.Text=“”;
    comboState.Text=“”;
    txtZip.Text=“”;
    txtEmail.Text=“”;
    txtPhone.Text=“”;
    rtxtAdd.Text=“”;
    }
    私有无效列表视图1\u SelectedIndexChanged(对象发送方,事件参数e)
    {
    txtFName.Text=people[listView1.SelectedItems[0].Index].FirstName;
    txtAddress.Text=people[listView1.SelectedItems[0]。Index]。地址;
    txtCity.Text=people[listView1.SelectedItems[0].Index].City;
    comboState.Text=people[listView1.SelectedItems[0]。Index]。状态;
    txtZip.Text=people[listView1.SelectedItems[0].Index].ZipCode;
    txtEmail.Text=people[listView1.SelectedItems[0]。Index]。电子邮件;
    txtPhone.Text=people[listView1.SelectedItems[0]。Index]。电话号码;
    txtZip.Text=people[listView1.SelectedItems[0].Index].ZipCode;
    rtxtAdd.Text=people[listView1.SelectedItems[0].Index]。其他;
    }
    私有void txtFName_TextChanged(对象发送方,事件参数e)
    {
    }
    }
    公共阶层人士
    {
    公共字符串名{get;set;}
    公共字符串地址{get;set;}
    公共字符串City{get;set;}
    公共字符串状态{get;set;}
    公共字符串ZipCode{get;set;}
    公共字符串PhoneNumber{get;set;}
    公共字符串电子邮件{get;set;}
    公共字符串附加{get;set;}
    }
    }
    
    您需要单击属性窗口中的闪电图标以转到事件选项卡。
    在那里,您将看到
    SelectedIndexChanged
    事件(假设您选择了正确的控件)


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

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


    您需要将处理程序绑定到该事件才能运行代码。

    SelectedIndexChanged
    可能会在属性窗口中列为OnSelectedIndexChanged
    一旦事件处理程序正常工作,文本框应按预期填充值。

    SelectedIndexChanged
    可能会在属性窗口中列为OnSelectedIndexChanged
    一旦事件处理程序正常工作,文本框中应按预期填充值。

    No;它不是。这是一个受保护的虚拟方法。No;它不是。这是一个受保护的虚拟方法。@webby68-在该视频的开头,您可以看到闪电已经被选中。好的,谢谢,这正是我需要的。我只是简化了y错过了这么一个小细节。非常感谢。这确实解决了问题。@webby68那么你应该接受SLaks的答案。请看@webby68-在该视频的开头,你可以看到闪电已经被选中。好的,谢谢,这正是我需要的。我只是错过了这么一个小细节。非常感谢。这确实解决了问题。@webby68那么你应该接受SLaks的回答。看到了吗