예제 #1
0
        /// <summary>
        /// Loads controls on the panel
        /// </summary>
        private void LoadControls()
        {
            foreach (SearchListBoxItem item in searchFieldsCollection)
            {
                //add masked textbox or textbox to the panel
                if (!string.IsNullOrEmpty(item.Pattern))
                {
                    //Add masked textbox to the panel
                    MaskedTextBox fieldTextBox = new MaskedTextBox();
                    fieldTextBox.Location = new System.Drawing.Point(300, textLinePosition);
                    fieldTextBox.Size     = new System.Drawing.Size(286, 20);
                    fieldTextBox.BringToFront();
                    fieldTextBox.Leave += new EventHandler(fieldTextBox_Leave);
                    //                    fieldTextBox.MaskInputRejected += new MaskInputRejectedEventHandler(maskedTextBox_MaskInputRejected);
                    fieldTextBox.KeyUp += FieldTextBox_KeyUp;
                    string mask = AppData.Instance.DataPatternsDataTable.GetMaskByPattern(item.Pattern);
                    //fieldTextBox.Mask = AppData.Instance.DataPatternsDataTable.GetExpressionByMask(mask, item.Pattern);
                    fieldTextBox.Tag  = item.Type;
                    fieldTextBox.Name = item.Name;
                    //fieldTextBox.Mask = mask;

                    if (item.Type.Equals("Number") || item.Type.Equals("PhoneNumber"))
                    {
                        //    fieldTextBox.PromptChar = '_';
                    }
                    else if (item.Type.Equals("Date") || item.Type.Equals("DateTime"))
                    {
                        //    fieldTextBox.PromptChar = '_';
                        fieldTextBox.Text = String.Format("{0}", string.Empty);
                    }
                    else if (item.Type.Equals("Time"))
                    {
                        //    fieldTextBox.PromptChar = '_';
                        fieldTextBox.Text = String.Format("{0:T}", string.Empty);
                    }

                    splitContainer1.Panel2.Controls.Add(fieldTextBox);
                    textLinePosition += 38;
                    if (myControlDictionaryData.ContainsKey(item.Name.ToString()))
                    {
                        fieldTextBox.Text = myControlDictionaryData[item.Name.ToString()].ToString();
                    }

                    myControlDictionary.Add(item.Name.ToString(), fieldTextBox);
                    myControlItemDictionary.Add(fieldTextBox, item);
                    fieldTextBox.Focus();
                }
                else
                {
                    //if (item.Type.Equals("YesNo"))
                    //{
                    //    ComboBox cbxYesNo = new ComboBox();
                    //    Configuration config = Configuration.GetNewInstance();
                    //    cbxYesNo.Items.AddRange(new object[] { config.Settings.RepresentationOfYes, config.Settings.RepresentationOfNo, config.Settings.RepresentationOfMissing });
                    //    cbxYesNo.Location = new System.Drawing.Point(300, textLinePosition);
                    //    cbxYesNo.Size = new System.Drawing.Size(286, 20);
                    //    cbxYesNo.BringToFront();
                    //    cbxYesNo.Leave += new EventHandler(fieldTextBox_Leave);
                    //    splitContainer1.Panel2.Controls.Add(cbxYesNo);
                    //    textLinePosition += 38;

                    //    myControlDictionary.Add(item.Name.ToString(), cbxYesNo);
                    //    myControlItemDictionary.Add(cbxYesNo, item);
                    //    cbxYesNo.Focus();
                    //}
                    //else
                    {
                        ////add textbox to panel
                        TextBox fieldTextBox = new TextBox();
                        fieldTextBox.Name     = item.Name;
                        fieldTextBox.Location = new System.Drawing.Point(300, textLinePosition);
                        fieldTextBox.Size     = new System.Drawing.Size(286, 20);
                        fieldTextBox.BringToFront();
                        fieldTextBox.Leave += new EventHandler(fieldTextBox_Leave);
                        fieldTextBox.KeyUp += FieldTextBox_KeyUp;
                        splitContainer1.Panel2.Controls.Add(fieldTextBox);
                        textLinePosition += 38;

                        myControlDictionary.Add(item.Name.ToString(), fieldTextBox);

                        myControlItemDictionary.Add(fieldTextBox, item);
                        fieldTextBox.Focus();
                    }
                }

                //add field name label to panel
                Label fieldName = new Label();
                fieldName.Tag       = item.Name.ToString().ToUpperInvariant();
                fieldName.Location  = new System.Drawing.Point(26, labelLinePosition);
                fieldName.Size      = new System.Drawing.Size(35, 13);
                fieldName.AutoSize  = true;
                fieldName.FlatStyle = FlatStyle.System;
                fieldName.Text      = item.Name.ToString();
                splitContainer1.Panel2.Controls.Add(fieldName);
                labelLinePosition += 38;

                //add pattern label to panel
                Label lblPattern = new Label();
                lblPattern.Tag       = item.Name.ToString().ToUpperInvariant();
                lblPattern.Location  = new System.Drawing.Point(620, patternLinePosition);
                lblPattern.Size      = new System.Drawing.Size(35, 13);
                lblPattern.AutoSize  = true;
                lblPattern.FlatStyle = FlatStyle.System;
                splitContainer1.Panel2.Controls.Add(lblPattern);
                patternLinePosition += 38;
                if (!string.IsNullOrEmpty(item.Pattern))
                {
                    lblPattern.Text = item.Pattern.ToString();
                }

                //myControlDictionary.Add(item.Name.ToString(), fieldTextBox);
                //myControlItemDictionary.Add(fieldTextBox, item);
            }
        }
예제 #2
0
        static int Main(string[] args)
        {
            // Based on code by Gorkem Gencay on StackOverflow.com:
            // http://stackoverflow.com/questions/97097/what-is-the-c-sharp-version-of-vb-nets-inputdialog#17546909

            #region Initialize variables

            const string deftitle = "Title";
            const string deftext  = "Prompt";

            bool   heightset           = false;
            bool   showpassword        = false;
            bool   timeoutset          = false;
            bool   widthset            = false;
            string input               = string.Empty;
            string mask                = String.Empty;
            string showpasswordprompt  = "Show password";
            string text                = deftext;
            string title               = deftitle;
            int    height              = defheight;
            int    timeout             = 0;
            int    width               = defwidth;
            string cancelcaption       = "&Cancel";
            string okcaption           = "&OK";
            string localizationstring  = String.Empty;
            bool   localizedcaptionset = false;

            #endregion Initialize variables


            #region Command Line Parsing

            if (args.Length == 0)
            {
                return(ErrorMessage( ));
            }

            foreach (string arg in args)
            {
                if (arg == "/?")
                {
                    return(ErrorMessage( ));
                }
            }

            text      = String.Empty;
            title     = String.Empty;
            defanswer = String.Empty;

            foreach (string arg in args)
            {
                if (arg[0] == '/')
                {
                    if (arg.Length == 1)
                    {
                        return(ErrorMessage( ));
                    }
                    else if (arg.Length == 2)
                    {
                        switch (arg.ToString( ).ToUpper( ))
                        {
                        case "/A":
                            if (ASCIIonly)
                            {
                                return(ErrorMessage("Duplicate command line switch /A"));
                            }
                            ASCIIonly = true;
                            break;

                        case "/B":
                            if (bw)
                            {
                                return(ErrorMessage("Duplicate command line switch /B"));
                            }
                            bw   = true;
                            bold = Console.ForegroundColor;
                            break;

                        case "/L":
                            if (localizedcaptionset)
                            {
                                return(ErrorMessage("Duplicate command line switch /L"));
                            }
                            localizedcaptionset = true;
                            break;

                        case "/M":
                            return(HelpMessage("mask"));

                        case "/N":
                            if (!filtered)
                            {
                                return(ErrorMessage("Duplicate command line switch /N"));
                            }
                            filtered = false;
                            break;

                        case "/P":
                            if (password)
                            {
                                return(ErrorMessage("Duplicate command line switch /P"));
                            }
                            password = true;
                            break;

                        case "/S":
                            if (showpassword)
                            {
                                return(ErrorMessage("Duplicate command line switch /S"));
                            }
                            showpassword = true;
                            break;

                        case "/T":
                            if (timeoutset)
                            {
                                return(ErrorMessage("Duplicate command line switch /T"));
                            }
                            timeout    = deftimeout;
                            timeoutset = true;
                            break;

                        default:
                            return(ErrorMessage("Invalid command line switch {0}", arg));
                        }
                    }
                    else if (arg.Length == 3)
                    {
                        switch (arg.ToUpper( ))
                        {
                        case "/M:":
                            ErrorMessage("No mask specified with /M");
                            Console.WriteLine("\n\n");
                            return(HelpMessage("mask"));

                        default:
                            return(ErrorMessage("Invalid command line switch \"{0}\"", arg.ToUpper( )));
                        }
                    }
                    else
                    {
                        switch (arg.Substring(0, 3).ToUpper( ))
                        {
                        case "/H:":
                            if (heightset)
                            {
                                return(ErrorMessage("Duplicate command line switch /H"));
                            }
                            try
                            {
                                height = Convert.ToInt32(arg.Substring(3));
                                if (height < defheight || height > Screen.PrimaryScreen.Bounds.Height)
                                {
                                    return(ErrorMessage("Invalid screen height: \"{0}\"\n\tHeight must be an integer between {1} and {2} (screen height)", arg.Substring(3), defheight.ToString( ), Screen.PrimaryScreen.Bounds.Height.ToString( )));
                                }
                                heightset = true;
                            }
                            catch (FormatException e)
                            {
                                return(ErrorMessage("Invalid height: \"{0}\"\n\t{1}", arg.Substring(3), e.Message));
                            }
                            break;

                        case "/L:":
                            if (localizedcaptionset)
                            {
                                return(ErrorMessage("Duplicate command line switch /L"));
                            }
                            localizedcaptionset = true;
                            localizationstring  = arg.Substring(3);
                            break;

                        case "/M:":
                            if (usemask)
                            {
                                return(ErrorMessage("Duplicate command line switch /M"));
                            }
                            mask = arg.Substring(3).Trim("\"".ToCharArray( ));
                            if (String.IsNullOrWhiteSpace(mask))
                            {
                                ErrorMessage("No mask specified with /M");
                                Console.WriteLine("\n\n");
                                return(HelpMessage("mask"));
                            }
                            usemask = true;
                            break;

                        case "/R:":
                            if (regexset)
                            {
                                return(ErrorMessage("Duplicate command line switch /R"));
                            }
                            pattern  = arg.Substring(3);
                            regexset = true;
                            break;

                        case "/S:":
                            if (showpassword)
                            {
                                return(ErrorMessage("Duplicate command line switch /S"));
                            }
                            showpassword       = true;
                            showpasswordprompt = arg.Substring(3);
                            break;

                        case "/T:":
                            if (timeoutset)
                            {
                                return(ErrorMessage("Duplicate command line switch /T"));
                            }
                            try
                            {
                                timeout = Convert.ToInt32(arg.Substring(3)) * 1000;
                                if (timeout < 1000)
                                {
                                    return(ErrorMessage("Invalid timeout: \"{0}\"\n\tTimeout value must be a positive integer, at least 1.", arg.Substring(3)));
                                }
                                timeoutset = true;
                            }
                            catch (FormatException e)
                            {
                                return(ErrorMessage("Invalid timeout: \"{0}\"\n\t{1}", arg.Substring(3), e.Message));
                            }
                            break;

                        case "/W:":
                            if (widthset)
                            {
                                return(ErrorMessage("Duplicate command line switch /W"));
                            }
                            try
                            {
                                width = Convert.ToInt32(arg.Substring(3));
                                if (width < defwidth || width > Screen.PrimaryScreen.Bounds.Width)
                                {
                                    return(ErrorMessage("Invalid screen width: \"{0}\"\n\tWidth must be an integer between {1} and {2} (screen width)", arg.Substring(3), defwidth.ToString( ), Screen.PrimaryScreen.Bounds.Width.ToString( )));
                                }
                                widthset = true;
                            }
                            catch (FormatException e)
                            {
                                return(ErrorMessage("Invalid width: \"{0}\"\n\t{1}", arg.Substring(3), e.Message));
                            }
                            break;

                        default:
                            return(ErrorMessage("Invalid command line switch \"{0}\"", arg));
                        }
                    }
                }
                else
                {
                    if (String.IsNullOrWhiteSpace(text))
                    {
                        text = arg;
                    }
                    else if (String.IsNullOrWhiteSpace(title))
                    {
                        title = arg;
                    }
                    else if (String.IsNullOrWhiteSpace(defanswer))
                    {
                        defanswer = arg;
                    }
                    else
                    {
                        return(ErrorMessage("Invalid command line argument \"{0}\"", arg));
                    }
                }
            }

            // Default title if none specified
            if (String.IsNullOrWhiteSpace(title))
            {
                title = "© 2014 Rob van der Woude";
            }

            // "Bold" color depends on /BW
            if (bw)
            {
                bold = Console.ForegroundColor;
            }
            else
            {
                bold = ConsoleColor.White;
            }

            // Switch /A requires /M
            if (ASCIIonly && !usemask)
            {
                return(ErrorMessage("Command line switch /A (ASCII only) can only be used together with /M"));
            }

            // Switch /S implies /P
            if (showpassword)
            {
                password = true;
            }

            // Set timer if /T:timeout was specified
            if (timeoutset)
            {
                System.Timers.Timer timer = new System.Timers.Timer( );
                timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
                timer.Interval = timeout;
                timer.Start( );
            }

            // For /S (Show password checkbox) add 25 px to window height unless height is specified
            if (showpassword && !heightset)
            {
                height += 25;
            }

            #endregion Command Line Parsing


            #region Set Localized Captions

            if (localizedcaptionset)
            {
                cancelcaption = Load("user32.dll", 801, cancelcaption);
                okcaption     = Load("user32.dll", 800, okcaption);
                if (!String.IsNullOrWhiteSpace(localizationstring))
                {
                    string pattern = @"^((OK|Cancel)=[^;\""]*;)*((OK|Cancel)=[^;\""]*);?$";
                    Regex  regex   = new Regex(pattern, RegexOptions.IgnoreCase);
                    if (regex.IsMatch(localizationstring))
                    {
                        string[] locstrings = localizationstring.Split(";".ToCharArray( ));
                        foreach (string locstring in locstrings)
                        {
                            string key = locstring.Substring(0, locstring.IndexOf('='));
                            string val = locstring.Substring(Math.Min(locstring.IndexOf('=') + 1, locstring.Length - 1));
                            if (!String.IsNullOrWhiteSpace(val))
                            {
                                switch (key.ToUpper( ))
                                {
                                case "OK":
                                    okcaption = val;
                                    break;

                                case "CANCEL":
                                    cancelcaption = val;
                                    break;

                                default:
                                    return(ErrorMessage("Invalid localization key \"{0}\"", key));
                                }
                            }
                        }
                    }
                    else
                    {
                        return(ErrorMessage("Invalid localization string:\n\t{0}", localizationstring));
                    }
                }
            }

            #endregion Set Localized Captions


            #region Define Form

            Size size     = new Size(width, height);
            Form inputBox = new Form( );

            inputBox.FormBorderStyle = FormBorderStyle.FixedDialog;
            inputBox.MaximizeBox     = false;
            inputBox.MinimizeBox     = false;
            inputBox.StartPosition   = FormStartPosition.CenterParent;
            inputBox.ClientSize      = size;
            inputBox.Text            = title;

            Label labelPrompt = new Label( );
            labelPrompt.Size     = new Size(width - 20, height - 90);
            labelPrompt.Location = new Point(10, 10);
            labelPrompt.Text     = text.Replace("\\n", "\n");
            inputBox.Controls.Add(labelPrompt);

            textBox      = new TextBox( );
            textBox.Size = new Size(width - 20, 25);
            if (showpassword)
            {
                textBox.Location = new Point(10, height - 100);
            }
            else
            {
                textBox.Location = new Point(10, height - 75);
            }
            if (password)
            {
                textBox.PasswordChar = '*';
                if (showpassword)
                {
                    // Insert a checkbox with label "Show password" 25 px below the textbox
                    CheckBox checkbox = new CheckBox( );
                    checkbox.Checked  = false;
                    checkbox.Location = new Point(11, textBox.Location.Y + 25);
                    checkbox.Width    = inputBox.Width - 22;
                    checkbox.Click   += new EventHandler(checkbox_Click);
                    checkbox.Text     = showpasswordprompt;
                    inputBox.Controls.Add(checkbox);
                }
            }
            else
            {
                textBox.Text = defanswer;
            }

            maskedTextBox              = new MaskedTextBox( );
            maskedTextBox.Mask         = mask;
            maskedTextBox.Location     = textBox.Location;
            maskedTextBox.PasswordChar = textBox.PasswordChar;
            maskedTextBox.Text         = textBox.Text;
            maskedTextBox.Size         = textBox.Size;
            maskedTextBox.AsciiOnly    = ASCIIonly;

            if (usemask)
            {
                inputBox.Controls.Add(maskedTextBox);
            }
            else
            {
                inputBox.Controls.Add(textBox);
            }

            Button okButton = new Button( );
            okButton.DialogResult = DialogResult.OK;
            okButton.Name         = "okButton";
            okButton.Size         = new Size(80, 25);
            okButton.Text         = okcaption;
            okButton.Location     = new Point(width / 2 - 10 - 80, height - 40);
            inputBox.Controls.Add(okButton);

            Button cancelButton = new Button( );
            cancelButton.DialogResult = DialogResult.Cancel;
            cancelButton.Name         = "cancelButton";
            cancelButton.Size         = new Size(80, 25);
            cancelButton.Text         = cancelcaption;
            cancelButton.Location     = new Point(width / 2 + 10, height - 40);
            inputBox.Controls.Add(cancelButton);

            inputBox.AcceptButton = okButton;              // OK on Enter
            inputBox.CancelButton = cancelButton;          // Cancel on Esc
            inputBox.Activate( );
            inputBox.BringToFront( );
            inputBox.Focus( );

            if (usemask)
            {
                maskedTextBox.BringToFront( );                // Bug workaround
                maskedTextBox.Select(0, 0);                   // Move cursor to begin
                maskedTextBox.Focus( );
            }
            else
            {
                textBox.BringToFront( );                // Bug workaround
                textBox.Select(0, 0);                   // Move cursor to begin
                textBox.Focus( );
            }

            #endregion Define Form


            #region Show Dialog and Return Result

            DialogResult result = inputBox.ShowDialog( );
            if (result == DialogResult.OK)
            {
                int rc = ValidateAndShowResult( );
                return(rc);
            }
            else
            {
                if (timeoutelapsed)
                {
                    ValidateAndShowResult( );
                    return(3);
                }
                else
                {
                    return(2);
                }
            }

            #endregion Show Dialog and Return Result
        }