单击按钮提交时如何在c#中重新加载表单?

时间:2013-04-09 07:44:03

标签: c# forms button reload

我的C#中有一个组合框,其名称为frmMain,当我在名为button1_Click的设置表单中添加(使用按钮frmSettings)产品时自动填充。当我点击按钮button1_Click时,我想重新加载frmMain,以便新添加的产品可见。

我尝试使用

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

我知道这段代码很有趣但是没用。 :d

这是Windows窗体!

修改

请参阅我的计划的这张图片以便更好地理解。 这是我frmMain enter image description here

以下是我的设置frmSettings表单的外观。因此,正如您所看到的,当我单击提交按钮时,我想让frmMain重新加载,以便我添加到设置中的更新值对frmMain组合框可见。

enter image description here

8 个答案:

答案 0 :(得分:5)

更新:由于您在此处更改了问题,因此更新版本是更新产品

这是您的产品形式:

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(),例如另一个按钮。 此示例仅使用本地变量来存储您的产品。还有一些关于添加产品的检查缺失,但我想你明白了......

答案 1 :(得分:1)

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

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
}

答案 2 :(得分:1)

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

答案 3 :(得分:0)

答案 4 :(得分:0)

试试这段代码。

this.Refresh();
Application.Doevents();

答案 5 :(得分:0)

如果您正在寻找从usercontrol刷新页面。这是expample我在哪里从usercontrol重新刷新表单 找到此重新加载按钮的表单。 然后调用invalidiate tab control并刷新它。

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

答案 6 :(得分:0)

this.Refresh();
Refresh();
this.Hide();
frmScholars ss = new frmScholars();
ss.Show();

答案 7 :(得分:-1)

我认为,当您点击按钮时调用frmMain_load(sender,e)应重新加载表单。

您也可以尝试Invalidate()表格,就像@Nahum说的那样。