Results 1 to 14 of 14

Thread: Creating a custom button and adding it to the toolbox...

  1. #1
    ChristinaSeay is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Oct 2008
    Posts
    14
    Reputation
    58

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

    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?

  2. #2
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    11,330
    Reputation
    1543
    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).

  3. #3
    roccoman's Avatar
    roccoman is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 2.0
    Join Date
    Aug 2010
    Location
    Indiana
    Posts
    36
    Reputation
    36
    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

  4. #4
    ChristinaSeay is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Oct 2008
    Posts
    14
    Reputation
    58

    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?

  5. #5
    roccoman's Avatar
    roccoman is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 2.0
    Join Date
    Aug 2010
    Location
    Indiana
    Posts
    36
    Reputation
    36
    glad to help

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

  6. #6
    ChristinaSeay is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Oct 2008
    Posts
    14
    Reputation
    58
    Yeah... I have 3 developers in my group and we're trying to standardize our forms so they have consistent layouts, etc.

  7. #7
    roccoman's Avatar
    roccoman is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 2.0
    Join Date
    Aug 2010
    Location
    Indiana
    Posts
    36
    Reputation
    36
    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

  8. #8
    ChristinaSeay is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Oct 2008
    Posts
    14
    Reputation
    58

    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...

  9. #9
    roccoman's Avatar
    roccoman is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 2.0
    Join Date
    Aug 2010
    Location
    Indiana
    Posts
    36
    Reputation
    36
    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

  10. #10
    ChristinaSeay is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Oct 2008
    Posts
    14
    Reputation
    58

    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.

  11. #11
    roccoman's Avatar
    roccoman is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 2.0
    Join Date
    Aug 2010
    Location
    Indiana
    Posts
    36
    Reputation
    36
    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.

  12. #12
    ChristinaSeay is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Oct 2008
    Posts
    14
    Reputation
    58

    ...

    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...

  13. #13
    StoneCodeMonkey is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2009
    Posts
    56
    Reputation
    53
    If you are still looking for the Toolbox icon, try ToolboxBitmap Attribute

  14. #14
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    11,330
    Reputation
    1543
    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.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking