?
Solved

How to reload the form

Posted on 2003-11-03
1
Medium Priority
?
316 Views
Last Modified: 2010-04-16
Greetings,

I have a C# form which users will use it for searching. How to reload the form or clear the form value once the user try to search for another one.( I have only one button for searching)

Thanks in advance.

0
Comment
Question by:mrong
1 Comment
 
LVL 20

Accepted Solution

by:
TheAvenger earned 160 total points
ID: 9677018
Create a small method to clear the form items by resetting them to the initial values, then call this method at the end of the search.

This has one disadvantage - if you add a new control to the form, you will have to add a line of code in this method to reset it's value as well.

You can make a more general solution as well, but it will work for a limited kind of controls: at the end of the OnLoad method remember all controls' values and on reset put them back. It will work fine for simple controls. Here is an example:

// In the class members
private Hashtable values = new HashTable();
...
// At the end of the OnLoad event
foreach (Control ctrl in this.Controls) {
  if (ctrl is TextBox)
    values[ctrl] = ctrl.Text;
  else if (ctrl is DateTimePicker)
    values[ctrl] = ((DateTimePicker)ctrl).Value;
  else ....   // Add here the same code for every type of control you have on the form
}
...
// At the reset function (called at the end of the search)
foreach (Control ctrl in this.Controls) {
  object val = values[ctrl];
  if (val == null)
    continue;

  if (ctrl is TextBox)
    ctrl.Text = (string)val;
  else if (ctrl is DateTimePicker)
    ((DateTimePicker)ctrl).Value = (DateTime)val;
  else ....   // Add here the same code for every type of control you have on the form
}
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Loops Section Overview
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question