What, if any, printable character did the user type based on the values ​​in the given System.Windows.Forms.KeyEventArgs?

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

KeyDown provides me with a KeyEventArgs object with the properties KeyCode, KeyData, KeyValue, modifiers, Alt, Shift, Control.

My first attempt was to treat KeyCode as ascii code, but the KeyCode on my keyboard is 46, period ("."), So I end up printing the period when the user enters the delete key. So, I know that my logic is inadequate.

(For the curious, the problem is that I have my own combobox in the DataGridView control collection, and somehow SOME characters that I print do not trigger KeyPress and TextChanged ComboBox events. These letters include Q, $,% ....

This code will reproduce the problem. Create a form application and replace ctor with this code. Run it and try to enter the letter Q in two combo.

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);
    }
+3
source share
4 answers

Many controls override default key input events. For example, the panel will not respond to them at all by default. As for simple controls, you can try:

protected override bool IsInputKey(Keys keyData) {
    // This snippet informs .Net that arrow keys should be processed in the panel (which is strangely not standard).

    switch (keyData & Keys.KeyCode) {
        case Keys.Left:
            return true;
        case Keys.Right:
            return true;
        case Keys.Up:
            return true;
        case Keys.Down:
            return true;
    }
    return base.IsInputKey(keyData);

}

IsInputKey , . , , , , , , .

, DataGridView ComboBox, . , :

http://www.dotnet247.com/247reference/msgs/29/148332.aspx

:

http://dotnetperls.com/previewkeydown

+3

System.Text.Encoding.ASCII System.Text.Encoding.Default

0

Try:

KeysConverter converter = new KeysConverter();
string key = converter.ConvertTo(e.KeyCode, typeof(string));

, . KeyPress ... ( KeyPreview = true KeyPress) , . , , , , , .

0
source

Just like the idea of ​​throwing it there, if it looks like your DataGridViewhooks for keyboard events before they can reach your child control, can you provide your own handlers for keyboard events that interest you directly on DataGridView, and in the method (s) handler (1) suppress normal event handling DataGridViewand / or (2) manually pass the event along with your child control?

0
source

Source: https://habr.com/ru/post/1698224/


All Articles