我有一个父表单,其中包含1个方法来刷新名为 resetPanel()的面板内容。 我在父表单中也有一个按钮。如果单击该按钮,将打开一个新表单。我会做一些更改,然后点击保存。内容将保存在数据库中,子窗体将关闭。现在将显示父表单。
我想立即调用resetPanel()方法,以便面板显示更新的值。 我怎样才能做到这一点?
答案 0 :(得分:4)
如果您的子表单是对话框,则只需检查表单的对话框结果:
// Do not forget to release resources acquired:
// wrap IDisposable into using(..) {...}
using (Form myChildForm = new MyChildForm()) {
//TODO: If you want to pass something from the main form to child one: do it
// On any result save "Cancel" update the panel
if (myChildForm.ShowDialog() != DialogResult.Cancel)
resetPanel();
}
如果您的孩子不 对话,您可以将this
作为参考传递给子表单:
Form myChildForm = new MyChildForm(this);
myChildForm.Show(); // <- Just show, not ShowDialog()
...
private MyMainForm m_MainForm;
public MyChildForm(MyMainForm form) {
m_MainForm = form;
}
private void save() {
//TODO: Save to database here
// Main form update
if (!Object.ReferenceEquals(null, m_MainForm))
m_MainForm.resetPanel(); // <- resetPanel should be public or internal method
}
private saveButton_Click(object sender, EventArgs e) {
save();
}
答案 1 :(得分:1)
关闭Form2
后,您可以调用ResetPanel方法:
Form2 f2 = new Form2();
f2.ShowDialog();
resetPanel(); // <-- this will be executed when you close the second form
答案 2 :(得分:0)
表示例如你的父表单名称如果表单Form1和子表单名称作为Form2转到ur子表单设计器页面将其更改为访问修饰符
以及您希望调用的方法
Form2 f2=new Form2();
f2.Show();
.//from here on you can write your concerned code
答案 3 :(得分:0)
如果您的resetPanel
方法正在进行数据库调用,那么您很可能会避免它。虽然这不会获得应用程序中其他用户正在更新的任何数据。只需根据您的需要修改我的另一个answer代码。这只是一个样本:
public class ParentForm : Form
{
Button openButton = new Button();
public ParentForm()
{
openButton.Click += openButton_Click;
}
void openButton_Click(object sender, EventArgs e)
{
ChildForm childForm = new ChildForm();
childForm.OKButtonClick += childForm_OKButtonClick;
childForm.ShowDialog();
}
void childForm_OKButtonClick(object sender, MyEventArgs e)
{
// Use properties from event args and set data in this form
}
}
public class ChildForm : Form
{
Button okButton = new Button();
TextBox name = new TextBox();
TextBox address = new TextBox();
public event EventHandler<MyEventArgs> OKButtonClick;
public ChildForm()
{
okButton.Click += okButton_Click;
}
void okButton_Click(object sender, EventArgs e)
{
try
{
bool saveSucceeded = false;
// Try saving data here
if (saveSucceeded)
{
if (OKButtonClick != null)
{
MyEventArgs myEventArgs = new MyEventArgs();
// Just get updated data from screen and send it to another form
myEventArgs.Name = name.Text;
myEventArgs.Address = address.Text;
OKButtonClick(sender, myEventArgs);
}
Close();
}
else
{
MessageBox.Show("Data could not be saved.");
}
}
catch (Exception ex)
{
// Perform proper exception handling
}
}
}
public class MyEventArgs : EventArgs
{
public string Name
{
get;
set;
}
public string Address
{
get;
set;
}
}
答案 4 :(得分:0)
尝试将Save
中的child Form
按钮设置为DialogResult.Ok
,然后将“保存”按钮设置为子表单的AcceptButton
。然后测试结果是否是用户按下保存按钮。以编程方式它看起来像这样:
Form2 chidForm = new Form2();
childForm.btnSave.DialogResult = DialogResult.Ok
childForm.AcceptButton = childForm.btnSave
if (childForm.ShowDialog() == DialogResult.Ok)
{
resetPanel();
}
现在,我假设Save
中的Child Form
按钮名为btnSave
。