UserControl With a Constructor Via IDE

E

eBob.com

I've done usercontrols with constructors and usercontrols using the IDE.
But I guess I've never used the IDE to do a usercontrol with a constructor.

When I'm working in usercontrol.vb, under (Declarations) I can click on New
(although it's greyed out) and I'm taken to a New Sub but in
usercontrol.Designer.vb. I'm under the distinct impression that I really
shouldn't mess with xxxxxxxx.Designer.vb. So, question 1, how do I do a
constructor for a usercontrol using the IDE? Is it OK to modify the New sub
in usercontrol.Designer.vb? Can I add parameters? I'm pretty sure I can
get away with both but is that the right thing to do?

Also, while researching this problem I noticed that the existing New sub
does not do a MyBase.New. I thought that any class that Inherts a class
must issue MyBase.New. Question 2, how does the IDE's New Sub for a
usercontrol get away without doing that?

Can anyone help me with these questions?

Thanks, Bob
 
C

Cor Ligthert[MVP]

Hi Bob,

What do you call the "the IDE". The IDE is the complete complex of tools you
get in Visual Studio to integrate everything you do to develop in one
environment.

I assume you are not creating programs with notepad?

Cor
 
F

Family Tree Mike

eBob.com said:
I've done usercontrols with constructors and usercontrols using the IDE.
But I guess I've never used the IDE to do a usercontrol with a constructor.

When I'm working in usercontrol.vb, under (Declarations) I can click on
New (although it's greyed out) and I'm taken to a New Sub but in
usercontrol.Designer.vb. I'm under the distinct impression that I
really shouldn't mess with xxxxxxxx.Designer.vb. So, question 1, how do
I do a constructor for a usercontrol using the IDE? Is it OK to modify
the New sub in usercontrol.Designer.vb? Can I add parameters? I'm
pretty sure I can get away with both but is that the right thing to do?

Also, while researching this problem I noticed that the existing New sub
does not do a MyBase.New. I thought that any class that Inherts a
class must issue MyBase.New. Question 2, how does the IDE's New Sub for
a usercontrol get away without doing that?

Can anyone help me with these questions?

Thanks, Bob

Bob,

Normally, to do this I do these steps:

1. right click the project, and select "Add", then "User Control".
2. right click "UserControl1.vb" and select "View Code".
3. Go to "Declarations dropdown". I see four items: New and Finalize
(enabled), Dispose and InitializeComponent (disabled). Select New.

Now the empty user control code shows:
Public Class UserControl1

Public Sub New()

' This call is required by the Windows Form Designer.
InitializeComponent()

' Add any initialization after the InitializeComponent() call.

End Sub
End Class

New does appear disabled if I go to the InitializeComponent call, which
is in the designer.vb. If I select "New" in the designer.vb _without_
any constructor created, it does add it to the designer.vb file.

As you say, it is probably best not to add the constructor to the
designer file, but it is not prohibited.

I hope this helps.
 
E

eBob.com

Hi Cor,

By "IDE" I meant that part of Visual Studio which allows me to graphically
design a form/control via drag/drop and code. I never use Notepad to create
a program, but sometimes if I have a really simple usercontrol I just add a
class to the end of Form1.vb.

Bob
 
E

eBob.com

Thank you Mike. I just did a little test. If I create a constructor when I
create the usercontrol then my experience is as you describe. In my case I
had already created and used the usercontrol without a constructor - i.e.
without a constructor which I had touched. And I think that accounts for
why it ended up in the xxxxxx.Designer.vb file.

This morning I found the courage to modify the constructor in the
xxxxxx.Designer.vb file and that seems to work just fine. I think in the
future I will try to remember to touch the constructor in the initial design
work.

Thanks, Bob
 
S

Sergey Poberezovskiy

Bob,

I would suggest manually cut the IDE created constructor from the .Designer
file and paste it into the code behind - this way you will have the
custom/IDE code separation.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top