Question Subclassed controls WithEvents in the forms designer

JimA

New member
Joined
Feb 14, 2012
Messages
4
Location
New Mexico, USA
Programming Experience
10+
I created a simple class to add a little functionality to a button control:

Public Class pwButton
Inherits System.Windows.Forms.Button

Public Sub New()
MyBase.New()
ForeColor = Color.Indigo
BackColor = Color.LightGray
Height = 10
End Sub

End Class

After building, the control can be added to the controls box and placed on a form. However, this line of code...

Private Sub btn_Click(sender As System.Object, e As System.EventArgs) Handles PwButton1.Click

results in the error...

Handles clause requires a WithEvents variable defined in the containing type or one of its base types.

After much searching and reading, I discovered that the declaration line in the forms designer failed to add a Withevents keyword. Changing this line...

Friend PwButton1 As Controls.Controls.pwButton

to...

Friend WithEvents PwButton1 As Controls.Controls.pwButton

solved the issue. That's good, and I learned a lot in tracking down what's going on, but I'd prefer that the WithEvents keyword was added when placing the control on the form as it does with the regular .net controls. Not a big deal, but I'm curious about why it's missing.

Suggestions on what I might have missed?

Thanks,

Jim
 
Well, I am unique! :) Since it's no longer a show stopper, I'll continue with my project and hope I find the answer soon. One of the modules in this project (a conversion from VB6 to .net) has over 300 data inputs. I'd like to solve the issue before then, so if anyone has any ideas....

Thank you for replying.

Jim
 
If a component/control class has any public events the designer will always add the variable as WithEvents.
You say "the control can be added to the controls box", if the class is in current solution/project it should be added automatically.
This part looks suspicious: "Controls.Controls.pwButton", namespace is always added by designer, but here you have another level which means either a nested namespace (not likely) or nested class or module. I think you have nested this class in another class or module, and that is the cause of problems. You should not nest this type, use namespace if you need added type arrangement.
 
Thank you, John. "controls box" should read ToolBox. ie, the control is compiled and can be placed on the form. The controls.controls.pwButton looked funny to me, too, but being a .net newbie, I don't quite understand how namespaces work. I'll go read some more and see if that's the source of the problem. If it is, I'll let you guys know. If not, I'll let you know that, too.

Thanks for the steerage.

Jim
 
Back
Top