简体   繁体   English

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

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

I have a combo box in my C# which is place in form named frmMain which is automatically fill when I add (using button button1_Click ) a product in my settings form named frmSettings .我的 C# 中有一个组合框,它位于名为frmMain的表单中,当我在名为frmSettings的设置表单中添加(使用按钮button1_Click )产品时,它会自动填充。 When I click the button button1_Click I want to reload the frmMain for the new added product will be visible.当我单击按钮button1_Click I want to reload frmMain时,新添加的产品将可见。

I tried using我尝试使用

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

I know this code is so funny but it didn't work.我知道这段代码很有趣,但它没有用。 :D :D

This is windows form!这是windows表格!

EDIT编辑

Please see this image of my program for better understanding.请查看我的程序的这张图片以便更好地理解。 This is my frmMain这是我的frmMain在此处输入图像描述

Here is what my settings frmSettings form look like.这是我的设置frmSettings表单的样子。 So, as you can see when I click the submit button I want to make the frmMain to reload so that the updated value which I added to the settings will be visible to frmMain comboBox.所以,正如您看到的,当我单击提交按钮时,我想让frmMain重新加载,这样我添加到设置中的更新值将对frmMain comboBox 可见。

在此处输入图像描述

Update: Since you changed your question here is the updated version to update your products更新:由于您更改了您的问题,这里是更新您的产品的更新版本

This is your products form:这是您的产品形式:

private frmMain main;

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

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

It will need the mainform in the constructor to pass the data to it.它将需要构造函数中的 mainform 将数据传递给它。

And the main form:和主要形式:

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();
}

You then can call UpdateForm() from everywhere on you form, another button for example.然后,您可以从表单上的任何地方调用UpdateForm() ,例如另一个按钮。 This example uses just a local variable to store your products.此示例仅使用一个局部变量来存储您的产品。 There are also missing certain checks for adding a product, but I guess you get the idea...还缺少添加产品的某些检查,但我想你明白了......

There is no such built in method to set all your values as you desire.没有这样的内置方法可以根据需要设置所有值。 As i mentioned in the comment that you should create a method with your required settings of all controls, here is the sample code:正如我在评论中提到的,您应该使用所有控件的所需设置创建一个方法,这是示例代码:

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
}

Try out this code.试试这个代码。

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

IF you are looking to refresh page from usercontrol .Here is expample where i amrefreshing form from usercontrol Find the form in which this reload button is.如果您希望从 usercontrol 刷新页面。这里是我从 usercontrol 刷新表单的示例 查找此重新加载按钮所在的表单。 Then Call invalidiate tab control and refresh it.然后调用无效选项卡控件并刷新它。

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

Not required reload for entire form.不需要重新加载整个表单。 Just create a function for form initialise.只需为表单初始化创建一个函数。 you can call this function any time.您可以随时调用此函数。 This will refresh the form.这将刷新表单。

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(); 

}

If you want to automatically update the value of the other form when you click the button from another one you can use timer control.如果你想在你点击另一个表单的按钮时自动更新另一个表单的值,你可以使用定时器控件。 Just set the timer to 0.5s in order to update the form fast只需将计时器设置为 0.5 秒即可快速更新表单

I think that, by calling the frmMain_load(sender,e) when you are clicking the button should reload the form.我认为,通过在单击按钮时调用frmMain_load(sender,e)应该重新加载表单。

You may also try to Invalidate() the form just like @Nahum said.您也可以像@Nahum 所说的那样尝试Invalidate()表单。

暂无
暂无

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

相关问题 C#WinForm:提交另一个表单后重新加载表单? - C# WinForm : Reload form after submit another form? 单击按钮通过另一个表单重新加载表单 - Reload a Form through another Form by Button click 单击C#和jQuery的提交按钮时,表单不提交 - Form does not submit when I click submit button of it C# & jQuery 如何单击按钮或提交表单或单击URL或单击C#4.0 WPF Webbrowser应用程序上的图像 - How to click button or submit form or click url or click image on C# 4.0 WPF webbrowser application 在 C# 中,当我单击第二个表单的按钮时,如何在第一个表单中打开第三个表单 - In C# how to open third form inside first form when i click a button of the second form 当我单击 C# Windows 表单中的按钮时,如何从来自文本框的另一个 class 中获取值? - how can I reach the value from another class coming from textbox when I click the button in C# Windows form? 如何检查按钮是否在C#中的Windows窗体应用程序中的另一个按钮单击事件内被单击 - how to check whether a button is clicked, inside another button click event in windows form application in C# 单击按钮后,以另一种形式刷新标签文本 - refresh label text in another form after button click C# C# 处理对话框的按钮单击另一种形式 - C# Handle a dialog's button click in another form 在按钮上单击以其他窗口形式显示面板c# - Displaying a panel in another windows form on button click c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM