Click here to Skip to main content
15,895,256 members
Home / Discussions / C#
   

C#

 
AnswerRe: C # + Regex help (for a newbie) Pin
Gerry Schmitz28-Dec-17 8:17
mveGerry Schmitz28-Dec-17 8:17 
You can suppress certain keystrokes in the first place without having to resort to some field edits. The following is straight from the docs. (BTW all this is easier in WPF).
// Boolean flag used to determine when a character other than a number is entered. 
        private bool nonNumberEntered = false;

        // Handle the KeyDown event to determine the type of character entered into the control. 
        private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            // Initialize the flag to false.
            nonNumberEntered = false;

            // Determine whether the keystroke is a number from the top of the keyboard. 
            if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
            {
                // Determine whether the keystroke is a number from the keypad. 
                if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
                {
                    // Determine whether the keystroke is a backspace. 
                    if(e.KeyCode != Keys.Back)
                    {
                        // A non-numerical keystroke was pressed. 
                        // Set the flag to true and evaluate in KeyPress event.
                        nonNumberEntered = true;
                    }
                }
            }
            //If shift key was pressed, it's not a number. 
            if (Control.ModifierKeys == Keys.Shift) {
                nonNumberEntered = true;
            }
        }

        // This event occurs after the KeyDown event and can be used to prevent 
        // characters from entering the control. 
        private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            // Check for the flag being set in the KeyDown event. 
            if (nonNumberEntered == true)
            {
                // Stop the character from being entered into the control since it is non-numerical.
                e.Handled = true;
            }
        }
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal

GeneralRe: C # + Regex help (for a newbie) Pin
Damian Bz28-Dec-17 23:21
Damian Bz28-Dec-17 23:21 
GeneralRe: C # + Regex help (for a newbie) Pin
Damian Bz29-Dec-17 1:48
Damian Bz29-Dec-17 1:48 
GeneralRe: C # + Regex help (for a newbie) Pin
Gerry Schmitz29-Dec-17 6:03
mveGerry Schmitz29-Dec-17 6:03 
QuestionSet datagrid cell value individually and dynamically Pin
Hervend26-Dec-17 22:17
Hervend26-Dec-17 22:17 
AnswerRe: Set datagrid cell value individually and dynamically Pin
Richard MacCutchan26-Dec-17 22:37
mveRichard MacCutchan26-Dec-17 22:37 
GeneralRe: Set datagrid cell value individually and dynamically Pin
Hervend26-Dec-17 22:52
Hervend26-Dec-17 22:52 
GeneralRe: Set datagrid cell value individually and dynamically Pin
Pete O'Hanlon26-Dec-17 22:56
mvePete O'Hanlon26-Dec-17 22:56 
GeneralRe: Set datagrid cell value individually and dynamically Pin
Hervend26-Dec-17 23:37
Hervend26-Dec-17 23:37 
GeneralRe: Set datagrid cell value individually and dynamically Pin
Richard MacCutchan26-Dec-17 23:29
mveRichard MacCutchan26-Dec-17 23:29 
GeneralRe: Set datagrid cell value individually and dynamically Pin
Hervend26-Dec-17 23:35
Hervend26-Dec-17 23:35 
QuestionParentheses vs curly brackets Pin
User9874326-Dec-17 12:17
professionalUser9874326-Dec-17 12:17 
AnswerRe: Parentheses vs curly brackets Pin
Gerry Schmitz26-Dec-17 12:36
mveGerry Schmitz26-Dec-17 12:36 
GeneralRe: Parentheses vs curly brackets Pin
User9874326-Dec-17 12:54
professionalUser9874326-Dec-17 12:54 
GeneralRe: Parentheses vs curly brackets Pin
Gerry Schmitz26-Dec-17 12:57
mveGerry Schmitz26-Dec-17 12:57 
GeneralRe: Parentheses vs curly brackets Pin
User9874326-Dec-17 15:00
professionalUser9874326-Dec-17 15:00 
GeneralRe: Parentheses vs curly brackets Pin
BillWoodruff26-Dec-17 15:36
professionalBillWoodruff26-Dec-17 15:36 
GeneralRe: Parentheses vs curly brackets Pin
User9874326-Dec-17 17:59
professionalUser9874326-Dec-17 17:59 
GeneralRe: Parentheses vs curly brackets Pin
BillWoodruff27-Dec-17 19:40
professionalBillWoodruff27-Dec-17 19:40 
GeneralRe: Parentheses vs curly brackets Pin
User987432-Jan-18 23:13
professionalUser987432-Jan-18 23:13 
GeneralRe: Parentheses vs curly brackets Pin
Gerry Schmitz28-Dec-17 7:08
mveGerry Schmitz28-Dec-17 7:08 
GeneralRe: Parentheses vs curly brackets Pin
User987432-Jan-18 22:29
professionalUser987432-Jan-18 22:29 
GeneralRe: Parentheses vs curly brackets Pin
Gerry Schmitz3-Jan-18 5:13
mveGerry Schmitz3-Jan-18 5:13 
GeneralRe: Parentheses vs curly brackets Pin
Pete O'Hanlon26-Dec-17 22:26
mvePete O'Hanlon26-Dec-17 22:26 
AnswerRe: Parentheses vs curly brackets Pin
BillWoodruff26-Dec-17 15:31
professionalBillWoodruff26-Dec-17 15:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.