Saved new record, what is the best method for clearing a form.

OK, this is a nub question by a guy who knows a lot about web but not nearly enough about Windows forms.

If I open a new form to create and save a new record (like a new user), what would be the best way to re-initialize the form after the record was saved to start over again with a blank form

I do not really want to close the form, because a data entry person may want to sit and enter numerous records without having to open a new form all the time.

Is there any way easier than manually resetting each individual control (which could be a pain on complicated forms)

Thanks.




Answer this question

Saved new record, what is the best method for clearing a form.

  • ronmurp

    There's no one solution to your problem. But the solution that Yifung Lin presented is a clean one.
  • TheRoon

    I have posted a similar question on the Windows Forms Data Controls and Databinding forum (althought for data tables). I am using the tableadapter.fill method and binding it to a non-existent record. It seemed to work for my purposes. Can you bind your form to a non-existent item in your class


  • CoreyMasson

    If you have a custom data source, Use the Data Sources panel (found under the Data menu) and add a Object Data Source that points to your custom collection. Then from the data source panel, you can drag your WorkOrder table to the form and have it render as a grid or a set of detail controls and all the binding and renaming will be done for you through a Binding Source. You will also get a Navigator in the deal. Then it is very easy to move to the new record and call the save function when editing is complete. (see my article http://www.15seconds.com/issue/051117.htm).



  • frog37

    Thanks.

    This seems like a very common and simple task for such an elabotate solution.

    Would it just be easier to close the form and re-open a new one



  • Srik

    There isn't a generic way to re-initialize a form with any array of controls. If I'm dealing with a complex form, I think I would create an interface with a single method to re-initialize and have all the controls on the form implement the interface. I would subclass any existing Winform controls I need and implement the interface there and also in UserControls I create. Then, enumerating through the controls to re-initialize them is pretty simple. Here's a small example:

    public partial class Form1 : Form {

    public Form1() {

    InitializeComponent();

    MyTextBox textbox = new MyTextBox();

    this.Controls.Add(textbox);

    }

    // Interface with single Reinitialize method

    interface IReinitialize {

    void Reinitialize();

    }

    // Look through the controls on the form and call Reinitialize

    // on the ones that implement the interface. We can even make this

    // recursive to deal with nested child controls if they're not contained

    // in a UserControl that implements the interface

    private void ReinitializeAllControls() {

    foreach (Control control in this.Controls) {

    IReinitialize reinitialize = control as IReinitialize;

    if (reinitialize != null)

    reinitialize.Reinitialize();

    }

    }

    // Sample control that implements IReinitialize

    public class MyTextBox : TextBox, IReinitialize {

    public MyTextBox() {

    this.Text = "Hello world";

    }

    public void Reinitialize() {

    this.Clear();

    }

    }

    }


  • eferreyra

    OK, you have me intrigued.

    Again, I am a web guy, so I tend to think very stateless.

    I have an item. Call it a 'work order'.

    I drill to the Customer, then I want to add a new work order.

    Currently I open a form called 'Add Work Order' with a bunch of blank fields. I fill them in and save them. I then want that same form to be blank so the user can add another quickly.

    I read up on the BindingSource, and that looks like a great concept. I bind to a 'WorkOrder' class, that is delivered via a business logic layer. The Forms application has no access to the data layer. There are no DataSets available in the Forms application space, only custom objects and List<CustomObject>.

    I do not think I really want to 'page' back an forth through records. I don't find paging a useful tool when a customer may have hundreds of work orders. Typically there is a DataGridView on another form with sorting and search filtering that you can pick one work order to get the to details, and a separate form for creating a new work order.

    I know this is 'web like' in is workflow, and am very open minded to doing it the WIndows Forms way.

    The tool bar with a 'save' and a 'new' would really be all I needed.



  • David Zillner

    If you are using VS2005, simply move to a new record in the Binding Source. If you are using the navigation bar, click the yellow + icon. Otherwise, in code:

    MyBindingSource.AddNew()

    will move to a blank record at the end of the recordset and prepare for data entry.

    If you are not using VS2005 or you are not using Binding Sources, consider moving to it--very powerful tools.



  • millerfj

    Great idea.

    I just have a bunch of text boxes and stuff that are filled with values from a custom class.

    Is there a better way



  • Saved new record, what is the best method for clearing a form.