当单击另一种表单中的按钮提交时,如何在 c# 中重新加载表单?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15896135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to reload form in c# when button submit in another form is click?
提问by Jayseer
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
. When I click the button button1_Click
I want to reload the frmMain
for the new added product will be visible.
我的 C# 中有一个组合框,它位于名为 name 的表单中frmMain
,当我button1_Click
在名为frmSettings
. 当我单击按钮时,button1_Click
我想重新加载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!
这是窗体!
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. 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.
这是我的设置frmSettings
表单的样子。因此,如您所见,当我单击提交按钮时,我想让frmMain
重新加载,以便我添加到设置中的更新值对组合frmMain
框可见。
采纳答案by DerApe
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.
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...
然后,您可以UpdateForm()
从表单上的任何地方调用,例如另一个按钮。此示例仅使用一个局部变量来存储您的产品。还缺少添加产品的某些检查,但我想你明白了......
回答by Nahum
回答by Shaharyar
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
}
回答by Pir Fahim Shah
Try out this code.
试试这个代码。
this.Refresh();
Application.Doevents();
回答by pali
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. Then Call invalidiate tab control and refresh it.
如果您希望从 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
回答by user5553449
this.Refresh();
Refresh();
this.Hide();
frmScholars ss = new frmScholars();
ss.Show();
回答by Kiran
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.
您也可以Invalidate()
像@Nahum 所说的那样尝试表格。
回答by Teja Reddy
this.Close();
frmMain main = new frmMain();
main.Show();