Link to home
Start Free TrialLog in
Avatar of donmghi
donmghi

asked on

VB.NET 2008 Radio Button Double-Click

I have three radio buttons representing different finishes on a product.  The user can select any one of the finishes or can select to have no finish by not selecting a button.  My space is limited so I am not able to add a "None" option as  a way to "deselect" a finish.

I have read that an overload can be added to the radio button to capture the double click event. I am already using a custom radio button, so I just need to add the overload to my custom control.

My control currently:

Imports System.Drawing
Public Interface IDisabled
    Property BackColorDisabled() As Color
    Property ForeColorDisabled() As Color
End Interface

Imports System.Drawing
Imports System.Windows.Forms

Public Class RadioEx
    Inherits System.Windows.Forms.RadioButton
    Implements IDisabled

    Private mblnEnabled As Boolean = True
    Private mclrBackColorDisabled As Color = Color.White
    Private mclrForeColorDisabled As Color = Color.Black

    Public Property BackColorDisabled() As System.Drawing.Color Implements IDisabled.BackColorDisabled
        Get
            Return mclrBackColorDisabled
        End Get
        Set(ByVal Value As System.Drawing.Color)
            mclrBackColorDisabled = Value
        End Set
    End Property

    Public Property ForeColorDisabled() As System.Drawing.Color Implements IDisabled.ForeColorDisabled
        Get
            Return mclrForeColorDisabled
        End Get
        Set(ByVal Value As System.Drawing.Color)
            mclrForeColorDisabled = Value
        End Set
    End Property

    Public Shadows Property Enabled() As Boolean
        Get
            Return mblnEnabled
        End Get
        Set(ByVal Value As Boolean)
            'Keeps the current Back/Fore color
            Static sclrBackColor As System.Drawing.Color
            Static sclrForeColor As System.Drawing.Color
            Static sblnTabStop As Boolean

            If sclrBackColor.IsEmpty Then
                sclrBackColor = Me.BackColor
                sclrForeColor = Me.ForeColor
                sblnTabStop = Me.TabStop
            End If

            'Stop looping in itself
            Static sblnIsProcessing As Boolean
            If sblnIsProcessing Then Exit Property
            sblnIsProcessing = True

            mblnEnabled = Value
            If mblnEnabled Then
                Me.AutoCheck = True
                Me.TabStop = sblnTabStop
            Else
                Me.AutoCheck = False
                Me.TabStop = False
            End If
            Me.Invalidate()

            sblnIsProcessing = False
        End Set
    End Property

    Private Sub RadioButtonEx_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        If Me.Enabled Then
            If Me.Checked Then
                e.Graphics.DrawImage(My.Resources.radio_yellow_check_copy, 0, 0, 14, 14)
            Else
                e.Graphics.DrawImage(My.Resources.radio_yellow_nocheck_copy, 0, 0, 14, 14)
            End If
        Else
            If Me.Checked Then
                e.Graphics.DrawImage(My.Resources.radio_white_check_copy, 0, 0, 14, 14)
            Else
                e.Graphics.DrawImage(My.Resources.radio_white_nocheck_copy, 0, 0, 14, 14)
            End If
        End If
    End Sub
End Class
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

For me, the user comes first. And your idea forgets the user.

A radio button is an intuitive thing to use for most users. Double clicking to deactivate a radio button is not intuitive at all.

You have a problem as a programmer, not enough space for a "None" radio button. This is your problem, not the user's problem. Trying to change the way a radio button function won't solve your problem, it will make it worse because most users won't have the proper reflex. Even if you tell them to double-click, they will forget about that, because none of the 1000 radio buttons they have used in the past behave that way.

This is your problem, solve it, don't let the users solve it by something that is awkward for them.

Find that space. What is one more radio button on a form. Make the font .5 smaller, you will probably find that space. Isn't is possible to split the display in 2 tabs? Is it possible to replace a ListBoxt by a ComboBox? What about getting rid of some fancy logo that has nothing to do with the data?

A button that is a little smaller is smaller, but is still a button and the user know what to do with it. A Radio Button that reacts to a Double Click to cancel is something that the user does not expect. It will cause problem for your user, and for you because most users won't have the proper reflex.

When you have a problem as a programmer or a form designer, solve it as a programmer. Do not try to force the user to learn new ways to work with something he is used to work with.

When somebody sees a fork, he know that it is meant to be put in his mouth, not in his... double-click.
Avatar of donmghi
donmghi

ASKER

The user wants a double click.  This is how their vb6 version works.

I am looking for a coding solution.
Avatar of Nasir Razzaq
Use the code shown in the question itself

http://stackoverflow.com/questions/1064107/how-do-i-get-a-doubleclick-event-in-a-net-radio-button


Translated to VB

Public Class RadioButtonWithDoubleClick
	Inherits RadioButton
	Public Sub New()
		MyBase.New()
		Me.SetStyle(ControlStyles.StandardClick Or ControlStyles.StandardDoubleClick, True)
	End Sub

	<EditorBrowsable(EditorBrowsableState.Always), Browsable(True)> _
	Public Shadows Event MouseDoubleClick As MouseEventHandler
	Protected Overrides Sub OnMouseDoubleClick(e As MouseEventArgs)
		Dim temp As MouseEventHandler = MouseDoubleClick
		RaiseEvent temp(Me, e)
	End Sub
End Class

Open in new window

Avatar of donmghi

ASKER

That code has errors with MouseDoubleClick and temp in these lines:  

        Dim temp As MouseEventHandler = MouseDoubleClick
        RaiseEvent temp(Me, e)

I found this example that does not have errors, but does not quite work.  Getting closer.

Imports System.ComponentModel

Public Class RadioButtonWithDoubleClick

    Inherits System.Windows.Forms.RadioButton
    Public Sub New()
        'InitializeComponent()
        MyBase.New()
        Me.SetStyle(ControlStyles.StandardClick Or ControlStyles.StandardDoubleClick, True)
    End Sub

    <EditorBrowsable(EditorBrowsableState.Always), Browsable(True)> _
    Public Shadows Event MouseDoubleClick As MouseEventHandler

    Protected Overrides Sub OnMouseDoubleClick(e As MouseEventArgs)
        MyBase.OnMouseDoubleClick(e)
        ' raise the event
        RaiseEvent MouseDoubleClick(Me, e)
    End Sub
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Do you handle this event in your form?
*Lines #16 and #18 in my previous post are not necessary.
Avatar of donmghi

ASKER

Worked perfectly.  Thank you.