繁体   English   中英

c#点击其他表单提交按钮时,如何重新加载表单?

[英]How to reload form in c# when button submit in another form is click?

我的 C# 中有一个组合框,它位于名为frmMain的表单中,当我在名为frmSettings的设置表单中添加(使用按钮button1_Click )产品时,它会自动填充。 当我单击按钮button1_Click I want to reload frmMain时,新添加的产品将可见。

我尝试使用

frmMain main = new frmMain();
main.Close();
main.Show();

我知道这段代码很有趣,但它没有用。 :D

这是windows表格!

编辑

请查看我的程序的这张图片以便更好地理解。 这是我的frmMain在此处输入图像描述

这是我的设置frmSettings表单的样子。 所以,正如您看到的,当我单击提交按钮时,我想让frmMain重新加载,这样我添加到设置中的更新值将对frmMain comboBox 可见。

在此处输入图像描述

更新:由于您更改了您的问题,这里是更新您的产品的更新版本

这是您的产品形式:

private frmMain main;

public frmSettings(frmMain mainForm)
{
  main = mainForm;
  InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
  main.AddProduct(textBox1.Text);
}

它将需要构造函数中的 mainform 将数据传递给它。

和主要形式:

private frmSettings settings;
private List<string> products = new List<string>();

public frmMain()
{
  InitializeComponent();
  //load products from somewhere
}

private void button1_Click(object sender, EventArgs e)
{
  if (settings == null)
  {
    settings = new frmSettings(this);
  }
  settings.Show();
}

private void UpdateForm()
{
  comboBoxProducts.Items.Clear();
  comboBoxProducts.Items.AddRange(products.ToArray());

  //Other updates
}

public void AddProduct(string product)
{
  products.Add(product);
  UpdateForm();
}

然后,您可以从表单上的任何地方调用UpdateForm() ,例如另一个按钮。 此示例仅使用一个局部变量来存储您的产品。 还缺少添加产品的某些检查,但我想你明白了......

没有这样的内置方法可以根据需要设置所有值。 正如我在评论中提到的,您应该使用所有控件的所需设置创建一个方法,这是示例代码:

private void ReloadForm()
{
    comboBox.ResetText();
    dataGridView.Update();   
    //and how many controls or settings you want, just add them here
}

private void button1_Click(object sender, EventArgs e)
{
    ReloadForm();   //and call that method on your button click
}

试试这个代码。

this.Refresh();
Application.Doevents();
this.Refresh();
Refresh();
this.Hide();
frmScholars ss = new frmScholars();
ss.Show();
this.Close();
frmMain main = new frmMain();
main.Show();

如果您希望从 usercontrol 刷新页面。这里是我从 usercontrol 刷新表单的示例 查找此重新加载按钮所在的表单。 然后调用无效选项卡控件并刷新它。

Dim myForm As Form = btnAuthorise.FindForm()

For Each c As Control In myForm.Controls
                If c.Name = "tabControlName" Then
                    DirectCast(c, System.Windows.Forms.TabControl).Invalidate()
                    DirectCast(c, System.Windows.Forms.TabControl).Refresh() 'force the call to the drawitem event
                End If
 Next

不需要重新加载整个表单。 只需为表单初始化创建一个函数。 您可以随时调用此函数。 这将刷新表单。

private void acc_Load(object sender, EventArgs e)
{
     form_Load();           
}

public void form_Load()
{
    // write form initialise codes example listView1.Clear();...
}

private void button1_Click(object sender, EventArgs e) //edit account
{
    //Do something then refresh form
    form_Load(); 

}

如果你想在你点击另一个表单的按钮时自动更新另一个表单的值,你可以使用定时器控件。 只需将计时器设置为 0.5 秒即可快速更新表单

我认为,通过在单击按钮时调用frmMain_load(sender,e)应该重新加载表单。

您也可以像@Nahum 所说的那样尝试Invalidate()表单。

暂无
暂无

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

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