首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通讯录教程

通讯录教程
EN

Stack Overflow用户
提问于 2012-10-15 16:38:01
回答 2查看 5.7K关注 0票数 0

我一直在和初学者创建地址簿的c#教程合作。我遵照指示,遇到了两个问题。

  1. 当我选择Listview框的属性时,缺少SelectedIndexChanged事件。我已经重新启动和更新了这个程序好几次。我直接给文件写了信,但它仍然不存在。
  2. 从列表中选择联系人时,文本框不反映所选内容。我怀疑这与ui中缺少SelectedIndexChanged事件有关。即使在代码中设置SelectedIndexChanged事件时,这种情况仍然存在。

所有的帮助都将不胜感激。我的代码在下面

代码语言:javascript
复制
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; }
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-10-15 16:39:51

您需要单击“属性”窗口中的闪电图标才能转到Events选项卡。

在那里,您将看到SelectedIndexChanged事件(假设您选择了正确的控件)。

为了运行代码,需要将处理程序绑定到该事件。

票数 5
EN

Stack Overflow用户

发布于 2012-10-15 16:40:08

属性窗口中的OnSelectedIndexChanged.可能会列出SelectedIndexChanged一旦使事件处理程序正常工作,文本框应按预期的方式填充该值。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12900082

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档