DataGridViewの10人のリンゴの間で30個のリンゴを分割したいと考えています。
DataGridViewは、KeyPreviewがtrueに設定されたフォームにあります。人の名前は、読み取り専用に設定されたDataGridViewTextBoxColumn(Column1)に表示されます。整数は空のDataGridViewTextBoxColumn(Column2)に入力されます。 キーが離されると、合計が計算/再計算され、column2の合計が30の場合、フォームのOKボタンが有効になります(elseが無効になります)。KeyPressイベントブロックDataGridViewのKeyUpイベント.net
問題はkeyEventsに関するものです。 KeyPressイベントをバインドすると、KeyUpはトリガーされません。
// Bind events to DataGridViewCell
private void m_DataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control != null)
{
e.Control.KeyUp -= m_DataGridView_KeyUp;
e.Control.KeyPress -= m_DataGridView_KeyPress;
e.Control.KeyUp += m_DataGridView_KeyUp;
e.Control.KeyPress += m_DataGridView_KeyPress;
}
}
//Only accept numbers
private void m_GridView_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar >= 48 && e.KeyChar <= 57) || e.KeyChar == 8)
{
e.Handled = false;
}
else
{
e.Handled = true;
}
}
// Sum the apples in column2
private void m_DataGridView_KeyUp(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 1 && e.RowIndex > 0)
{
int count = 0;
int parser = 0;
foreach (DataGridViewRow item in this.m_DataGridView.Rows)
{
if (item.Cells[1].Value != null)
{
int.TryParse(item.Cells[1].Value.ToString(), out parser);
count += parser;
}
}
//make the ok button enabled
m_buttonDividedApplen.Enabled = (count == 30);
}
}
このストーリーの問題は、見知らぬ人と見知らぬ人になります。私がセルを切り替えると、keyupイベントがトリガされます。キーアップがトリガーONE時間を取得することがあります。
KeyUpは常に実行されていませんか、またはキー入力時にKeyUpを処理した場合のみですか? – gbianchi
KeyUpは、KeyPressが添付されていない場合にのみ実行されます。 –