Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8622571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:05:26+00:00 2026-06-12T07:05:26+00:00

I’ve got this code in a custom class derived from DataGridView: protected override bool

  • 0

I’ve got this code in a custom class derived from DataGridView:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    if (char.IsNumber(Convert.ToChar(keyData)) || 
        char.IsControl(Convert.ToChar(keyData)) || 
        (keyData >= Keys.NumPad0 && keyData <= Keys.NumPad9) ||
        (keyData == Keys.Up) ||
        (keyData == Keys.Down) ||
        (keyData == Keys.Left) ||
        (keyData == Keys.Right) ||
        (keyData == Keys.Home) ||
        (keyData == Keys.PageDown) ||
        (keyData == Keys.PageUp) ||
        (keyData == Keys.Space) ||
        (keyData == Keys.Back) ||
        (keyData == Keys.Decimal))
    {
        return false;
    }
    return true;
}

I can enter data (numbers and .), and tab from cell to cell, but if I hit the “Shift” key, I get, “System.OverflowException was unhandled by user code
Message=Value was either too large or too small for a character.
Source=mscorlib
StackTrace:
at System.Convert.ToChar(Int32 value)…”

I assume this is the line that’s causing the problem:

char.IsControl(Convert.ToChar(keyData))

…but why is Shift problematic, and what should I do to get it to disregard the Shift key (there’s no reason for the user to press the Shift key in the DGV).

UPDATE

I must admit I don’t quite understand quetzalcoatl’s answer, but I tried to apply it this way:

Keys specials = keyData & Keys.Modifiers; 
Keys keycode = keyData & ~Keys.Modifiers;

if (char.IsNumber(Convert.ToChar(keyData)) || 
    //char.IsControl(Convert.ToChar(keyData)) || 
    //(keyData == specials) || <-- didn't work
    //(keyData == keycode) ||  <-- didn't work
    (keyData != specials) ||
    (keyData != keycode) ||
    . . .

…and still get the same error. How can I apply the information he provided to solve the problem?

UPDATE 2

I’m still struggling with this.

I’ve tried this:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    Keys keycode = keyData & ~Keys.Modifiers;

    if (keycode >= Keys.D0 && keycode <= Keys.D9 ||          
        keycode >= Keys.NumPad0 && keycode <= Keys.NumPad9 ||
        (keycode == Keys.Up) ||
        (keycode == Keys.Down) ||
        (keycode == Keys.Left) ||
        (keycode == Keys.Right) ||
    (keyData == Keys.Tab) ||
        (keycode == Keys.Tab) ||
        (keycode == Keys.Home) ||
        (keycode == Keys.PageDown) ||
        (keycode == Keys.PageUp) ||
        (keycode == Keys.Space) ||
        (keycode == Keys.Back) ||
        (keycode == Keys.Decimal))
    {
        return false;
    }
    return true;
}

…and this approach:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        Keys keycode = keyData & ~Keys.Modifiers;

        bool isDigit = keycode >= Keys.D0 && keycode <= Keys.D9 ||
                       keycode >= Keys.NumPad0 && keycode <= Keys.NumPad9;
        bool isControl = (keyData & Keys.Modifiers) != Keys.None;

        if (isDigit ||
            isControl ||
            (keyData == Keys.Up) ||
            (keyData == Keys.Down) ||
            (keyData == Keys.Left) ||
            (keyData == Keys.Right) ||
        (keyData == Keys.Tab) ||
            (keyData == Keys.Home) ||
            (keyData == Keys.PageDown) ||
            (keyData == Keys.PageUp) ||
            (keyData == Keys.Space) ||
            (keyData == Keys.Back) ||
            (keyData == Keys.Decimal))
        {
            return false;
        }
        return true;
}

Both of them solve the Shift key causing an overflow, but both of them prevent a decimal from being entered. I don’t understand that, as Decimal is being (supposedly) explicitly allowed. Everything else explicitly checked for is allowed (Home, PageUp, PageDown, etc.) – it’s just the “.” character that is being barred entry.

UPDATE 3

Using the second approach above and replacing “Keys.Decimal” with “Keys.OemPeriod” works:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    Keys keycode = keyData & ~Keys.Modifiers;

    bool isDigit = keycode >= Keys.D0 && keycode <= Keys.D9 ||
                   keycode >= Keys.NumPad0 && keycode <= Keys.NumPad9;
    bool isControl = (keyData & Keys.Modifiers) != Keys.None;

    if (isDigit ||
        isControl ||
        (keyData == Keys.Up) ||
        (keyData == Keys.Down) ||
        (keyData == Keys.Left) ||
        (keyData == Keys.Right) ||
        (keyData == Keys.Home) ||
        (keyData == Keys.Tab) ||
        (keyData == Keys.PageDown) ||
        (keyData == Keys.PageUp) ||
        (keyData == Keys.Space) ||
        (keyData == Keys.Back) ||
        (keyData == Keys.OemPeriod))
    {
        return false;
    }
    return true;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T07:05:27+00:00Added an answer on June 12, 2026 at 7:05 am

    Do not convert a Keys to char. This does not work. Test if it is a digit with

    Keys code = keyData & ~Keys.Modifiers;
    
    bool isDigit = code >= Keys.D0 && code <= Keys.D9 ||
                   code >= Keys.NumPad0 && code <= Keys.NumPad9
    

    Test if it is a control key with

    bool isControl = (keyData & Keys.Modifiers) != Keys.None;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,
I am doing a simple coin flipping experiment for class that involves flipping a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.