編集可能なdatagridviewセルに入力できるのは1文字だけにする必要があります(他のすべての列、奇数番号の列は編集可能です)。これらのセルの1つにユーザーが2番目の文字を追加した場合、カーソルは次のセルに移動し、その2番目の値をそこに置く必要があります(そのキーをもう一度押すと、下に移動します)。グリッドの下部(12行目)にある場合は、行0に移動し、2列も右に移動する必要があります。
私はこれをやってみました:
private void dataGridViewPlatypus_KeyDown(object sender, KeyEventArgs e) {
var currentCell = dataGridViewPlatypus.CurrentCell;
int currentCol = currentCell.ColumnIndex;
int currentRow = currentCell.RowIndex;
if (currentCell.Value.ToString().Length > 0) {
if (currentRow < 11) {
dataGridViewPlatypus.CurrentCell.RowIndex = currentRow+1;
} else if (currentRow == 11) {
currentCell.RowIndex = 0;
currentCell.ColumnIndex = currentCell.ColumnIndex + 2;
dataGridViewPlatypus.CurrentCell = currentCell;
}
}
}
...しかし、RowIndexとColumnIndexは読み取り専用であるため、割り当てることができないエラーメッセージが表示されます。
では、どうすればこれを達成できますか?
警告:現在、編集可能な最後の列の下部にある場合は、列1に移動するためのロジックも追加する必要があることを知っています。
アップデート
tergiverの答えから、これは私がこれまでに得たものですが、次のセルに進む方法がわかりません。
protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
if (this.ActiveControl == dataGridViewPlatypus)
{
var currentCell = dataGridViewPlatypus.CurrentCell;
if (currentCell.Value.ToString().Length == 1)
{
;//Now what?
}
}
return base.ProcessCmdKey(ref msg, keyData);
}
更新2
皆さんありがとう; これは、私がそれをほぼ機能させるために使用しているものです(ユーザーがキーを押したままにして、その値を後続のセルに継続的に入力できるようにしたい):
private void dataGridViewPlatypus_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) {
int columnIndex = (((DataGridView)(sender)).CurrentCell.ColumnIndex);
if (columnIndex % 2 == 1) {
e.Control.KeyDown -= TextboxNumeric_KeyDown;
e.Control.KeyDown += TextboxNumeric_KeyDown;
e.Control.KeyUp -= TextboxNumeric_KeyUp;
e.Control.KeyUp += TextboxNumeric_KeyUp;
}
}
private void TextboxNumeric_KeyDown(object sender, KeyEventArgs e) {
var tb = sender as TextBox;
if (tb != null) {
tb.MaxLength = 1;
}
}
// TODO: Now need to find a way to be able to just press down once
private void TextboxNumeric_KeyUp(object sender, KeyEventArgs e) {
var tb = sender as TextBox;
if (tb != null && tb.TextLength >= 1) {
if (dataGridViewPlatypus.CurrentCell.RowIndex != dataGridViewPlatypus.Rows.Count - 1) {
dataGridViewPlatypus.CurrentCell = dataGridViewPlatypus[
dataGridViewPlatypus.CurrentCell.ColumnIndex,
dataGridViewPlatypus.CurrentCell.RowIndex + 1];
} else { // on last row
this.dataGridViewPlatypus.CurrentCell = this.dataGridViewPlatypus.CurrentCell.ColumnIndex != dataGridViewPlatypus.Columns.Count - 1 ? this.dataGridViewPlatypus[this.dataGridViewPlatypus.CurrentCell.ColumnIndex + 2, 0] : this.dataGridViewPlatypus[1, 0];
}
}
}