2017-03-18 46 views
1

sdfdfdfdf從另一種形式刷新組合框

在我的組合框中我展示一些主機名,如果我要到另一臺主機添加到組合框,我打開一個新的形式來添加它,當我點擊保存主機按鈕新主機是寫在一個txt文件,然後我運行一個方法,加載保存在組合框中的txt文件中的所有主機,問題是運行我的方法後組合框不刷新。

這是我的存檔主機方法。

private void btnSaveHost_Click(object sender, EventArgs e) 
{ 
    if (textAlias.Text.Trim().Length > 0 && textHost.Text.Trim().Length > 0) 
    { 
     if (!Directory.Exists("C:\\MCDFC")) 
     { 
      Directory.CreateDirectory("C:\\MCDFC"); 

     } 

     try 
     { 
      System.IO.StreamWriter file = new System.IO.StreamWriter("C:\\MCDFC\\Hosts.txt", true); 
      file.WriteLine(textAlias.Text + "#" + textHost.Text); 
      file.Close(); 
      file.Dispose(); 
      MessageBox.Show("Host saved", "Notification", MessageBoxButtons.OK, MessageBoxIcon.Information); 
      textAlias.Text = ""; 
      textHost.Text = ""; 
      mainForm mf = new mainForm(); 
      mf.loadHosts(); 
     } 
     catch(Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 
    } 
    else 
     MessageBox.Show("One or both fields are empty", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); 

} 

這是刷新組合框的方法:

public void loadHosts() 
{ 
    List<host> hosts = new List<host>(); 
    if (File.Exists("C:\\MCDFC\\Hosts.txt")) 
    { 
     string[] lines = System.IO.File.ReadAllLines(@"C:\\MCDFC\\Hosts.txt"); 

     for(int x =0;x<lines.Length;x++) 
     { 
      hosts.Add(new host(lines[x].Split('#')[0], lines[x].Split('#')[1])); 
     } 

     cmbHosts.DataSource = hosts; 
     cmbHosts.DisplayMember = "aliasName"; 
     cmbHosts.ValueMember = "hostName"; 

    } 
} 
+0

不相關的問題裏面,但爲什麼在地球上的呼叫後,爲了避免一個局部變量和一個單獨的代碼行分開兩次?不要試圖不惜一切代價聰明,有時你會灼傷你的手指。 – Steve

+0

你在哪裏調用AddHost表單?那裏你應該把刷新邏輯 –

回答

3

這是一個經典的問題

這些行不符合你的想法

mainForm mf = new mainForm(); 
mf.loadHosts(); 

這裏有一個新建mainForm的實例已創建,您可以爲該實例調用loadHost方法。受該調用影響的組合是新實例擁有的組合,而不是第一個實例mainForm上可見的組合。
當然,新實例是隱藏的(你永遠不會調用Show),所以你什麼都看不到。

要解決此問題,您應該使用事件通知或將mainForm的第一個實例傳遞給您的addHost表單。 (我不知道第二個表格的確切名稱,所以我將在下面的示例中將其命名爲addHost,將其更改爲真實姓名)。

我會告訴你如何使用事件通知,因爲我認爲比傳遞第一個實例更面向對象。

首先在全局級別的addHost表單中聲明一個事件。

public class addHost: Form 
{ 
    public delegate void OnAddHost(string host, string alias) 
    public event OnAddHost HostAdded; 
    .... 

現在在按鈕中單擊事件如果某個外部客戶端已聲明其感興趣訂閱事件通知,則引發此事件。

.... 
// Side note: using statement is the preferred way to handle disposable resources 
// You don't need to call Close and Dispose, it is done automatically at the end of the using block 
// The compiler add the required code and works also in case of exceptions 
using(StreamWriter file = new System.IO.StreamWriter("C:\\MCDFC\\Hosts.txt", true)) 
{ 
    file.WriteLine(textAlias.Text + "#" + textHost.Text); 
} 
MessageBox.Show("Host saved", "Notification", MessageBoxButtons.OK, MessageBoxIcon.Information); 
textAlias.Text = ""; 
textHost.Text = ""; 

// If someone subscribe to the event it will receive it... 
HostAdded?.Invoke(textAlias.Text, textHost.Text); 
.... 

最後,在你的MainForm,剛剛創建addHost實例設置的事件到事件處理程序代碼的MainForm

// These lines goes where you open the addHost form 
using(frmAddHost fa = new frmAddHost()) 
{ 
    fa.HostAdded += hostAddedHandler; 
    fa.ShowDialog(); 
} 
... 
private void hostAddedHandler(string host, string alias) 
{ 
    // I call loadHost but you can look at what has been added 
    loadHosts(); 
} 
0

嘗試設置爲null,然後分配新中源,

cmbHosts.DataSource = null; 
    cmbHosts.DataSource = hosts;