p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Access (http://p2p.wrox.com/forumdisplay.php?f=18)
-   -   Range Validator in ADO.net (http://p2p.wrox.com/showthread.php?t=27907)

abdul_wasie April 9th, 2005 11:12 AM

Range Validator in ADO.net
 
how can i include range validation in textbox ? using C# with ADO.net?

Abdul Wasie

Bob Bedell April 9th, 2005 10:39 PM

Hi Adbul,

The following will give the user immediate validation feedback when they tab out of an unbound textbox.

1. Create a form with a textbox and a button (the button's only purpose will be to provide a control to receive focus when you tab out of the textbox). Drop an ErrorProvider control on the form to place it in the components tray. Name the ErrorProvider control 'err'.

2. Create a class to store your range criteria as static properties. Here's the code:

Code:

using System;

namespace RangeValidation
{
    public class Globals
    {
        public static int MinRange{get{return 6;}}
        public static int MaxRange{get{return 12;}}

    }
}

3. In the form's InitializeComponent method, find the code that initializes the textbox. Add the following line of code to wire up the Validating event):

Code:

//
// txtRange
//
this.txtRange.Validating += new System.ComponentModel.CancelEventHandler(this.RangeValidate);

Notice that the event handler calls the RangeValidate method.

4. Here's the RangeValidate method:

Code:

private void RangeValidate(object sender, CancelEventArgs e)
        {
            Debug.Assert(sender == txtRange,
                "RangeValidate method called by wrong control");

            try
            {
                int range = int.Parse(txtRange.Text);
                if(range < Globals.MinRange || range > Globals.MaxRange)
                {
                    err.SetError(txtRange, "Range must be between 6 and 12");
                    e.Cancel = true;
                }
            }
            catch
            {
                err.SetError(txtRange, "BUG: Range must be an integer");
                e.Cancel = true;
            }

            if(e.Cancel)
                txtRange.SelectAll();
            else
                err.SetError(txtRange, "");
        }



Include the System.Diagnostics namspace as a using directive.

HTH,

Bob


abdul_wasie April 10th, 2005 01:27 AM

thanks bob for replying...

one more problem, i have to open subform from active form so i was hiding the active form and opening the subform when i click on button following is the code which i am using the problem is that when i close my progamme myprogramme.exe is still running in windows task manager - processs

private void button4_Click(object sender, System.EventArgs e)
        {
            this.Hide();
            TCodeCatalog TCode = new TCodeCatalog();
            DialogResult dr = TCode.ShowDialog();

        }

Abdul Wasie

Bob Bedell April 10th, 2005 07:50 PM

Hi Abdul,

Not quite sure whats happening there. Processes don't get garbage collected and should terminate when your program returns to the main entry routine:

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
    Application.Run(new Form1());
} //process should terminate here when codepath returns to this point.

The only thing I can think of that might prevent that from happening is if you a spawning a new thread in you code somewhere other than the main UI thread and that second thread isn't terminating successfully.

Bob





All times are GMT -4. The time now is 09:31 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.