Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente |
lang:csharp:ihm:form:touches [2017/02/06 09:38] – [KeyDown, KeyPress, KeyUp] : KeyChar avec KeyPress. root | lang:csharp:ihm:form:touches [2020/04/28 23:02] (Version actuelle) – mhtml -> html root |
---|
Généralement, c'est l'événement ''KeyPress'' qui s'en occupe. Mais comme le TextBox ne s'occupe que des caractères alphanumériques, il faut utiliser l'événement ''PreviewKeyDown''. | Généralement, c'est l'événement ''KeyPress'' qui s'en occupe. Mais comme le TextBox ne s'occupe que des caractères alphanumériques, il faut utiliser l'événement ''PreviewKeyDown''. |
| |
[[http://stackoverflow.com/questions/1646998/up-down-left-and-right-arrow-keys-do-not-trigger-keydown-event|Source]], {{ :lang:csharp:ihm:form:touches:c_-_up_down_left_and_right_arrow_keys_do_not_trigger_keydown_event_-_stack_overflow.maff |Archive}} | [[http://stackoverflow.com/questions/1646998/up-down-left-and-right-arrow-keys-do-not-trigger-keydown-event|c# - Up, Down, Left and Right arrow keys do not trigger KeyDown event - Stack Overflow]] {{ :lang:csharp:ihm:form:touches:c_-_up_down_left_and_right_arrow_keys_do_not_trigger_keydown_event_-_stack_overflow_2020-04-28_10_55_41_pm_.html |Archive du 29/10/2009 le 28/04/2020}} |
| |
=====KeyDown, KeyPress, KeyUp===== | =====KeyDown, KeyPress, KeyUp===== |
* ''KeyUp'' : comme l'événement ''KeyDown'' sauf que les attributs ''Text'' ou ''Value'' prennent en compte le nouveau caractère. | * ''KeyUp'' : comme l'événement ''KeyDown'' sauf que les attributs ''Text'' ou ''Value'' prennent en compte le nouveau caractère. |
| |
| =====DataGridView===== |
| Dans le cas d'un DataGridView, KeyDown, KeyPress et KeyUp ne marchent pas. |
| |
| En fouillant sur Internet ([[https://social.msdn.microsoft.com/Forums/windows/en-US/db486d50-48f3-405d-bc7a-ad3720d4dd57/datagridview-how-to-capture-a-cells-keypress-event?forum=winformsdatacontrols|DataGridView, how to capture a cell's KeyPress event...]] {{ :lang:csharp:ihm:form:touches:datagridview_how_to_capture_a_cell_s_keypress_event..._2020-04-28_10_55_40_pm_.html |Archive du 09/08/2005 le 28/04/2020}}), la solution suivante est préconisée : |
| <code csharp> |
| private void MyDataGridViewInitializationMethod() |
| { |
| ... |
| dataGridView.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView_EditingControlShowing); |
| } |
| |
| private void dataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) |
| { |
| e.Control.KeyPress += |
| new KeyPressEventHandler(Control_KeyPress); |
| } |
| |
| private void Control_KeyPress(object sender, KeyPressEventArgs e) |
| { |
| if (char.IsNumber(e.KeyChar)) |
| { |
| // your code here |
| } |
| } |
| </code> |
| |
| Mais chez moi, ça ne marchait pas. Par contre, ''PreviewKeyDown'' marche bien. |