Creating Custom Controls in VBNet (not User Control, AFAIK) 
Author Message
 Creating Custom Controls in VBNet (not User Control, AFAIK)

Please excuse if this proves to be a duplicate post.
I am having fits trying to get my posts to make it
into the DotNet newsgroups.  However, I am not having
any problems with any other newsgroups; only the
DotNet's.

I am wanting to inherit from the standard Button and
override the background color property; assigning the
new Button the name MyButton, as shown below.  This
should be simple!

--------------------------------------------------
Imports System.WinForms

Public Class MyButton : System.WinForms.Button  
   Me.BackColor = Color.LightGray
End Class
--------------------------------------------------

And, I get the error:

   Expected variable, constant, Enum, Type, or procedural
   declaration.

under 'system'.

Can anyone provide a 'tested' and 'proven' means, in VB,
to simply inherit from a standard control and override a
default property such as background color.  This should
be very simple, and common thing to do, but I have been
unable to find a sample in the SDK's.

MyButton, of course, should appear, and be persistent,
on the ToolBox.

Thanks,  

Jim Holloman



Mon, 16 Jun 2003 09:00:27 GMT  
 Creating Custom Controls in VBNet (not User Control, AFAIK)

Quote:
>I am having fits trying to get my posts to make it
>into the DotNet newsgroups.  

Okay, then risking being redundant:

Quote:
>Imports System.WinForms

>Public Class MyButton : System.WinForms.Button  

This is kinda-sorta-C# syntax. VB.NET is

Public Class MyButton
  Inherits System.WinForms.Button

Quote:
>   Me.BackColor = Color.LightGray
>End Class



Mon, 16 Jun 2003 06:07:14 GMT  
 Creating Custom Controls in VBNet (not User Control, AFAIK)

Quote:
> Please excuse if this proves to be a duplicate post.
> I am having fits trying to get my posts to make it
> into the DotNet newsgroups.

Try using this server instead of msnews...
    207.46.180.23

--
Jonathan Allen



Mon, 16 Jun 2003 06:09:06 GMT  
 Creating Custom Controls in VBNet (not User Control, AFAIK)
Jonathan,

Quote:
> > Please excuse if this proves to be a duplicate post.
> > I am having fits trying to get my posts to make it
> > into the DotNet newsgroups.

> Try using this server instead of msnews...
>     207.46.180.23

This worked great for me...

(made the change ~2 weeks ago)



Mon, 16 Jun 2003 06:24:44 GMT  
 Creating Custom Controls in VBNet (not User Control, AFAIK)

Quote:
> Please excuse if this proves to be a duplicate post.
> I am having fits trying to get my posts to make it
> into the DotNet newsgroups.  However, I am not having
> any problems with any other newsgroups; only the
> DotNet's.

Not only have all three of your posts made it in to this group, but there
have actually been two replies to the first one...

hth hand
--
Adam D. Barratt



Mon, 16 Jun 2003 06:17:42 GMT  
 Creating Custom Controls in VBNet (not User Control, AFAIK)
Jim,

Quote:
> Imports System.WinForms

> Public Class MyButton : System.WinForms.Button
>    Me.BackColor = Color.LightGray
> End Class

There's so much wrong with the code, I don't know where to start with the
error messages.  Try...

Public Class MyButton
    Inherits System.WinForms.Button

    Sub New()
        MyBase.New()
        Me.BackColor = System.Drawing.Color.LightGray
    End Sub

End Class

... and then read the manual ;-)

--
David.



Mon, 16 Jun 2003 07:24:32 GMT  
 Creating Custom Controls in VBNet (not User Control, AFAIK)
Thanks, Jonathan.

I have tried both msnews.microsoft.com and
207.46.180.23.  At times, like yesterday, neither
of them would allow me to post.  Then, today, both
of them are allowing me to post -- althought it
sometimes takes several hours for my posts to be
reflected in the newsgroups.

The week just prior to Xmax day, the 207.46.180.23
was working fine.  I am a loss to understand the
on again/off again behavior.

Jim H.

Quote:

> Try using this server instead of msnews...
>     207.46.180.23

> Jonathan Allen



Mon, 16 Jun 2003 11:41:17 GMT  
 Creating Custom Controls in VBNet (not User Control, AFAIK)


Quote:

> Try using this server instead of msnews...
>     207.46.180.23

You are best to use msnews.microsoft.com rather than the specific IP as the
IP address may be changing soon, so be warned. The problems people saw a few
weeks ago was due to a change in the servers, now running on win2K.
consequently the stats got changed and OE on people's machines would get
confused and needed to be reset. This was probably made worse due to
mirroring in some circumstances.


Mon, 16 Jun 2003 13:27:46 GMT  
 Creating Custom Controls in VBNet (not User Control, AFAIK)
Thanks, fellows, for the help.  I managed to create a Custom
Control with the code below.  However, I can not get it to appear
on the Toolbox.  

The steps are listed after the code.   Have I found a bug
or am I doing something wrong?

'-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Imports System.ComponentModel
Imports System.WinForms.Button

Public Class Btn2

    Inherits System.WinForms.Button

    Public Sub New()
        MyBase.New
        InitializeComponent
        Me.BackColor = System.Drawing.Color.LightGray
    End Sub

#Region " Component Designer generated code "

    'Required by the Component Designer
    Private components As Container
    Private WithEvents Button1 As System.WinForms.Button

    'NOTE: The following procedure is required by the Component
           Designer
    'It can be modified using the Component Designer.  
    'Do not modify it using the code editor.
    Private Sub InitializeComponent()
       Me.components = New System.ComponentModel.Container
       Me.Button1 = New System.WinForms.Button


       Button1.Location = New System.Drawing.Point(419, 124)
       Button1.Size = New System.Drawing.Size(75, 23)
       Button1.TabIndex = 0
       Button1.Text = "Button1"

    End Sub

#End Region

End Class

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Steps:

1)   Select File | New | Project | Select Class Library | Set Name
     to Btn1 | Press Enter

2)   The Class Designer (CD) opens with the default name of
     Class1.vb and the message:

     "Drag components from the Server Explorer or Toolbox to the
     designer surface".

3)   Open Toolbox and drag a "Button" from the Win Forms Group to
     the CD surface.

4)   Double-click the CD surface to open the code window.

5)   Added "Imports System.WinForm.Button" to the top.
     Changed "Public Class 1" to "Public Class Btn1"

6)   Changed "Inherits System.ComponentModel", right under the
     Class Statement to "Inherits System.WinForms.Button".

7)   Add under "InitializeComponent", "Me.BackColor =
     System.Drawing.Color.LightGray"

8)   Did a Build with no errors.

9)   Checked Toolbox.  Btn1 does not appear.

10)  Went to Solutions Explorer (SE) and right-clicked References
     | Add Reference | Browse and then selected the Btn1.dll and it
     appeared under the References Heading.

11)  Checked Toolbox again -- still no Btn1.

12)  Set focus to the CD surface and pressed F7 to open the code
     window.

13)  Added "Imports Btn1" at the top of the code.

14)  Did another Build -- with no errors.

15)  Shift-F7 to return to the CD surfrace from the code window.

16)  Checked ToolBox -- still no Btn1.

17)  From top menu, select Project | Add Windows Form and then
     Open.  Form1 is added to the project.

18)  Press F7 while focus is on the Forms Designer (FD) to open the
     code window.

19)  Added "Imports Btn1" to the top of the code.

20)  Shift-F7 to return to the FD surface.

21)  Checked Toolbox again.  No Btn1.

22)  WHAT DOES IT TAKE TO GET Btn1 ON THE TOOLBOX???

Posted: 11-28-2K 5:45a



Mon, 16 Jun 2003 21:42:18 GMT  
 Creating Custom Controls in VBNet (not User Control, AFAIK)
Well, I just managed to get a custom contol to
appear on the Toolbox during the development
of the control.  I took the following steps.

Instead of modifying the Class1.vb module that
is created by default, I deleted it and then from
the Main Menu, selected Project | Add User Control
and then modified that code.  I realized a few
mintues ago that I wasn't following the suggestion
made by J. W. -- I just thought that I was.

However, the control still does not appear on the
Toolbox for other projects; so it is still of no
value.  I start a new Windows Application and add
a reference to the Custom Control's DLL.  I also
added an Imports statement to the Win Form.  But,
the custom control still does not appear.  I am
apparently still missing something.

Jim Holloman



Mon, 16 Jun 2003 22:15:47 GMT  
 Creating Custom Controls in VBNet (not User Control, AFAIK)
Courtesy of Mark Boulter (MSFT):

You need to add the ShowInToolboxAttribute to the class declaration:

    public class <ShowInToolbox(true)> MyControl
    ....
    End Class

Once you've done this you should be able to add the control to the
toolbox
by right-clicking on the toolbox and selecting "Customize Toolbox...".

Go the the ".Net Frameworks Components" tab, select Browse and
select your control assembly (DLL file).

The control will be added to the list of components. Select the
CheckBox
next to the control and it should appear of the toolbox.

I've just verified that this works with the following control:

Imports System.ComponentModel
Imports System.Drawing
Imports System.WinForms

Public Class <ShowInToolBox(True)> Control1

    Inherits System.WinForms.RichControl

    Private Sub InitializeComponent()

    End Sub

    Public Sub New()
        MyBase.New()

    End Sub

    'UserControl1 overrides dispose to clean up the component list.
    Public Overrides Sub Dispose()
        MyBase.Dispose()
    End Sub

    Protected Overrides Sub OnPaint(ByVal e As
System.WinForms.PaintEventArgs)
        e.Graphics.FillRectangle(New SolidBrush(Color.Red),
ClientRectangle)
    End Sub

End Class



Tue, 17 Jun 2003 03:44:40 GMT  
 Creating Custom Controls in VBNet (not User Control, AFAIK)
That worked!

Many thanks, Sjoerd, especially for taking the time to test
the code.

Don't know where you found such a gem (do you have a private
channel to Mark?).  I searched the VBNet newsgroup and found
no match on "ShowInToolbox".  I searched the OLH and found one
match dealing with C# and Web Forms.  The documentation at
www.gotdotnet.com on Creating Controls makes no mention of
"ShowInToolbox".

I did find that once you remove a Custom Control from the
Toolbox, you still can not go back and update the control.
Apparently the control remains attached to a process and the
Build will not overwrite the DLL file.  You first have to
close the IDE and then reload.  I will make a report to Msft.

For the benefit of others, here is my code that places two
custom controls, a Button and a Textbox, in one project and
one DLL.

'-----------------------------------------------------------
Imports System.ComponentModel
Imports System.Drawing
Imports System.WinForms

Public Class <ShowInToolBox(True)> UC104Btn

    'Inherits System.WinForms.UserControl  ' This will place a "skeleton"  icon (with no image) on the toolbox
    Inherits System.WinForms.Button        ' This will place a "grid like" icon (with image)    on the toolbox

    Public Sub New()
        MyBase.New
        InitializeComponent
        Me.BackColor = Color.LightGray
    End Sub

    'UserControl1 overrides dispose to clean up the component list.
    Overrides Public Sub Dispose()
        MyBase.Dispose
        components.Dispose
    End Sub

#Region " Windows Form Designer generated code "   ' Probably should say, "Class Designer generated code"
   ' -- Generated Code Here --
#End Region

End Class

Public Class <ShowInToolBox(True)> UC104Text    

    'Inherits System.WinForms.UserControl
    Inherits System.WinForms.TextBox

    Public Sub New()
        MyBase.New
        InitializeComponent
        Me.BackColor = Color.Red
    End Sub

    'UserControl1 overrides dispose to clean up the component list.
    Overrides Public Sub Dispose()
        MyBase.Dispose
        components.Dispose
    End Sub

#Region " Windows Form Designer generated code "
   ' -- Generated Code Here --
#End Region

End Class
'-----------------------------------------------------------



Tue, 17 Jun 2003 09:29:24 GMT  
 Creating Custom Controls in VBNet (not User Control, AFAIK)

Quote:
>Many thanks, Sjoerd, especially for taking the time to test
>the code.

I'd love to take credit for that, but Mark did that too.

Quote:
>Don't know where you found such a gem (do you have a private
>channel to Mark?).

I *wish*. I'm not even a full-fledged beta tester. It came up ages ago
in the group and I save a lot of messages :-)

Glad to be of service
Sjoerd



Tue, 17 Jun 2003 06:47:42 GMT  
 
 [ 15 post ] 

 Relevant Pages 

1. Create a Custom Control (not a User Control, AFAIK)

2. Custom user controls Not being added to ToolBox

3. Custom Control window could not be created in EXE

4. user controls and custom controls

5. Creating custom methods for a custom control

6. Create User Control as Data Source Using ADO Data Control

7. User Control - User-defined type not defined

8. Creating App for user to create Forms and Controls with VB

9. Enum problem in Custom User Control

10. User control or inherit to make custom button?

11. Trapping the right click event in a custom user control

12. Custom Inherited User Control Doesn't Show Up in Toolbox

 

 
Powered by phpBB® Forum Software