Solved

How to reload the form

Posted on 2003-11-03
1
310 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 20

Accepted Solution

by:
TheAvenger earned 40 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

Suggested Solutions

Title # Comments Views Activity
Calculate number of nights between two dates 5 91
defining NULL or 0 10 50
Dynamics CRM Plugin Query TotalRecordCount returning -1 13 60
How to repeat the data 4 30
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

749 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