What, if any, printable character did a user type based on the values in a given System.Windows.Form

Posted by Corey Trager on Stack Overflow See other posts from Stack Overflow or by Corey Trager
Published on 2008-10-13T19:17:59Z Indexed on 2010/04/11 12:03 UTC
Read the original article Hit count: 227

Filed under:
|
|

As a workaround for a problem, I think I have to handle KeyDown events to get the printable character the user actually typed.

KeyDown supplies me with a KeyEventArgs object with the properities KeyCode, KeyData, KeyValue, Modifiers, Alt, Shift, Control.

My first attempt was just to consider the KeyCode to be the ascii code, but KeyCode on my keyboard is 46, a period ("."), so I end up printing a period when the user types the delete key. So, I know my logic is inadequate.

(For those who are curious, the problem is that I have my own combobox in a DataGridView's control collection and somehow SOME characters I type don't produce the KeyPress and TextChanged ComboBox events. These letters include Q, $, %....

This code will reproduce the problem. Generate a Form App and replace the ctor with this code. Run it, and try typing the letter Q into the two comboxes.

public partial class Form1 : Form
{
    ComboBox cmbInGrid;
    ComboBox cmbNotInGrid;
    DataGridView grid;

    public Form1()
    {
        InitializeComponent();

        grid = new DataGridView();

        cmbInGrid = new ComboBox();
        cmbNotInGrid = new ComboBox();

        cmbInGrid.Items.Add("a");
        cmbInGrid.Items.Add("b");
        cmbNotInGrid.Items.Add("c");
        cmbNotInGrid.Items.Add("d");

        this.Controls.Add(cmbNotInGrid);
        this.Controls.Add(grid);
        grid.Location = new Point(0, 100);
        this.grid.Controls.Add(cmbInGrid);
    }

© Stack Overflow or respective owner

Related posts about .NET

Related posts about datagridview