Solved

How to reload the form

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now