Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reload or refresh Windows Form into original state?

Tags:

c#

winforms

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.

like image 737
Michael Vincent Montero Avatar asked Nov 04 '13 02:11

Michael Vincent Montero


People also ask

How do I refresh a Windows Form?

You can use the Form. Invalidate(); or Form. Refresh(); methods.

How do you refresh a form?

To refresh the records in Datasheet or Form view, on the Home tab, in the Records group, click Refresh All, and then click Refresh. To refresh the records in PivotTable or PivotChart view, on the Design tab, in the Data group, click Refresh Pivot. Press SHIFT+F9.

How do I refresh a Windows Form in VB net?

The simplest solution might be to just do the clearing by hand. Alternatively, you can put all your controls into a user control container. Then just instantiate that user control on your form. If you want to reload your form content, you now just need to remove and re-instantiate the user control.

How Windows forms are created and used?

Introduction. Windows Forms is a UI framework for building Windows desktop apps. It provides one of the most productive ways to create desktop apps based on the visual designer provided in Visual Studio. Functionality such as drag-and-drop placement of visual controls makes it easy to build desktop apps.


2 Answers

"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.

like image 74
Stephen Byrne Avatar answered Oct 08 '22 10:10

Stephen Byrne


This is simple. You must create a new form object and close current form.

 Form fr = new Form();
     fr.Show();
     this.Close();
like image 1
xwpedram Avatar answered Oct 08 '22 10:10

xwpedram