简体   繁体   English

如何以一种形式从另一种形式重新加载组合框项目

[英]How to reload combobox items in one form from another form

I have two window forms, Form1 is for country name and Form2 is for city name. 我有两个窗口窗体,Form1用于国家名称,Form2用于城市名称。 In form2 I have a comboBox which fetch country name from database that I saved by Form1 and its working fine and fetching data properly. 在form2中,我有一个comboBox,它可以从由Form1保存的数据库中获取国家/地区名称,并且可以正常工作并正确获取数据。 But I want a button beside comboBox like please see this image for better understanding , and when click on it button will open Form1 and when I will add a new country in Form1, comboBox in Form2 should get updated and Form1 should close. 但是我想在comboBox旁边添加一个按钮, 如需更好地理解此图像 ,单击该按钮将打开Form1,并且当我在Form1中添加新的国家/地区时,Form2中的comboBox应该更新,而Form1应该关闭。 How to do this? 这个怎么做? My code is... 我的代码是...

In Form2 (for city name) 在Form2中(用于城市名称)

private void addBtn_Click(object sender, EventArgs e)
    {
        Add_Country ac = new Add_Country();
        ac.ShowDialog();
    }

public void refreshComboBox()
    {
        comboBox_CountryName.Refresh();
    }

In Form1(for country name) 在Form1中(用于国家/地区名称)

private void saveBtn_Click(object sender, EventArgs e)
    {
        string country = txtBox_countryName.Text.ToLower();
        insertCountry(country);
        showCountry();
        Add_City ad = new Add_City(); //Form2 object refrence.
        ad.refreshComboBox();
        this.Close();
        MessageBox.Show("Country added successfully.");
        clearControl();
     }

The line ac.ShowDialog() helps you handle after the Add_Country form is closed. ac.ShowDialog()可帮助您在Add_Country表单关闭后进行处理。 So, you can refetch the countries from database just after ShowDialog line such as; 因此,您可以在ShowDialog行之后从database refetch countries ,例如;

private void addBtn_Click(object sender, EventArgs e)
{
    Add_Country ac = new Add_Country();
    ac.ShowDialog();
    // Repopulate the Country Combobox 
}

in form2 after showdialog() refresh your combo, you don't need to refresh it in the first form 在showdialog()刷新组合后,在form2中,您无需以第一种形式刷新它

private void addBtn_Click(object sender, EventArgs e)
{
    Add_Country ac = new Add_Country();
    ac.ShowDialog();
    refreshComboBox()
}

when you are closing the dialog the run time cursor will be back to the form2 and the other story will be executed 当您关闭对话框时,运行时光标将返回到form2,并且将执行其他操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM