Home All Groups Group Topic Archive Search About

User Control constructors taking arguments?

Author
5 Sep 2006 12:33 PM
Kristian Frost
Hi, me again,
I'm trying to create a User Control in VS2003 that takes an object as an 
argument from its parent form and designs itself accordingly.
At the moment, I'm having to create a default, empty constructor method 
and then adjust the size later by sending in the argument in a later 
method and then resizing everything, instead of simply creating things 
according to these arguments in the first place.

Obviously, seeing as the whole design of the User Control is based on the 
object I'm feeding it, I'd prefer to feed the object directly into the 
constructor, but doing that involves altering that part of the code that 
Visual Studio tells you not to (the InitializeComponent() method) and 
which throws an absolute, utter fit whenever I have done.

These work fine as workarounds go, but I really would prefer to simply 
have a constructor for the User Control that takes an argument.

Perhaps this would be better put into the microsoft.public.vstudio.general 
newsgroup, seeing as this seems to be all VS's fault for rewriting my code 
and throwing fits and stuff, but as that looks utterly dead, I thought I'd 
try here first.
Any advice, or I do stick to my workarounds?

KF

--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/

Author
5 Sep 2006 2:33 PM
Robinson
> These work fine as workarounds go, but I really would prefer to simply
> have a constructor for the User Control that takes an argument.
>
> Perhaps this would be better put into the microsoft.public.vstudio.general
> newsgroup, seeing as this seems to be all VS's fault for rewriting my code
> and throwing fits and stuff, but as that looks utterly dead, I thought I'd
> try here first.
> Any advice, or I do stick to my workarounds?

You can create a constructor that takes an argument of course.   Just make
sure the first thing you do in the constructor is call
"InitializeComponent".  Of course you can't make use of this in Design-Time
only if you dynamically create the control at run-time, which I assume you
are wanting to do.  Otherwise, you could give the control a property and set
that property at Design-Time, but this might require some messing about ;).
Author
5 Sep 2006 3:46 PM
Kristian Frost
Ah well, I guess I'll just have to code everything in manually, then.
It's pretty annoying, though. The main reason I'm using VB in the first 
place is because of the ease-of-use of the Form Designer. Not being able 
to use that now for a lot of my custom components... kind of makes using 
VB a bit pointless, really.

KF

On Tue, 05 Sep 2006 15:33:36 +0100, Robinson 
<itoldyounottospamme@nowmyinboxisfull.com> wrote:

Show quoteHide quote
>
>> These work fine as workarounds go, but I really would prefer to simply
>> have a constructor for the User Control that takes an argument.
>>
>> Perhaps this would be better put into the 
>> microsoft.public.vstudio.general
>> newsgroup, seeing as this seems to be all VS's fault for rewriting my 
>> code
>> and throwing fits and stuff, but as that looks utterly dead, I thought 
>> I'd
>> try here first.
>> Any advice, or I do stick to my workarounds?
>
> You can create a constructor that takes an argument of course.   Just 
> make sure the first thing you do in the constructor is call 
> "InitializeComponent".  Of course you can't make use of this in 
> Design-Time only if you dynamically create the control at run-time, 
> which I assume you are wanting to do.  Otherwise, you could give the 
> control a property and set that property at Design-Time, but this might 
> require some messing about ;).



--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Author
5 Sep 2006 4:05 PM
Robinson
> Ah well, I guess I'll just have to code everything in manually, then.
> It's pretty annoying, though. The main reason I'm using VB in the first
> place is because of the ease-of-use of the Form Designer. Not being able
> to use that now for a lot of my custom components... kind of makes using
> VB a bit pointless, really.


You can determine how the visual designer interacts with your control and
make your control like all the other Windows Forms controls too.  There is a
whole section on generating your own designer methods.  Look up
ParentControlDesigner for example.  It's entirely possible to do, but yes
you do have to write more code to get it to work properly (it can also be a
bit of a pain to debug).