Reputation: 53
How to reload or refresh the Windows Form into original state? i have used 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.
Upvotes: 5
Views: 83948
Reputation: 35
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();
}
Upvotes: 1
Reputation: 557
This is simple. You must create a new form object and close current form.
Form fr = new Form();
fr.Show();
this.Close();
Upvotes: 2
Reputation: 7475
"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.
However
I'd recommend that once you are comfortable with doing this, you then stop doing it and use the data-binding facility that'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).
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.
Hope that helps.
Upvotes: 3