从另一种形式刷新组合框

时间:2017-03-18 07:55:22

标签: c# .net combobox

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

    }
}

2 个答案:

答案 0 :(得分:2)

这是一个经典问题

这些行不符合您的想法

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

此处创建了{strong> NEW 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();
}

答案 1 :(得分:0)

尝试设置为null,然后分配新源

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