2

I have created a Search form with a function Find Text, How do I save the last search so I can create a Find Again Button when the form has been re-opened?

My code for the search is:

    private void btnSearch_Click(object sender, EventArgs e)
    {
        if (cboField.SelectedIndex == -1)
        {
            return;
        }

        if (txtValue.TextLength == 0)
        {
            return;
        }

        string filter;
        filter = "[" + cboField.Text + "]";

        filter += lblOperation.Text + "'" + txtValue.Text + "'";

        try
        {
            peopleBindingSource.Filter = filter;
        }

        catch (System.Data.EvaluateException)
        {
            MessageBox.Show("Please enter valid values in your text fields.");
        }
    }
JaredH20
  • 370
  • 1
  • 7
  • 22

3 Answers3

2

If you want the search to persist when the app is closed, store and retrieve the user's past search using a Settings class (other answers in that question discuss alternatives)

Community
  • 1
  • 1
stuartd
  • 70,509
  • 14
  • 132
  • 163
2

When your user runs a search I'd copy the search criteria to a User Setting and save it. When the user opens the form retrieve this search criteria and do with it what you will (display it, run it, etc.).

Read more about User Settings here.

Jay Riggs
  • 53,046
  • 9
  • 139
  • 151
0

If it's only necessary to remember the last search, I'd recommend writing this value in the application configuration file.

To add an application configuration file to your C# project

  • On the Project menu, click Add New Item.
  • The Add New Item dialog box appears.
  • Select the Application Configuration File template and then click Add.
  • A file named app.config is added to your project.

Add a key to this configuration file

The file should look something like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
   <appSettings>
      <add key="LastSearch" value="" />
   </appSettings>
</configuration>

Updating the value To overwrite a value in this configuration file, please refer to the following code sample (unfortunately there is no built-in modify method in .NET) http://www.freevbcode.com/ShowCode.asp?ID=7718

Pepijn Olivier
  • 927
  • 1
  • 18
  • 32