DataGridView에서 10 명의 전체 사과를 30 명으로 나누고 싶습니다.
DataGridView는 KeyPreview가 true로 설정된 양식에 있습니다. 사람들의 이름은 readonly로 설정된 DataGridViewTextBoxColumn (Column1)에 표시됩니다. 정수는 빈 DataGridViewTextBoxColumn (Column2)에 입력됩니다. 키를 놓으면 합계가 계산/재 계산되고 column2의 합계가 30이면 양식 확인 버튼이 활성화됩니다 (이 비활성화 된 경우).KeyPress 이벤트 블록 DataGridView의 KeyUp 이벤트
문제는 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 이벤트가 트리거됩니다. 때로는 키 입력이 한 번 트리거됩니다.
KeyUp이 항상 실행 중이 아니며 키 누르기로 처리했을 때만 실행됩니까? – gbianchi
KeyUp은 KeyPress가 첨부되지 않은 경우에만 실행됩니다. –