2012-10-15 152 views
-1

我一直在使用a c# tutorial for beginners to create an address book。我遵循指示,我遇到了兩個問題。地址簿教程

  1. 當我選擇列表視圖框的屬性時,SelectedIndexChanged事件丟失。我重新啓動並刷新了該程序數次。我已經直接寫了文件,但它仍然不存在。

  2. 從列表中選擇一個聯繫人時,文本框不反映選擇。我懷疑這與ui中SelectedIndexChanged事件的缺失有關。即使我在代碼中設置了SelectedIndexChanged事件,這仍然存在。

所有幫助將不勝感激。我的代碼是低於

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; } 
    } 
} 

回答

5

您需要單擊屬性窗口中的閃電螺栓圖標才能轉到事件選項卡。
在那裏,你將看到SelectedIndexChanged事件(假設你選擇了正確的控制。

您需要將處理該事件綁定,以便您的代碼運行。

+0

@ webby68 - 。在那個視頻的開始,你可以看到閃電已經被選中。 – Bobson

+0

好的,謝謝正是我需要的。我只是錯過了這樣一個小細節。非常感謝。這確實解決了這個問題。 – webby68

+0

@ webby68然後你應該接受SLaks的回答。請參閱http://stackoverflow.com/faq#howtoask –

0

的SelectedIndexChanged可能會在屬性上市窗口OnSelectedIndexChanged 一旦你得到該事件處理工作,文本框應與價值預期來填充

+0

否;它不是。這是一個受保護的虛擬方法。 – SLaks