Microsoft Communities

Welcome to WindowsClient.net | Sign in | Join

Here are some frequently asked questions about Windows Forms and their answers.

Windows Forms FAQs

Why does a column with custom style in a DataGrid not receive focus when Tab is pressed to enter the column?

This behavior can be seen when you embedded a control like a TextBox or ComboBox in your derived GridColumnStyle. If you press the Tab key slowly, you may see the cell get focus on the KeyDown and the cell lose focus on the KeyUp. One way to avoid this problem is to subclass the embedded control, and override its WndProc method, ignoring the KeyUp.

[C#]

using System.Windows.Forms;

public class CustomComboBox : ComboBox
{
  private const int WM_KEYUP = 0x101;

  protected override void WndProc( ref Message m )
  {
    if ( m.Msg == WM_KEYUP ) return; // ignore the KeyUp
    base.WndProc( ref m );
  }
}

[Visual Basic]

Public Class CustomTextBox
  Inherits TextBox
  Private WM_KEYUP As Integer = &H101

  Protected Overrides Sub WndProc(ByRef m As Message)
    If m.Msg = WM_KEYUP Then
      Return ' ignore the KeyUp
    End If
    MyBase.WndProc(m)
  End Sub
End Class

Contributed from George Shepherd's Windows Forms FAQ



Page view counter