Click here to Skip to main content
15,880,725 members
Home / Discussions / C#
   

C#

 
GeneralRe: C # + Regex help (for a newbie) Pin
Damian Bz27-Dec-17 6:29
Damian Bz27-Dec-17 6:29 
GeneralRe: C # + Regex help (for a newbie) Pin
Damian Bz27-Dec-17 7:37
Damian Bz27-Dec-17 7:37 
GeneralRe: C # + Regex help (for a newbie) Pin
Damian Bz27-Dec-17 9:15
Damian Bz27-Dec-17 9:15 
GeneralRe: C # + Regex help (for a newbie) Pin
Richard MacCutchan27-Dec-17 21:56
mveRichard MacCutchan27-Dec-17 21:56 
GeneralRe: C # + Regex help (for a newbie) Pin
OriginalGriff28-Dec-17 1:51
mveOriginalGriff28-Dec-17 1:51 
GeneralRe: C # + Regex help (for a newbie) Pin
Damian Bz28-Dec-17 6:59
Damian Bz28-Dec-17 6:59 
AnswerRe: C # + Regex help (for a newbie) Pin
Gerry Schmitz28-Dec-17 8:17
mveGerry Schmitz28-Dec-17 8:17 
GeneralRe: C # + Regex help (for a newbie) Pin
Damian Bz28-Dec-17 23:21
Damian Bz28-Dec-17 23:21 
Thanks Gerry,
I'd actually stumbled upon a similar but shorter solution and was working on that before I read your comment.

C#
void textBoxSample_KeyPress(object sender, KeyPressEventArgs e)
    {
        e.Handled = Char.IsPunctuation(e.KeyChar) ||  
                      Char.IsSeparator(e.KeyChar) || 
                      Char.IsSymbol(e.KeyChar);
    }


This works really well when I want to stop any non whole numerical value, and I still use this in conjunction with the Text_Changed event to check for correct range etc.

Where this goes wrong is when I need to allow a "." i.e. 7.2 as the Char.IsPunctuation blocks the "."
But if I remove Char.IsPunctuation it allows characters like \ / ? which crashes my app ! Frown | :(

How can I get round this either by modifying what I have or using your suggested method or other ?
Here's what I have:
C#
private void nicstrength_KeyPress(object sender, KeyPressEventArgs e)
{
    e.Handled = Char.IsSeparator(e.KeyChar) || Char.IsSymbol(e.KeyChar); //|| Char.IsPunctuation(e.KeyChar);

}

private void nicstrength_TextChanged(object sender, EventArgs e)
{
    double ns;
    if (string.IsNullOrEmpty(nicstrength.Text))
        // do nothing
        ;
    else
    {
        if ((nicstrength.Text.Any(chr => char.IsLetter(chr))) || (nicstrength.Text.StartsWith(".")))


        {
            MessageBox.Show("Please enter a number between 1 and 10 inc decimal points i.e 7.2");
            nicstrength.Clear();
            nicstrength.Focus();
        }
        else
        {
            ns = Convert.ToDouble(nicstrength.Text);

            if (!(ns >= 1 && ns <= 10))
            {
                MessageBox.Show("Please enter a number between 1 and 10 inc decimal points i.e 7.2");
                nicstrength.Clear();
                nicstrength.Focus();
            }
        }

    }
}


Many thanks
Damian
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 

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.