[英]How to update a ComboBox from a window when closing another window?
首先,我要道歉:
就是說,讓我們開始討論這個問題。
我有這個窗口:
如果我單擊標記為紅色的按鈕:
這將打開:
這應該是針對市場的軟件。 第一個窗口負責向庫存訂購更多物品。 第二個窗口負責將供應商添加到系統中。
組合框顯示系統上的所有供應商。 我想要在單擊以紅色矩形突出顯示的按鈕后在第二個窗口中添加供應商時,組合框將使用新數據自動更新。
我在此代碼中使用了“更新”按鈕:
this.tb_FornecedorTableAdapter.Fill(this.tccDataSet.tb_Fornecedor);
它起作用了,但是我嘗試在其他窗口上使用FormClosing
, FormClosed
和Deactivate
事件,但是它根本不起作用(我將代碼上的“ this”修改為很多,但這對我沒有幫助) 。 有什么方法可以做我想要的嗎?
在第一個窗口中聲明一個公共方法:
public void RefreshCombo()
{
this.tb_FornecedorTableAdapter.Fill(this.tccDataSet.tb_Fornecedor);
}
然后在第一個窗口中添加按鈕單擊事件
WindowB window=new WindowB(this);
WindowB.Show();
然后在子窗口中添加ctor方法:
private WindowA windowParent;
public WindowB(WindowA parent)
{
InitializeComponent();
this.windowParent=parent;
}
在WindowB FormClosing事件中
this.windowParent.RefreshCombo()
如果ComboBox已使用SQL Server中的數據更新,則可以嘗試以下操作:
// When button Adicionar is clicked
private void buttonAdd_Click(object sender, EventArgs e)
{
using(Form formAdd = new Form()) // This is the Gerenciar Fornecedor form
{
formAdd.ShowDialog(this); // Show the form. The next statement will not be executed until formAdd is closed
// Put the your code to update the ComboBox items here
}
}
在這種情況下,您可以做的是在子窗體上添加一個屬性來存儲組合框值,並在組合框值更改時填充它。 此外,在子窗體上創建一個將從父窗體調用的方法。 它將顯示子窗體並返回組合框值。
public partial class ChildForm : Form
{
public ChildForm()
{
InitializeComponent();
}
private string _comboValue { get; set; }
public string ShowAndGetComboValue()
{
this.ShowDialog();
return _comboValue;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
_comboValue = comboBox1.SelectedItem.ToString();
}
}
然后,可以在父表單上以這種方式顯示子表單:
ChildForm form = new ChildForm();
string comboValue = form.ShowAndGetComboValue();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.