• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 366
  • Last Modified:

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

0
GIANTOCR
Asked:
GIANTOCR
  • 2
  • 2
1 Solution
 
bmatumburaCommented:

Public Class DigitalIn
    Inherits BaseInputDate
 
    Private Sub YesBox_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles YesBox.MouseDown
        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) Handles NoBox.MouseDown
        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.
 
    End Sub
End Class

Open in new window

0
 
bmatumburaCommented:
What do you want to do with the parameter locFS?
0
 
GIANTOCRAuthor Commented:
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.
0
 
GIANTOCRAuthor Commented:
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.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now