2014-06-12 289 views
1

我期待的是当我完成编辑单元格并单击输入时,光标将聚焦到指定的单元格。PreviewKeyDown事件触发两次

在我的表单中,我期望游标会依次关注单元格列索引5,2,3。 之后的下一行列索引将为5.

但是,PreviewKeyDown事件ID会处理两次。

所以我通过了我想要的第二步,并最终得到一个错误。

这是到目前为止,我已经试过执行:

void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) 
{ 
    TextBox txtCell = e.Control as TextBox; 
    if (txtCell != null) 
    { 
     txtCell.PreviewKeyDown += new PreviewKeyDownEventHandler(txtCell_PreviewKeyDown); 
     txtCell.PreviewKeyDown += new PreviewKeyDownEventHandler(txtCell_PreviewKeyDown); 

     txtCell.KeyDown += new KeyEventHandler(txtCell_KeyDown); 
     txtCell.KeyDown += new KeyEventHandler(txtCell_KeyDown); 
    } 
} 

void txtCell_KeyDown(object sender, KeyEventArgs e) 
{ 
    try 
    { 
     TextBox tCell = (TextBox)sender; 

     if (dataGridView1.CurrentCell.ColumnIndex == 5) 
     { 
      if (e.KeyCode == Keys.Return) 
      { 
       e.Handled = true; 
      } 
     } 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.Message); 
    } 
} 

void txtCell_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) 
{ 
    try 
    { 
     if (e.KeyCode == Keys.Return) 
     { 
      int iColumn = dataGridView1.CurrentCell.ColumnIndex; 
      int iRow = dataGridView1.CurrentCell.RowIndex; 

      if (iColumn == 5) 
      { 
       dataGridView1.Focus(); 
       dataGridView1.CurrentCell = dataGridView1[2, iRow]; 

       //-----I want to test the focus across the cell or not. 
       dataGridView1.CurrentCell.Value = "123"; 
      } 
      if (iColumn == 2) 
      { 
       dataGridView1.Focus(); 
       dataGridView1.CurrentCell = dataGridView1[3, iRow]; 

       //-----I want to test the focus across the cell or not. 
       dataGridView1.CurrentCell.Value = "123"; 
      } 
      if (iColumn == 3) 
      { 
       dataGridView1.Focus(); 
       dataGridView1.CurrentCell = dataGridView1[5, iRow + 1]; 

       //-----I want to test the focus across the cell or not. 
       dataGridView1.CurrentCell.Value = "123"; 
      } 
     } 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.Message); 
    } 
} 
+0

这是wpf吗?如果是这样,您需要将事件标记为已处理。 – paqogomez

+0

是的,它是窗口窗体应用程序。 需要什么? 我只是一个初学者。 你能解释一下吗? 感谢您的帮助。 –

+0

这是一个winform还是WPF? –

回答

0

谢谢你,Nimesh。
我也不明白为什么。 但我解决了这样......

public Form3() 
    { 
     InitializeComponent(); 
     dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing); 
    } 

    bool flag = false; 

    void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) 
    { 
     TextBox txtCell = e.Control as TextBox; 
     if (txtCell != null) 
     { 
      txtCell.PreviewKeyDown -= new PreviewKeyDownEventHandler(txtCell_PreviewKeyDown); 
      txtCell.PreviewKeyDown += new PreviewKeyDownEventHandler(txtCell_PreviewKeyDown); 

      txtCell.KeyDown -= new KeyEventHandler(txtCell_KeyDown); 
      txtCell.KeyDown += new KeyEventHandler(txtCell_KeyDown); 
     } 
    } 

    void txtCell_KeyDown(object sender, KeyEventArgs e) 
    { 
     try 
     { 
      TextBox tCell = (TextBox)sender; 

      if (dataGridView1.CurrentCell.ColumnIndex == 5) 
      { 
       if (e.KeyCode == Keys.Return) 
       { 
        e.Handled = true; 
       } 
      } 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 
    } 

    void txtCell_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) 
    { 
     TextBox tCell = (TextBox)sender; 

     if (!flag) 
     { 
      flag = true; 

      if (e.KeyCode == Keys.Return) 
      { 
       //e.SuppressKeyPress = true; 
       int iColumn = dataGridView1.CurrentCell.ColumnIndex; 
       int iRow = dataGridView1.CurrentCell.RowIndex; 

       if (iColumn == 5) 
       { 
        dataGridView1.Focus(); 
        dataGridView1.CurrentCell = dataGridView1[2, iRow]; 
        dataGridView1.CurrentCell.Value = "123"; 
        iColumn = 0; 
        iRow = 0; 
        flag = false; 
        return; 
       } 
       if (iColumn == 2) 
       { 
        dataGridView1.Focus(); 
        dataGridView1.CurrentCell = dataGridView1[3, iRow]; 
        dataGridView1.CurrentCell.Value = "123"; 
        iColumn = 0; 
        iRow = 0; 
        flag = false; 
        return; 
       } 
       if (iColumn == 3) 
       { 
        dataGridView1.Focus(); 
        dataGridView1.CurrentCell = dataGridView1[5, iRow + 1]; 
        dataGridView1.CurrentCell.Value = "123"; 
        iColumn = 0; 
        iRow = 0; 
        flag = false; 
        return; 
       } 
       flag = false; 
       return; 
      } 
      flag = false; 
      return; 
     } 
     flag = false; 
     return; 
    } 

    private void Form3_Load(object sender, EventArgs e) 
    { 
     dataGridView1.Focus(); 
     dataGridView1.CurrentCell = dataGridView1.Rows[0].Cells[5]; 
    } 
0

更改下面的代码。您正在创建两次处理程序,并且在添加新处理程序之前应删除处理程序。它将删除已经创建的处理程序(如果有)

void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) 
{ 
    TextBox txtCell = e.Control as TextBox; 
    if (txtCell != null) 
    { 
     txtCell.PreviewKeyDown -= new PreviewKeyDownEventHandler(txtCell_PreviewKeyDown); 
     txtCell.PreviewKeyDown += new PreviewKeyDownEventHandler(txtCell_PreviewKeyDown); 

     txtCell.KeyDown -= new KeyEventHandler(txtCell_KeyDown); 
     txtCell.KeyDown += new KeyEventHandler(txtCell_KeyDown); 
    } 
} 
+0

我试过了。 但它仍然是一样的。 –

+0

你是怎么知道你的代码执行两次该事件的?我的意思是你使用F11/F10键调试你的代码?在该事件中放置一行'Console.WriteLine(DateTime.Now.ToString());'并检入输出窗口。当您按任意键时,您的代码是否打印日期时间两次?并且不要按alt + tab或ctrl + tab等其他键来切换窗口。改为使用鼠标。 – Shell

+0

是的,我使用调试和F10。 –