2017-03-18 48 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;