RegexEditorDialog.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ FX-1434 / FX-1434 / 1.0 / untmp / whidbey / REDBITS / ndp / fx / src / Designer / WebForms / System / Web / UI / Design / WebControls / RegexEditorDialog.cs / 1 / RegexEditorDialog.cs

                            namespace System.Web.UI.Design.WebControls { 
    using System;
    using System.Design;
    using System.Drawing;
    using System.Collections; 
    using System.ComponentModel;
    using System.ComponentModel.Design; 
    using System.Globalization; 
    using System.Windows.Forms;
    using System.Diagnostics; 
    using System.Text.RegularExpressions;
    using System.Web.UI.Design.Util;
    using System.Windows.Forms.Design;
 
    /// 
    ///  
    ///  
    ///    Dialog for editting regular expressions used by the RegularExpressionValidator
    ///  
    [
    System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand, Flags=System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode),
    ToolboxItem(false)
    ] 
    public class RegexEditorDialog : Form {
 
        private System.ComponentModel.Container components; 
        private System.Windows.Forms.TextBox txtExpression;
        private System.Windows.Forms.ListBox lstStandardExpressions; 
        private System.Windows.Forms.Label lblStandardExpressions;
        private System.Windows.Forms.Label lblTestResult;
        private System.Windows.Forms.TextBox txtSampleInput;
        private System.Windows.Forms.Button cmdTestValidate; 
        private System.Windows.Forms.Label lblInput;
        private System.Windows.Forms.Label lblExpression; 
        private System.Windows.Forms.GroupBox grpExpression; 
        private System.Windows.Forms.Button cmdCancel;
        private System.Windows.Forms.Button cmdOK; 

        private string regularExpression;
        private bool settingValue;
        private bool firstActivate = true; 
        private ISite site;
        static object [] cannedExpressions; 
 

        ///  
        /// 
        /// 
        ///    [To be supplied.]
        ///  
        public string RegularExpression {
            get { 
                return regularExpression; 
            }
            set { 
                regularExpression = value;
            }
        }
 
        /// 
        ///  
        ///  
        ///    [To be supplied.]
        ///  
        public RegexEditorDialog(ISite site) {
            this.site = site;
            InitializeComponent();
            settingValue = false; 
            regularExpression = string.Empty;
        } 
 
        /// 
        ///  
        /// 
        ///    [To be supplied.]
        /// 
        protected override void Dispose(bool disposing) { 
            if (disposing) {
                components.Dispose(); 
            } 
            base.Dispose(disposing);
        } 

        private void InitializeComponent() {
            this.components = new System.ComponentModel.Container();
            this.lblTestResult = new System.Windows.Forms.Label(); 
            this.lstStandardExpressions = new System.Windows.Forms.ListBox();
            this.lblStandardExpressions = new System.Windows.Forms.Label(); 
            this.cmdTestValidate = new System.Windows.Forms.Button(); 
            this.txtExpression = new System.Windows.Forms.TextBox();
            this.lblInput = new System.Windows.Forms.Label(); 
            this.grpExpression = new System.Windows.Forms.GroupBox();
            this.txtSampleInput = new System.Windows.Forms.TextBox();
            this.cmdCancel = new System.Windows.Forms.Button();
            this.lblExpression = new System.Windows.Forms.Label(); 
            this.cmdOK = new System.Windows.Forms.Button();
 
            Font dialogFont = UIServiceHelper.GetDialogFont(site); 
            if (dialogFont != null) {
                Font = dialogFont; 
            }

            // Set RightToLeft mode based on resource file
            string rtlText = SR.GetString(SR.RTL); 
            if (!String.Equals(rtlText, "RTL_False", StringComparison.Ordinal)) {
                RightToLeft = RightToLeft.Yes; 
                RightToLeftLayout = true; 
            }
 
            MinimizeBox = false;
            MaximizeBox = false;
            ShowInTaskbar = false;
            StartPosition = System.Windows.Forms.FormStartPosition.CenterParent; 

            this.Text = SR.GetString(SR.RegexEditor_Title); 
            this.ImeMode = System.Windows.Forms.ImeMode.Disable; 
            this.AcceptButton = cmdOK;
            this.CancelButton = cmdCancel; 
            this.Icon = null;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.ClientSize = new System.Drawing.Size(348, 215);
            this.Activated += new System.EventHandler(RegexTypeEditor_Activated); 
            this.HelpRequested += new HelpEventHandler(Form_HelpRequested);
            this.HelpButton = true; 
            this.HelpButtonClicked += new CancelEventHandler(HelpButton_Click); 

            lstStandardExpressions.Location = new System.Drawing.Point(12, 30); 
            lstStandardExpressions.Size = new System.Drawing.Size(324, 84);
            lstStandardExpressions.TabIndex = 1;
            lstStandardExpressions.SelectedIndexChanged += new System.EventHandler(lstStandardExpressions_SelectedIndexChanged);
            lstStandardExpressions.Sorted = true; 
            lstStandardExpressions.IntegralHeight = true;
            lstStandardExpressions.Items.AddRange(CannedExpressions); 
 
            lblStandardExpressions.Location = new System.Drawing.Point(12, 12);
            lblStandardExpressions.Text = SR.GetString(SR.RegexEditor_StdExp); 
            lblStandardExpressions.Size = new System.Drawing.Size(328, 16);
            lblStandardExpressions.TabIndex = 0;

            txtExpression.Location = new System.Drawing.Point(12, 140); 
            txtExpression.TabIndex = 3;
            txtExpression.Size = new System.Drawing.Size(324, 20); 
            txtExpression.TextChanged += new System.EventHandler(txtExpression_TextChanged); 

            lblExpression.Location = new System.Drawing.Point(12, 122); 
            lblExpression.Text = SR.GetString(SR.RegexEditor_ValidationExpression);
            lblExpression.Size = new System.Drawing.Size(328, 16);
            lblExpression.TabIndex = 2;
 
            cmdOK.Location = new System.Drawing.Point(180, 180);
            cmdOK.DialogResult = System.Windows.Forms.DialogResult.OK; 
            cmdOK.Size = new System.Drawing.Size(75, 23); 
            cmdOK.TabIndex = 9;
            cmdOK.Text = SR.GetString(SR.OK); 
            cmdOK.FlatStyle = FlatStyle.System;
            cmdOK.Click += new System.EventHandler(cmdOK_Click);

            cmdCancel.Location = new System.Drawing.Point(261, 180); 
            cmdCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            cmdCancel.Size = new System.Drawing.Size(75, 23); 
            cmdCancel.TabIndex = 10; 
            cmdCancel.FlatStyle = FlatStyle.System;
            cmdCancel.Text = SR.GetString(SR.Cancel); 

            // This is hidden and out of the way for now.
            grpExpression.Location = new System.Drawing.Point(8, 280);
            grpExpression.ImeMode = System.Windows.Forms.ImeMode.Disable; 
            grpExpression.TabIndex = 4;
            grpExpression.TabStop = false; 
            grpExpression.Text = SR.GetString(SR.RegexEditor_TestExpression); 
            grpExpression.Size = new System.Drawing.Size(328, 80);
            grpExpression.Visible = false; 

            txtSampleInput.Location = new System.Drawing.Point(88, 24);
            txtSampleInput.TabIndex = 6;
            txtSampleInput.Size = new System.Drawing.Size(160, 20); 

            grpExpression.Controls.Add(lblTestResult); 
            grpExpression.Controls.Add(txtSampleInput); 
            grpExpression.Controls.Add(cmdTestValidate);
            grpExpression.Controls.Add(lblInput); 

            cmdTestValidate.Location = new System.Drawing.Point(256, 24);
            cmdTestValidate.Size = new System.Drawing.Size(56, 20);
            cmdTestValidate.TabIndex = 7; 
            cmdTestValidate.Text = SR.GetString(SR.RegexEditor_Validate);
            cmdTestValidate.FlatStyle = FlatStyle.System; 
            cmdTestValidate.Click += new System.EventHandler(cmdTestValidate_Click); 

            lblInput.Location = new System.Drawing.Point(8, 28); 
            lblInput.Text = SR.GetString(SR.RegexEditor_SampleInput);
            lblInput.Size = new System.Drawing.Size(80, 16);
            lblInput.TabIndex = 5;
 
            lblTestResult.Location = new System.Drawing.Point(8, 56);
            lblTestResult.Size = new System.Drawing.Size(312, 16); 
            lblTestResult.TabIndex = 8; 

            this.Controls.Add(txtExpression); 
            this.Controls.Add(lstStandardExpressions);
            this.Controls.Add(lblStandardExpressions);
            this.Controls.Add(lblExpression);
            this.Controls.Add(grpExpression); 
            this.Controls.Add(cmdCancel);
            this.Controls.Add(cmdOK); 
        } 

        ///  
        /// 
        /// 
        ///    [To be supplied.]
        ///  
        protected void txtExpression_TextChanged(object sender, System.EventArgs e){
            if (settingValue || firstActivate) 
                return; 
            lblTestResult.Text = string.Empty;
            UpdateExpressionList(); 
        }
        /// 
        /// 
        ///  
        ///    [To be supplied.]
        ///  
        protected void lstStandardExpressions_SelectedIndexChanged(object sender, System.EventArgs e) { 
            if (settingValue)
                return; 
            // first item should always be "(Custom)"
            if (lstStandardExpressions.SelectedIndex >= 1) {
                CannedExpression expression = (CannedExpression) lstStandardExpressions.SelectedItem;
                settingValue = true; 
                txtExpression.Text = expression.Expression;
                settingValue = false; 
            } 
        }
 
        /// 
        /// 
        /// 
        ///    [To be supplied.] 
        /// 
        protected void RegexTypeEditor_Activated(object sender, System.EventArgs e) { 
            if (!firstActivate) { 
                return;
            } 
            txtExpression.Text = RegularExpression;
            UpdateExpressionList();
            firstActivate = false;
        } 

        private void UpdateExpressionList() { 
            bool found = false; 
            settingValue = true;
            string expression = txtExpression.Text; 
            // first item is always be "(Custom)"
            for (int i = 1; i < lstStandardExpressions.Items.Count; i++) {
                if (expression == ((CannedExpression)lstStandardExpressions.Items[i]).Expression) {
                    lstStandardExpressions.SelectedIndex = i; 
                    found = true;
                } 
            } 
            if (!found) {
                lstStandardExpressions.SelectedIndex = 0; 
            }
            settingValue = false;
        }
 
        /// 
        ///  
        ///  
        ///    [To be supplied.]
        ///  
        protected void cmdTestValidate_Click(object sender, System.EventArgs args) {
            // Note: keep this in sync with RegularExpressionValidator.EvaluateIsValid

            try { 
                bool isValid;
                // check the expression first so we find all bad expressions 
                // we are looking for an exact match, not just a search hit 
                Match m = Regex.Match(txtSampleInput.Text, txtExpression.Text);
                isValid = (m.Success 
                    && m.Index == 0
                    && m.Length == txtSampleInput.Text.Length);

                // Blank input is always valid 
                if (txtSampleInput.Text.Length == 0) {
                    isValid = true; 
                } 
                else {
                } 
                lblTestResult.Text = isValid
                    ? SR.GetString(SR.RegexEditor_InputValid)
                    : SR.GetString(SR.RegexEditor_InputInvalid);
                lblTestResult.ForeColor = isValid ? Color.Black : Color.Red; 
            }
            catch { 
                lblTestResult.Text = SR.GetString(SR.RegexEditor_BadExpression); 
                lblTestResult.ForeColor = Color.Red;
            } 
        }

        private void ShowHelp() {
            IHelpService helpService = (IHelpService)site.GetService(typeof(IHelpService)); 
            if (helpService != null) {
                helpService.ShowHelpFromKeyword("net.Asp.RegularExpressionEditor"); 
            } 
        }
 
        /// 
        protected void cmdHelp_Click(object sender, System.EventArgs e) {
            ShowHelp();
        } 

        private void Form_HelpRequested(object sender, HelpEventArgs e) { 
            ShowHelp(); 
        }
 
        private void HelpButton_Click(object sender, CancelEventArgs e) {
            e.Cancel = true;
            ShowHelp();
        } 

        ///  
        ///  
        /// 
        ///    [To be supplied.] 
        /// 
        protected void cmdOK_Click(object sender, System.EventArgs e) {
            this.RegularExpression = this.txtExpression.Text;
        } 

        private static readonly RegExpEntry [] _entries = { 
            new RegExpEntry(SR.RegexCanned_SocialSecurity, SR.RegexCanned_SocialSecurity_Format), 
            new RegExpEntry(SR.RegexCanned_USPhone, SR.RegexCanned_USPhone_Format),
            new RegExpEntry(SR.RegexCanned_Zip, SR.RegexCanned_Zip_Format), 

            new RegExpEntry(SR.RegexCanned_FrZip, SR.RegexCanned_FrZip_Format),
            new RegExpEntry(SR.RegexCanned_FrPhone, SR.RegexCanned_FrPhone_Format),
 
            new RegExpEntry(SR.RegexCanned_DeZip, SR.RegexCanned_DeZip_Format),
            new RegExpEntry(SR.RegexCanned_DePhone, SR.RegexCanned_DePhone_Format), 
 
            new RegExpEntry(SR.RegexCanned_JpnZip, SR.RegexCanned_JpnZip_Format),
            new RegExpEntry(SR.RegexCanned_JpnPhone, SR.RegexCanned_JpnPhone_Format), 

            new RegExpEntry(SR.RegexCanned_PrcZip, SR.RegexCanned_PrcZip_Format),
            new RegExpEntry(SR.RegexCanned_PrcPhone, SR.RegexCanned_PrcPhone_Format),
            new RegExpEntry(SR.RegexCanned_PrcSocialSecurity, SR.RegexCanned_PrcSocialSecurity_Format), 
        };
 
        private object [] CannedExpressions { 
            get {
                if (cannedExpressions == null) { 
                    ArrayList regExps = new ArrayList();
                    regExps.Add(SR.GetString(SR.RegexCanned_Custom));
                    regExps.Add(new CannedExpression(SR.GetString(SR.RegexCanned_Email), @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"));
                    regExps.Add(new CannedExpression(SR.GetString(SR.RegexCanned_URL), @"http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?")); 

                    foreach (RegExpEntry entry in _entries) { 
                        if (entry.Name.Length > 0) { 
                            regExps.Add(new CannedExpression(SR.GetString(entry.Name), SR.GetString(entry.Format)));
                        } 
                    }

                    cannedExpressions = new object [regExps.Count];
                    regExps.CopyTo(cannedExpressions); 
                }
                return cannedExpressions; 
            } 
        }
 
        private class CannedExpression {

            public string Description;
            public string Expression; 

            public CannedExpression(string description, string expression) { 
                Description = description; 
                Expression = expression;
            } 

            public override string ToString() {
                return Description;
            } 
        }
 
        private class RegExpEntry { 
            public string Name;
            public string Format; 

            public RegExpEntry(string name, string format) {
                Name = name;
                Format = format; 
            }
        } 
    } 
}
 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.


                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK