OneSource
OneSource

Reputation: 529

VB.Net Winforms User Control Variable

I'm using VS 2005 in a VB.Net WinForms application. I have a custom User Control that requires a variable to render its data correctly. My question is, what's the best way to require the calling sub to populate the variable? I've thought of a couple of options:

What are your suggestions for populating the required variable that the User Control needs at design time?

Thanks in advance.

Upvotes: 0

Views: 2344

Answers (3)

Anonymouse
Anonymouse

Reputation: 1

I would overload the constructor, the default constructor would set the variable to a predetermined default value.

Upvotes: 0

JaredPar
JaredPar

Reputation: 755477

I've been down this road a few times with custom controls and I've come to one truth

Custom Controls must render without crashing if necessary properties are not set.

There are just far too many cases where you will get into this situation. The primary example is the WinForm designer. As soon as you drag your control onto a host, WinForms will create an instance of it and render it in the VS process. By default it will pass no variables to the constructor and hence the initial render will happen without any of your properties set.

You'll save a lot of time if you have your control render in some lesser state when the property is not set. This is how many of the standard controls work. Usually I have the control render a message to the effect of "Lacking Property X" or simply display nothing.

Upvotes: 3

Joel Coehoorn
Joel Coehoorn

Reputation: 416141

Add the variable as a parameter to all public constructors for the control?

Upvotes: 0

Related Questions