Solved

How to reload the form

Posted on 2003-11-03
1
308 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 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

809 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