Avatar of Wil999
Wil999Flag for Netherlands

asked on 

VB.NET - Class and form working together


I have a class called ParameterSelection, and a form called frmParameterSelection with a bunch of controls which are needed for the user to make a selection.

What is the best way to let the class work with, and react to the control events on the form?
Should i declare an instance of the class in the form_load event? Or should i declare an instance of the form in the class? I want to have as little code as possible in the form.

When i declare an instance of the form in my class, i can only react on the form events. (not on the form controls events) I don't think it is very pretty to declare each form control (withevents) in the class and pass them in in the constructor.

Hopefully someone has a clear way to do this.

Visual Basic.NET

Avatar of undefined
Last Comment
Avatar of ste5an
Flag of Germany image

Imho neither of your approaches.

The forms controls use their events to manipulate that property.

But the concrete implementation depends on the form type and usage. Is it a modal or non-modal form? How should it be used in your application?

These requirements control whether you need to use constructor injection, where you pass a ParameterSelection instance into the dialog or you use a public property to do so.

When you can post a concise and complete example (attached as zip), then it is easy to demonstrate..
Avatar of Wil999
Flag of Netherlands image


Hi ste5an,

It is a modal form.

Maybe I should explain it as a simple example:

frmMain (this is the form where the user selects a SQL List)
frmParamererSelection (for selecting parameters, which will then be applied to the SQL List later)


Current approach:
frmMain contains an instance of frmParameterSelection
frmParameterSelection contains an instance of the ParameterSelection class.
The ParameterSelection class uses a constructor to pass in frmParameterSelection (this way I have access to the controls on the frmParameterSelection form)

All control event code stays in the frmParameterSelection form, and all other code in the ParameterSelection class.

The nice thing is, that it actually works, but I am not sure this is the best (most simple) way to do it.
Avatar of ste5an
Flag of Germany image

Blurred text
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Wil999
Flag of Netherlands image


Thanks for taking the time to answer. It was very usefull!
Visual Basic.NET
Visual Basic.NET

Visual Basic .NET (VB.NET) is an object-oriented programming language implemented on the .NET framework, but also supported on other platforms such as Mono and Silverlight. Microsoft launched VB.NET as the successor to the Visual Basic language. Though it is similar in syntax to Visual Basic pre-2002, it is not the same technology,

Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews


IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo