2017-04-20 10 views
1

이상한 문제가 있습니다. 윈폼 응용 프로그램에서 나는 코드가 잘 작동Datagridview 안의 Tab 키의 코드에서 winforms의 다음 컨트롤을 선택하는 방법

class CalibDataGridView : System.Windows.Forms.DataGridView 
    { 
     protected override bool ProcessCmdKey(ref Message msg, Keys keyData) 
     { 
      int row = 0; 
      int col = 0; 
      // return base.ProcessCmdKey(ref msg, keyData); 
      switch (keyData) 
      { 
       case Keys.Down: 
        return true; 
       case Keys.Up: 
        return true; 

       case Keys.Right: 
        row = this.CurrentCell.RowIndex; 
        col = this.CurrentCell.ColumnIndex; 

        while (true) 
        { 
         col++; 
         if (col == this.Columns.Count) 
         { 
          row++; 
          col = 0; 
         } 

         if (row == this.Rows.Count) 
         { 
          col = 0; 
          row = 0; 
         } 

         if (!this[col, row].ReadOnly && this[col, row].Visible) break; 
        } 

        this.CurrentCell = this[col, row]; 

        return true; 

       case Keys.Left: 
        row = this.CurrentCell.RowIndex; 
        col = this.CurrentCell.ColumnIndex; 

        while (true) 
        { 
         col--; 
         if (col < 0) 
         { 
          row--; 
          col = this.Columns.Count - 1; 
         } 

         if (row < 0) 
         { 
          row = this.Rows.Count - 1; 
          col = this.Columns.Count - 1; 
         } 

         if (!this[col, row].ReadOnly && this[col, row].Visible) break; 
        } 

        this.CurrentCell = this[col, row]; 

        return true; 

       case Keys.Enter: 
        row = this.CurrentCell.RowIndex; 
        col = this.CurrentCell.ColumnIndex; 

        while (true) 
        { 

          row++; 
         if (row == this.Rows.Count) 
         { 
          row = 0; 
         } 

         if (!this[col, row].ReadOnly && this[col, row].Visible) break; 
        } 

        this.CurrentCell = this[col, row]; 

        return true; 

       default: 
        return base.ProcessCmdKey(ref msg, keyData); 
      } 
     } 
    } 

내가 화살표 키를 사용하여 그 안에 이동 및 키 입력을위한 몇 가지 코드를 구현 DataGridView에를 ...,하지만 내가하는 데 문제가 있어요로 이동하는 기능을 구현 Tab 키를 누르면 다음 컨트롤. 내가있는 DataGridView의 PreviewKeyDown 기능의 일부 코드 추가 : 배경 변경에 어딘가에 트리거 될 때

이 마지막 코드는 이상한 방식으로 작동
private void dgvRepeatability1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) 
     { 
      if (e.KeyCode == Keys.PageDown && tbcMain.SelectedIndex > 0) 
      { 
       tbcMain.SelectedIndex--; //changes tab page 
      } 
      else if (e.KeyCode == Keys.PageUp && tbcMain.SelectedIndex < 7) 
      { 

       tbcMain.SelectedIndex++; 
      } 
      else if (e.KeyCode == Keys.Tab) //does not work like it should!!!! 
      { 
       Console.Beep(1000, 200); 
       this.SelectNextControl((Control)sender, true, true, true, true); 

      } 
     } 

... 그것 같이 초점을해야합니다 (경고음 및 다음 제어 변경 배경 색상 때문에 나는 그것이 집중되고있는 것을 볼 수있다.) 그러나 나는 전체 형식에 초점을 잃는다 ... 나는 Tab 키 또는 화살표 키 또는 다른 키를 누를 수 있고 아무 일도 일어나지 않는다. ...

만약 내가 폼을 최소화하고 최대화한다면 다시 초점은 처음부터해야하는 것처럼 다음 컨트롤로 이동하고 모든 것이 제대로 작동해야합니다.

또한 .Select() 및 .Focus()를 사용하여 포커스를 변경했지만 작동하지 않았습니다.

미리 도움을 청하십시오!

private void dataGridView1_KeyDown(object sender, KeyEventArgs e) 
{ 
    if (e.KeyCode == Keys.Tab) 
    { 
     e.Handled = true; 
     this.SelectNextControl((Control)sender, true, true, true, true); 
    } 

당신은 또한 다음 컨트롤을 직접 찾을 수 있습니다 :

답변

1

당신은 SelectNextControl을 사용할 수 있습니다

private void dataGridView1_KeyDown(object sender, KeyEventArgs e) 
{ 
    if (e.KeyCode == Keys.Tab) 
    { 
     e.Handled = true; 
     var dataGrid = (DataGridView) sender; 
     var tabIndex = dataGrid.TabIndex; 
     var controls = this.Controls.Cast<Control>().Where(r => r.TabIndex > tabIndex); 
     if (controls.Any()) 
     { 
      controls.OrderBy(r => r.TabIndex).First().Select(); 
     } 
     else 
     { 
      this.Controls.Cast<Control>() 
         .Where(r => r.TabIndex <= tabIndex) 
         .OrderBy(r => tabIndex) 
         .First() 
         .Select(); 
     } 
    } 
} 
+0

감사합니다! 그것은 실제로 효과가있었습니다. 간단히 변경할 수 있습니다 : e.Handled = true; this.SelectNextControl ((Control) sender, true, true, true, true); 문제가 PreviewKeyDown 함수에 있다고 생각합니다 ... – Pajkec

+0

아, 저에게는 효과가없는 것 같았지만, 기쁘게 생각합니다. 나는 미래의 독자들에 대한 나의 대답을 편집 할 것이다 – EpicKip

+0

@Pajkec 아 .. 그것을 시험해 보았다. 그리고 나에게도 일하고있다. 나는 단지 1 값 false haha를 가졌다. 나는 어리 석다. – EpicKip