SilverShotBee SilverShotBee - 1 year ago 71 Question

VB.NET No Constructor Found For Custom Control

I made a user control that has been working fine until I made a change - I changed it from this:

Public Sub New()
End Sub

to this:

Public Sub New(Optional ViewMode As Boolean = False, Optional sMaterial As String = "", Optional sCost As String = "", Optional sQuantity As String = "", Optional bOnOrder As String = "", Optional bDelivered As String = "")
currMaterial = sMaterial
currCost = sCost
currQuantity = sQuantity
currOnOrder = bOnOrder
currDelivered = bDelivered
currViewmode = ViewMode
End Sub

I need to be able to write to the control and store values, but now that I've added this I'm getting the "No Constructor Found" error. What am I doing wrong?

Answer Source

In order to create an instance of any class, you need to invoke a constructor. When you add an instance of a control to a form in the designer, there's no way to pass arguments to a constructor so a parameterless constructor must be called. You have no parameterless constructor in your control any more so you cannot add an instance in the designer, only in code. If you want to be able to add an instance in the designer then reinstate the parameterless constructor and then set those values in the Properties window.

You can keep both constructors if you want to be able to create an instance in code too. In your case, your new constructor has default values for all the parameters anyway so simply set those fields/properties to those default values in the parameterless constructor.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download