Creating a custom control in VB.net?

B

BlarneyFOR

Hello,

I have been trying to use a custom class in my project. When I compile
and run the class, it works very well on the Pocket PC. When I go to
the designer though, it throws many many errors. If I switched between
the designer and the Code View, most of the time it deletes my code. I
realize it has something to do with changing the code that is
automatically generated by Visual Studio. So what I was wondering, is
how I would get around this problem?

I thought I might try and create a custom tool from the control and
insert that but I'm running out of ideas to do it in VB.net. I have
read the article over at Intelliprog for C#
(http://www.intelliprog.com/articles/index.html) and it looks like you
would work great for what I am trying to do, but I do not see a way to
do it in VB.

What is the best way to do this?
Many thanks!
 
B

BlarneyFOR

Ok, this will sound sarcastic, but it's not. Is it best that I simply
implement my code and let the errors fall where they may?

In other words, if I am using a custom picturebox control (Like
Sergey's Transparent Picturebox class) and I reference "transparent
pictureboxes" in the "#Region " Windows Form Designer generated code "
it will error if I switch to [Design] mode. And if I switch back to
View code, it invariably deletes the code.
I hope I am explaining this right.
 
S

Sergey Bogdanov

You can't directly modify "Windows Form Designer generated code" block.
If you want to have your control available from designer you should
write design-time version of this control. As Chris correctly said it is
not possible from VB at this moment.

As an alternative approach you may create new C# project with custom
controls and add its reference to your VB project in your solution.
When you have written design support for controls in C# project you
could add it to your toolbar and then use them in the VB.NET project
(again, see: http://www.intelliprog.com/articles/index.html).

Best regards,
Sergey Bogdanov
http://www.sergeybogdanov.com
 
P

Peter Foot [MVP]

If you use custom controls, with no design assemblies, then you are best
writing the code to instantiate them outside of the InitializeComponent
method, create the control, set the bounds and other properties then call
this.Controls.Add(mycontrol) to add the control to the form.
Put all the code to setup the control in your forms constructor after the
call the InitializeComponent. If for example you design a for with a
PictureBox and decide to switch to using a transparent picturebox, copy the
code from the InitializeComponent method which refers to your picture box
and paste it into your forms constructor, change the name, and make sure
your form has private member of the type of your custom control (won't
compile without it). Then delete the original control from the designer. You
will not see your control in design mode but you can set it's properties
anywhere in your code as you could with the standard control and handle any
of it's methods (make sure you declare it WithEvents if you are using
VB.NET)

Peter
 

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