[英]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();
you want to Invalidate the form你想让表单无效
http://msdn.microsoft.com/en-us/library/598t492a.aspx http://msdn.microsoft.com/en-us/library/598t492a.aspx
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.