[2005] Select Panel, get Focus Rectangle
I have a form that adds one or more custom controls to it that are inherited from the Panel class. I need the custom control to be selectable so the user can change the properties of individual controls. The Panel class is not selectable, even though it has a Public Select Method :ehh: . Can anyone suggest a method for allowing the custom control to be selectable and also display a focus rectangle?
Re: [2005] Select Panel, get Focus Rectangle
The Panel class a a Select method because it's inherited from Control. Certain members have no effect for certain controls. I've never made an unselectable control selectable but I have done it the other way around. here's my code for a Button that cannot receive focus:
vb.net Code:
''' <summary>
''' A button control that cannot receive focus.
''' </summary>
Public Class UnselectableButton
Inherits System.Windows.Forms.Button
#Region " Constructors "
''' <summary>
''' Initialises a new instance of the <see cref="UnselectableButton"/> class.
''' </summary>
Public Sub New()
MyBase.New()
'The button cannot receive focus.
Me.SetStyle(ControlStyles.Selectable, False)
End Sub
#End Region 'Constructors
#Region " Constants "
''' <summary>
''' The message received from Windows when the form is activated by a mouse click.
''' </summary>
Private Const WM_MOUSEACTIVATE As Integer = &H21
''' <summary>
''' The reply sent to Windows to indicate that the form should not be activated.
''' </summary>
Private Const MA_NOACTIVATE As Integer = 3
#End Region 'Constants
#Region " Variables "
''' <summary>
''' Corresponds to the MA_NOACTIVATE window process reply.
''' </summary>
Private ReadOnly noActivate As New IntPtr(MA_NOACTIVATE)
#End Region 'Variables
#Region " Methods "
''' <summary>
''' Processes Windows messages.
''' </summary>
''' <remarks>
''' Informs Windows not to activate the window when it is clicked with the mouse. Note that this behaviour applies to the client area of the window only.
''' </remarks>
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
MyBase.WndProc(m)
If m.Msg = WM_MOUSEACTIVATE Then
'Tell Windows not to activate the window on a mouse click.
m.Result = noActivate
End If
End Sub
#End Region 'Methods
End Class
Presumably you'd have to do something similar for your control. You'd set the Selectable style to True, but I'm not sure what else. You'd probably have to take responsibility for drawing the focus rectangle at least.
Re: [2005] Select Panel, get Focus Rectangle
That's an interesting idea. I'll see what I can do. If I can't get it to work I'll just improvise. Thanks again John!