C# 如何重新加载或刷新 Windows 窗体到原始状态?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19760945/
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 or refresh Windows Form into original state?
提问by Michael Vincent Montero
How to reload or refresh the Windows Form into original state? i have used this.Refresh();,this.Invalidate();,form.Refresh(),form.Invalidate()
如何重新加载或刷新 Windows 窗体到原始状态?我用过 this.Refresh();,this.Invalidate();,form.Refresh(),form.Invalidate()
private void AdduserBtn_Click_1(object sender, EventArgs e)
{
UserManagement obj = new UserManagement ();
obj.CourseCategoryId = (int) CourseRegCbox.SelectedValue;
obj.IDNumber = IDNumberTbox.Text;
obj.Password = PasswordRegTbox.Text;
obj.FName = FnameRegTbox.Text;
obj.LName = LnameRegTbox.Text;
obj.Gender = GenderTbox.Text;
obj.Email = EmailRegTbox.Text;
obj.PhoneNumber = PhonenumberRegTbox.Text;
obj.Address = AddressRegTbox.Text;
if ( UserManagement != null && UserManagement.Id > 0 )
{
obj.Id = UserManagement.Id;
if ( UserManagement.UserInfo_Update (obj) > 0 )
{
MessageBox.Show ("Record Succesfully Updated!");
UserInfoForm form = new UserInfoForm ();
form.Refresh ();
}
else
{
MessageBox.Show ("An error occured!");
}
}
else
{
if ( UserManagement.UserInfo_Insert (obj) > 0 )
{
MessageBox.Show ("Record Succesfully Added!");
UserInfoForm form = new UserInfoForm ();
form.Refresh ();
}
else
{
MessageBox.Show ("An error occured!");
}
}
}
I just want to reload the form into original state once the data properly save or updated.
一旦数据正确保存或更新,我只想将表单重新加载到原始状态。
采纳答案by Stephen Byrne
"this.Refresh();,this.Invalidate();,form.Refresh(),form.Invalidate()"
"this.Refresh();,this.Invalidate();,form.Refresh(),form.Invalidate()"
These functions just tell the window manager to redraw the form graphic; they have nothing to do with the state of the form's data.
这些函数只是告诉窗口管理器重新绘制窗体图形;它们与表单数据的状态无关。
Seems that all you need to do is set your control values back to their original values So, make a function on the form:
似乎您需要做的就是将控件值设置回其原始值所以,在表单上创建一个函数:
private void ResetForm()
{
//write code here to setup your dropdowns, put empty strings into textboxes, etc.
//pretty much the reverse of the process by which you copy the values into your user object.
}
and then in the sucess part of the code call the function:
然后在代码的成功部分调用该函数:
if ( UserManagement.UserInfo_Update (obj) > 0 )
{
MessageBox.Show ("Record Succesfully Updated!");
//reset this form, no need to make another one...
ResetForm();
}
and you can also include a call to ResetForm()
somewhere in your Form_Load
, etc.
并且您还可以ResetForm()
在您的Form_Load
等 中包含对某处的调用。
However
然而
I'd recommend that once you are comfortable with doing this, you then stop doing itand use the data-binding facilitythat's built into Winforms; what it allows you to do is use the designer to bind user interface elements on the form (textboxes, etc) to various Class properties (e.g. UserManagement
class).
我建议您一旦习惯了这样做,就停止这样做并使用Winforms 中内置的数据绑定工具;它允许您使用设计器将表单上的用户界面元素(文本框等)绑定到各种类属性(例如UserManagement
类)。
This way you can simply "reset" your form by creating a new instance of UserManagement
without having to deal with all the cruddy details of clearing out textboxes, etc. Otherwise you will find as your objects grow more complex, writing the code to manually reset form UI elments becomes more and more tedious and error-prone.
这样你就可以通过创建一个新的实例来简单地“重置”你的表单,UserManagement
而不必处理清除文本框等所有粗糙的细节。否则你会发现随着你的对象变得越来越复杂,编写代码来手动重置表单UI元素变得越来越乏味和容易出错。
Hope that helps.
希望有帮助。
回答by xwpedram
This is simple. You must create a new form object and close current form.
这很简单。您必须创建一个新的表单对象并关闭当前表单。
Form fr = new Form();
fr.Show();
this.Close();
回答by PoopaSaurasRex
I accomplished this by closing and opening the form. So replace the ***** with your form name
我通过关闭和打开表单来完成此操作。因此,将 ***** 替换为您的表单名称
***your form name** ss = new **your form name***();
ss.Show();
this.Hide();
I would also recommend putting a form close to fully close the form:
我还建议将表单关闭以完全关闭表单:
private void ***Your form name***Closed(object sender, FormClosedEventArgs e)
{
Application.Exit();
}