Link to home
Start Free TrialLog in
Avatar of GIANTOCR
GIANTOCR

asked on

Overloaded constructor User Control

I created a user control with two checkboxes on it. I added mouse down event handlers to prevent both checkboxes from being checked at the same time.

The constructor for the user control takes an argument that is used to set the fontsize of the control by passing it back to the base class.

I created an overloaded contructor that allows the control to be instantiated with or without passing the fontsize argument. If the control is instantiated without an argument, the constructor calls the other constructor with a default value for the argument.

The checkbox event handlers are added in the constructor that accepts an argument. If I instantiate the control by passing an argument (ie going directly through the constructor that adds the event handlers), then the control works as planned, and the checkbox mousedown event handlers work. If I instantiate the control without passing an argument and go through the constructor that calls the constructor that adds the handlers, then the event handlers do not work.

I am not sure why the event handlers are not being added when I go through the argument-less constructor. I would appreciate any help figuring this out. Thanks.
Public Class DigitalIn
    Inherits BaseInputDate
 
    Private Sub YesBox_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        If NoBox.Checked = True Then
            NoBox.Checked = False
        End If
    End Sub
    Private Sub NoBox_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        If YesBox.Checked = True Then
            YesBox.Checked = False
        End If
    End Sub
 
    Public Sub New()
        Me.New(8.5)
    End Sub
 
    Public Sub New(ByVal locFS As Double)
        MyBase.New(locFS)
        ' This call is required by the Windows Form Designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.
 
        AddHandler YesBox.MouseDown, AddressOf YesBox_MouseDown
        AddHandler NoBox.MouseDown, AddressOf NoBox_MouseDown
 
    End Sub
End Class

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of bmatumbura
bmatumbura

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
Avatar of bmatumbura
bmatumbura

What do you want to do with the parameter locFS?
Avatar of GIANTOCR

ASKER

Sorry for the delay, I got called away this morning and am just now able to get back to this.

Regarding your first comment, moving the button down event handlers out of the sub new(). These user controls will always be added at runtime. It is my understanding that when controls are added at runtime, event handlers need to be added with an Add Handler command.

<What do you want to do with the parameter locFS?>

The parameter locFS is used to set the font size of the control. This is set in the base control, from which the user control DigitalIn is derived from.

Still not sure why adding the event handlers work with one of the constructors but not the other. Thanks.
It works now. What was wrong was that originally I had an InitializeComponent() statement in both versions of the constructor. I deleted it from the code for the parameterless constructor before I posted the questions but had forgotton to rebuild the controls so it was not working. Once I rebuilt the controls the handlers worked. Thanks for trying to help.