HideSelection

Wed, October 27, 2004, 10:40 AM under MobileAndEmbedded
"[HideSelection] controls whether the current sub-selection is visibly hidden when the control loses focus. Most controls highlight the currently selected contained item and make its background color the selection color. If this property were set to True, then the background color would revert to normal when the control loses focus."

Some Windows Forms controls of the .NET Framework offer the HideSelection property (e.g. TextBox, ListView and TreeView). So, if for example you have a form with the 3 aforementioned controls on it and you clicked on each one of them, you would be able to see which TreeNode is selected, which ListViewItem is selected and what text is selected without further interaction (presuming you set their HideSelection property to false, which is not the default by the way). So the highlighting is preserved after the control loses focus.

Unfortunately, the Compact Framework does not offer the HideSelection property. Furthermore, the default is not consistent: it is true for TreeView but false for ListView!

You would have thought that with CF 2.0 they would offer us the HideSelection property or change the defaults to be consistent, but my suggestion was postponed.

So this entry serves as a container for the code that I use to simulate the HideSelection property (based on SetWindowLong). The sample should work for CF 2.0 as is and if you wish you can access the TreeView handle without resorting to any tricks (Control.Handle is available in CF 2.0). Place the code on a form that also contains a TreeView. It simulates TreeView.HideSelection = false.

' VB
Protected Overrides Sub OnGotFocus(ByVal e As System.EventArgs)
MyBase.OnGotFocus(e)
Static doneOnce As Boolean
If Not TreeView1 Is Nothing Then
TreeView1.Focus()
If doneOnce = False Then
doneOnce = True
Dim hWnd As IntPtr = Win32Api.GetFocus()
Dim lS As Int32 = Win32Api.GetWindowLong(hWnd, -16)
lS = lS Or &H20
Win32Api.SetWindowLong(hWnd, -16, lS)
End If
End If
End Sub

// C#
private bool doneOnce;
protected override void OnGotFocus(System.EventArgs e) {
base.OnGotFocus(e);
if (TreeView1 != null){
TreeView1.Focus();
if (doneOnce == false){
doneOnce = true;
IntPtr hWnd= Win32Api.GetFocus();
Int32 lS = Win32Api.GetWindowLong(hWnd, -16);
lS = lS | 0x20;
Win32Api.SetWindowLong(hWnd, -16, lS);
}
}
}
Comments are closed.