开发者

How to clear or regenerate window form after submitting using c#?

I am using student record entry window form. I want that after submitting data, all fields of form(i.e radio button, combobox etc) and messages(warning and successful) should be reset so that new user can add data.

  • is their any built in function to rese开发者_C百科t form in csharp?
  • or I have to write clear method for this?
  • or can I regenerate the form?


You must first clear the controls and then use InitializeComponent method to work perfectly.

this.Controls.Clear();
this.InitializeComponent();


This you can achieve in two ways:-

1) you can clear the fields of the form in one method. say public void clear() And whenever you want to clear the form simply call this method.

2) In second way you can destory the instance of the form and generate the new instance of the same form and then show this.

I will recomeded 1st one for you.

This is what I used to create in my every page

private void ClearControls()
{
    try
    {
        txtUserName.Text = string.Empty;
        //txtPassword.Text = string.Empty;
        txtFName.Text = string.Empty;
        txtMName.Text = string.Empty;
        txtLName.Text = string.Empty;
        lblUserType.Text = string.Empty;
    btnSave.Text = "Save";
    fnMessage(false, "");
}
catch (Exception ex)
{
    fnMessage(true, ex.Message);
}

}

Thanks


  1. Implement data binding to a given object (good starting point here)
  2. For resetting the form, create a new object and the binding will do it for you.

HTH


You can either re-create the form instance, or perhaps try something similar to this (untested):

        foreach (Control ctl in this.Controls)
        {
            switch (ctl.GetType().ToString())
            {
                case "TextBox":
                    ctl.Text = null;
                    break;
                case "ComboBox":
                    ctl.Text = null;
                    break;
            }
        }

Clearly, you can include as many different types of control as you wish and introduce other critieria (i.e. where control name begins with 'xyz' or where control resides within a particular panel).

Compared to other suggestions, the advantage of this approach is that if you have dozens of the same control type (typically textboxes), a few lines of code cover the lot. Additionally, if you add more controls of the covered types, you don't need to revisit the code to update it. Perhaps you could even create it as an extension method of your forms?


If form creation doesn't take too much resources it is easier to create new instance.


As i had same problem and my from was having nested controls. I tried CJM's method and it worked however i had to write a recursive function because of controls nesting (tab controls, containers, user controls etc)

Try out the following snippet if you want to clear whole form recursively

    private void clearRecursive(Control control)
    {
        foreach (Control subcontrol in control.Controls)
        {
            switch (subcontrol.GetType().ToString().Replace("System.Windows.Forms.", ""))
            {
                case "TextBox":
                    TextBox textBox = (TextBox)subcontrol;
                    textBox.Text = null;
                    break;

                case "ComboBox":
                    ComboBox comboBox = (ComboBox)subcontrol;
                    if (comboBox.Items.Count > 0)
                        comboBox.SelectedIndex = 0;
                    break;

                case "CheckBox":
                    CheckBox checkBox = (CheckBox)subcontrol;
                    checkBox.Checked = false;
                    break;

                case "RadioButton":
                    RadioButton radioButton = (RadioButton)subcontrol;
                    radioButton.Checked = false;
                    break;

                case "TreeView":
                    TreeView tv = (TreeView)subcontrol;
                    foreach (TreeNode node in tv.Nodes)
                    {
                        node.Checked = false;
                        CheckChildren(node, false);
                    }
                    break;

                case "ListBox":
                    ListBox listBox = (ListBox)subcontrol;
                    listBox.Items.Clear();
                    break;

                case "CheckedListBox":
                    CheckedListBox chklstbox = (CheckedListBox)subcontrol;
                    for (int i = 0; i < chklstbox.Items.Count; i++)
                    {
                        chklstbox.SetItemCheckState(i, CheckState.Unchecked);
                    }
                    break;
            }

            if (subcontrol.HasChildren)
                clearRecursive(subcontrol);
        }

    }


Call InitializeComponent method and Form_Load method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜