Question Creating a custom button and adding it to the toolbox...

ChristinaSeay

Member
Joined
Oct 22, 2008
Messages
14
Programming Experience
Beginner
Hey guys,

I've been at this one for days and I just can't seem to get it right.

Basically, I designed the button control the way I want it to be... background image, font style, size, etc... and I want to add that button to my toolbox so I can reuse it.

I'm not changing anything about how it works, just setting properties.

How do I need to go about doing this?
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,157
Location
Sydney, Australia
Programming Experience
10+
Any components you create in the current solution will be added to the Toolbox automatically for the current solution. If you want to use a component outside the solution it's declared in then the process is exactly the same as it is for any other component: right-click the Toolbox and select Choose Items, navigate to the DLL containing the desired component(s) if it isn't already listed, then select the desired component(s).
 

roccoman

Active member
Joined
Aug 24, 2010
Messages
36
Location
Indiana
Programming Experience
5-10
you can create a custom class and make it inherit windows.forms.button (not on dev machine so that wording may not be %100 correct)

then you can set its properties in the NEW method

example:

Code:
public class MyButton
''  again check to make sure that is correct
inherits windows.forms.button


public Sub New()

''  the image can be loaded from anywhere, i like to store that kinda thing in Resources
me.image = my.resources.IMG_MY_BUTTON
me.font = new Font() ''  you have to set this info in the Font New constructor based on what you want

''  more custom settings can be applied here as needed
end Sub

end class

then BUILD your application and you will see that component in the toolbox in a tab called "<project name> Components"

drop it into a form and you're good to go
 

ChristinaSeay

Member
Joined
Oct 22, 2008
Messages
14
Programming Experience
Beginner
Perfect!!

Perfect!! That did it!!

Well... almost... my new button is my toolbox, but it has a gear icon next to it. How can I change that?
 

roccoman

Active member
Joined
Aug 24, 2010
Messages
36
Location
Indiana
Programming Experience
5-10
glad to help

not sure about the icon in the toolbox - do you plan to give this custom button to other developers?
 

ChristinaSeay

Member
Joined
Oct 22, 2008
Messages
14
Programming Experience
Beginner
Yeah... I have 3 developers in my group and we're trying to standardize our forms so they have consistent layouts, etc.
 

roccoman

Active member
Joined
Aug 24, 2010
Messages
36
Location
Indiana
Programming Experience
5-10
then what you may want to do is create a DLL containing any custom controls you guys will need.

create a new project but instead of a windows app create a "Windows Control Library"

add your "MyButton" class to it and remove the default UserControl file from it (since you wont be using it)

this way if you want a "standardized" label, textbox, or any other object you can create it in this project, recompile the DLL, and then add that component in another project - kinda like you would an OCX or any other 3rd party component



as for the icon - try reading this How to: Provide a Toolbox Bitmap for a Control
 

ChristinaSeay

Member
Joined
Oct 22, 2008
Messages
14
Programming Experience
Beginner
Getting Closer...

Ok... I'm getting a little bit closer I think...

This code works just fine when it's a custom control, but not when it's a Class...
Code:
<System.ComponentModel.Description("Custom Button"), ToolboxBitmap(GetType(Button_Custom), "Button_Custom.bmp")> _
Public Class Button_Custom
    Inherits System.Windows.Forms.Button

    Public Sub New()

        InitializeComponent()

        Me.BackColor = System.Drawing.Color.Transparent
        Me.BackgroundImage = System.Drawing.Image.FromFile("C:\ButtonBackground.jpg")
        Me.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center
        Me.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.Font = New System.Drawing.Font("Tahoma", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.MaximumSize = New System.Drawing.Size(104, 30)
        Me.MinimumSize = New System.Drawing.Size(104, 30)
        Me.Name = "ButtonAstec"
        Me.Size = New System.Drawing.Size(104, 30)
        Me.TabIndex = 0
        Me.Text = "Button_Custom"
        Me.UseVisualStyleBackColor = False
    End Sub

End Class
I like the idea of trying to get all of our "standardized" components in one .dll file like that... it would make it much nicer. I'm thinking I'm just missing some small step here...
 

roccoman

Active member
Joined
Aug 24, 2010
Messages
36
Location
Indiana
Programming Experience
5-10
i dont know if it will work for a class - from what little i have seen on the subject its always shown as a compiled DLL
 

ChristinaSeay

Member
Joined
Oct 22, 2008
Messages
14
Programming Experience
Beginner
Hmmm...

I tried doing it where I have one Project and it has two custom controls in it, and it didn't work as a compiled .dll

Well... to clarify, I have this:

Solution
--> Project
-----> Custom_Button.vb <- User Control
-----------> Custom_Button.designer.vb
-----> Custom_Label.vb <- User Control
-----------> Custom_Label.designer.vb

When I build it out into a .dll file, and then put that in the toolbox, only the button shows up in the toolbox, not the Label.

And the icon image in the toolbox for the button didn't work like this.

But when I have it just:
I tried doing it where I have one Project and it has two custom controls in it, and it didn't work as a compiled .dll

Well... to clarify, I have this:

Solution
--> Project
-----> Custom_Button.vb <- User Control
-----------> Custom_Button.designer.vb

Then the button and icon for it work just fine.
 

roccoman

Active member
Joined
Aug 24, 2010
Messages
36
Location
Indiana
Programming Experience
5-10
one thing i did read while researching this is you would have to remove the reference from a project and then re-add it in again after you rebuild the DLL - or crete a new project & add the reference to the newly compiled DLL.

it could be as simple as that.

<<15 minutes later>>

i just created a WindowsControlLibrary with a ClassTextbox and ClassLabel, built the DLL, and created a new project to test it. when i added the reference to my DLL i saw both ClassTextbox and ClassLabel items in my toolbox.
 

ChristinaSeay

Member
Joined
Oct 22, 2008
Messages
14
Programming Experience
Beginner
...

I know I'm missing something sooooo simple at this point... I tried what you suggested and I started a whole new Windows Form Project to test the .dll in and I'm still getting the same results (Only the button showing in the toolbox and it not having the correct icon file.)

Here's what I have right now:

Solution
---> Project
------> Button_Custom.bmp
------> Button_Custom.vb <-Class, not custom control
------>Label_Custom.bmp
------> Label_Custom.vb <- Class, not custom control

The code for Button_Custom.vb is:
Code:
<System.ComponentModel.Description("Custom Button"), ToolboxBitmap(GetType(Button_Custom), "Button_Custom.bmp")> _
Public Class Button_Custom
    Inherits System.Windows.Forms.Button

    Public Sub New()

        InitializeComponent()

        Me.BackColor = System.Drawing.Color.Transparent
        Me.BackgroundImage = System.Drawing.Image.FromFile("\\Ast-fs01\homedirs\EngineeringPrograms\StandardFormDesign\FormHeaderBackground.jpg")
        Me.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center
        Me.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.Font = New System.Drawing.Font("Tahoma", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.MaximumSize = New System.Drawing.Size(104, 30)
        Me.MinimumSize = New System.Drawing.Size(104, 30)
        Me.Name = "Button_Custom"
        Me.Size = New System.Drawing.Size(104, 30)
        Me.TabIndex = 0
        Me.Text = "Button_Custom"
        Me.UseVisualStyleBackColor = False
    End Sub

End Class

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Button_Custom
    Inherits System.Windows.Forms.Button

    'Control overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Control Designer
    Private components As System.ComponentModel.IContainer

    ' 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.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        components = New System.ComponentModel.Container()
    End Sub

End Class
The code for Label_Custom.vb is:
Code:
<System.ComponentModel.Description("Custom Label"), ToolboxBitmap(GetType(Label_Custom), "Label_Custom.bmp")> _
Public Class Label_Custom
    Inherits System.Windows.Forms.Label
    Private Sub New()
        InitializeComponent()

        Me.BackColor = Color.Aqua

    End Sub

End Class

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Label_Custom
    Inherits System.Windows.Forms.Label

    'Control overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Control Designer
    Private components As System.ComponentModel.IContainer

    ' 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.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        components = New System.ComponentModel.Container()
    End Sub

End Class
Thank-you again for your help with this. I'm still "Googling" it too... but not having much luck on this so far...
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,157
Location
Sydney, Australia
Programming Experience
10+
This may or may not be useful, but there are two ways to add a component to the Toolbox. When you declare a component, it will be added automatically to the Toolbox for that solution. In that case, a generic icon will be used, even if you applied the ToolboxBitmap attribute. The other way is to add it to the Toolbox for all projects by right-clicking the Toolbox. In that case, whatever icon you specify using the TollboxBitmap attribute should be used.
 
Top Bottom