C# 从另一个窗体刷新组合框

C# 从另一个窗体刷新组合框,c#,.net,combobox,C#,.net,Combobox,在我的组合框中,我显示了一些主机名,如果我想在组合框中添加另一个主机,我会打开一个新表单来添加它,当我点击save host(保存主机)按钮时,新主机会写入一个txt文件,然后我会运行一个方法,加载组合框中txt文件中保存的所有主机,问题是,在运行我的方法后,组合框没有刷新 这是我的保存主机方法 private void btnSaveHost_Click(object sender, EventArgs e) { if (textAlias.Text.Trim().Length >

在我的组合框中,我显示了一些主机名,如果我想在组合框中添加另一个主机,我会打开一个新表单来添加它,当我点击save host(保存主机)按钮时,新主机会写入一个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"; 

    }
}
public void loadHosts()
{
列表主机=新列表();
如果(File.Exists(“C:\\MCDFC\\Hosts.txt”))
{
字符串[]行=System.IO.File.ReadAllLines(@“C:\\MCDFC\\Hosts.txt”);

对于(int x=0;x尝试设置为null,然后分配新源

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

这是一个经典问题

这些台词不符合你的想法

mainForm mf = new mainForm();
mf.loadHosts();
这里创建了
mainForm
NEW实例,并为该实例调用
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();
}

与您的问题无关,但究竟为什么只为了避免一个局部变量和一个代码行而调用Split两次呢?千万不要试图变得聪明,有时你会失去理智。你在哪里调用AddHost表单?你应该在那里放置刷新逻辑