2017-05-14 48 views
0

我以前創建了一個程序,可以將文本文件中的數據顯示到列表視圖中,並且用戶可以在列表視圖中單擊某個名稱,並顯示該名稱的電話號碼。現在我試圖在程序中添加一個新窗體,允許用戶從組合框中選擇一個名稱,然後在文本框中顯示該名稱,並允許用戶更改名稱並將其保存在文本文件中。將方法更改爲以另一種形式訪問的共享方法

Original Program

不管怎麼說,我試圖讓在新的形式訪問的原始程序加載事件,我似乎無法弄清楚如何做到這一點。這裏是我的代碼:

public partial class VendorsDictionary : Form 
{ 
    public VendorsDictionary() 
    { 
     InitializeComponent(); 
    } 

    private Dictionary<string,string> vendorPhones = new Dictionary<string,string>(); 

    public void VendorsDictionary_Load(object sender, EventArgs e) 
    { 
     string currentLine; 
     string[] fields = new string[2]; 
     StreamReader vendorReader = new StreamReader("Vendor.txt"); 

     while (vendorReader.EndOfStream == false) 
     { 
      currentLine = vendorReader.ReadLine(); 
      fields = currentLine.Split(','); 

      vendorPhones.Add(fields[1], fields[6]); 

      string[] name = { fields[1] }; 
      string[] city = { fields[3] }; 
      string[] state = { fields[4] }; 
      string[] zipcode = { fields[5] }; 
      string[] phone = { fields[6] }; 

      for (int i = 0; i < name.Length; i++) 
      { 
       lvDisplay.Items.Add(new ListViewItem(new[] { name[i], city[i], state[i], zipcode[i] })); 
      }  
     } 

     vendorReader.Close(); 
    } 

    private void lvDisplay_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     if (lvDisplay.SelectedItems.Count>0) 
     { 
      ListViewItem item = lvDisplay.SelectedItems[0]; 
      lblName.Text = item.SubItems[0].Text; 
      lblPhone.Text = vendorPhones[item.SubItems[0].Text]; 
     } 
    } 

    private void btnUpdate_Click(object sender, EventArgs e) 
    { 
     UpdateVendor updateVendor = new UpdateVendor(); 
     updateVendor.Show(); 
    } 
} 

我試圖將其更改爲靜態和一些其他的想法我已經在這裏找到,但似乎無法得到它的工作。任何幫助,將不勝感激!

+0

那麼爲什麼你需要'VendorsDictionary_Load'函數呢?考慮到這個功能只會將結果保存到'lvDisplay' – Ben

+0

[How to share forms forms?](http://stackoverflow.com/questions/3800603/how-to-share-data-between-forms) –

+0

在你的'UpdateVendor'表單上創建一個公共屬性(我們稱之爲'public string Name;',並將其設置爲組合框選定項目(名稱),然後使用'updateVendor.ShowDialog();'啓動窗體('ShowDialog ()'會阻止你的'VendorsDictionary'表單執行,直到'updateVendor'表單被關閉,然後你可以檢查'updateVendor.Name'屬性,並且你有用戶選擇的名字 –

回答

1

您不需要VendorsDictionary_Load方法中的所有代碼。你真的可以清理它。我會告訴你如何。在VendorsDictionary_Load

public static IEnumerable<Vendor> LoadVendors() 
{ 
    var vendors = 
     File.ReadAllLines("Vendor.txt").Select(x => x.Split(',')) 
      .Select(x => 
      new Vendor 
      { 
       Name = x[1], 
       City = x[3], 
       State = x[4], 
       ZipCode = x[5], 
       Phone = x[6] 
      }).ToList(); 

    return vendors; 
} 

更改代碼這樣::

這個類添加到您的項目:

public class Vendor 
{ 
    public string City { get; set; } 
    public string Name { get; set; } 
    public string Phone { get; set; } 
    public string State { get; set; } 
    public string ZipCode { get; set; } 
} 

添加此方法VendorsDictionary

public void VendorsDictionary_Load(object sender, EventArgs e) 
{ 
    var vendors = LoadVendors(); 
    foreach (var thisVendor in vendors) 
    { 
     vendorPhones.Add(thisVendor.Name, thisVendor.Phone); 
     lvDisplay.Items 
      .Add(new ListViewItem(new[] { thisVendor.Name, thisVendor.City, 
       thisVendor.State, thisVendor.ZipCode })); 
    } 
} 

使用LoadVendors方法無論你想要什麼:

var someOtherUsage = VendorsDictionary.LoadVendors(); 

要改進此代碼,請發送路徑至LoadVendors,以便您可以從任何位置加載供應商。另外,VendorDictionary不是一個表單的好名字。

相關問題