Question Help with Button selection at runtime?!

arijit08.b

Member
Joined
Nov 1, 2009
Messages
11
Programming Experience
1-3
I use Microsoft Visual Basic Express edition 2008 on my Windows XP SP2 and in it, in a Windows Form project, I add a button to my default form and set it's flatstyle to flat and borderwidth to 0. At runtime, when I click that button, I get a black color hollow box on that button at it's center...

If you don't understand what I mean, take a look at my attachment...

Please Help QUICKLY!!!
NOTE:


As you can see from the image, even my FORMBORDERSTYLE has been changed, it is because of THAT?
 

Attachments

  • buttonselected.JPG
    buttonselected.JPG
    46.5 KB · Views: 36

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,493
Location
Sydney, Australia
Programming Experience
10+
You're using a Button, so that's showing that it has focus. You don't want that Button to receive focus at all, so you'll have to customise it. Here's some code for a Button that can't receive focus:
VB.NET:
''' <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 " Variables "

    ''' <summary>
    ''' Corresponds to the MA_NOACTIVATE window process reply.
    ''' </summary>
    Private Shared ReadOnly noActivate As New IntPtr(NativeConstants.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 = NativeConstants.WM_MOUSEACTIVATE Then
            'Tell Windows not to activate the window on a mouse click.
            m.Result = noActivate
        End If
    End Sub

    Protected Overrides Sub OnClick(ByVal e As System.EventArgs)
        MyBase.OnClick(e)
    End Sub

#End Region 'Methods

End Class
You'll also need this:
VB.NET:
''' <summary>
''' Contains Win32 constant declarations.
''' </summary>
Friend NotInheritable Class NativeConstants

    ''' <summary>
    ''' Specifies that a window cannot be activated.
    ''' </summary>
    ''' <remarks>
    ''' If a child window has this style, clicking it does not cause its top-level parent to activate. Although a window that has this style will still receive mouse events, neither it nor its child windows can get the focus.
    ''' </remarks>
    Public Const WS_EX_NOACTIVATE As Integer = &H8000000

    ''' <summary>
    ''' This message is sent when the cursor is in an inactive window and the user presses a mouse button.
    ''' </summary>
    Public Const WM_MOUSEACTIVATE As Integer = &H21

    ''' <summary>
    ''' Does not activate the window, and does not discard the mouse message.
    ''' </summary>
    ''' <remarks>
    ''' This is one of the possible return values when the <see cref="WM_MOUSEACTIVATE"/> notification is received.
    ''' </remarks>
    Public Const MA_NOACTIVATE As Integer = 3

    ''' <summary>
    ''' Displays a window in its most recent size and position. This value is similar to <b>SW_SHOWNORMAL</b>, except the window is not actived.
    ''' </summary>
    ''' <remarks>
    ''' This is one of the possible values for the <b>nCmdShow</b> argument of the <see cref="NativeMethods.ShowWindow">ShowWindow</see> method.
    ''' </remarks>
    Public Const SW_SHOWNOACTIVATE As Integer = 4

End Class
 

arijit08.b

Member
Joined
Nov 1, 2009
Messages
11
Programming Experience
1-3
hey thanx SO much man after i put all of this in a new blank class and saved my project, it still isn't appearing in the toolbox...
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,493
Location
Sydney, Australia
Programming Experience
10+
I don't know what to tell you. It works for me. I just created a new project, created two new classes and pasted that code in, built the project and there it was.
 

Attachments

  • UnselectableButton.PNG
    UnselectableButton.PNG
    82.3 KB · Views: 25

arijit08.b

Member
Joined
Nov 1, 2009
Messages
11
Programming Experience
1-3
there you go, you've just answered me, i didn't BUILD that project... hope it works after building though...

nice interface you've got by the way, is it VS 2010 on Win7 OS?
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,493
Location
Sydney, Australia
Programming Experience
10+
nice interface you've got by the way, is it VS 2010 on Win7 OS?
VS 2008 Team Suite on Windows 7.

Did you add those classes to your current project? Components will only show up in the Toolbox automatically for the solution in which they are declared. If you want to use them in other solutions then you need to add them to the Toolbox manually, as with any other Microsoft or third-party component.
 
Top Bottom