locked
How do I create a custom control using a picturebox control? RRS feed

  • Question

  • Hi all :) I'm using vb10.

    I want to create a custom button out of a picturebox control.  There will be four different pictures displayed depending on the mouse.

    If mouse_enter display graphics 1 on control. If mouse_down display graphics 2 on control. If mouse_up display graphics 3 on control. If mouse_leave display graphics 4 on control.

    Any links on how to create a custom controls? Or walk thru? Or any examples?

    Saturday, December 11, 2010 6:56 PM

Answers

  • Lord Odyssey;

    Try this:

    Public Class Form1
    
      Private Sub PictureBox1_MouseEnter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.MouseEnter
        Me.PictureBox1.Image = Image.FromFile("C:\Test\IMG1.jpg")
      End Sub
    
      Private Sub PictureBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        Me.PictureBox1.Image = Image.FromFile("C:\Test\IMG2.jpg")
      End Sub
    
      Private Sub PictureBox1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
        Me.PictureBox1.Image = Image.FromFile("C:\Test\IMG3.jpg")
      End Sub
    
      Private Sub PictureBox1_MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.MouseLeave
        Me.PictureBox1.Image = Image.FromFile("C:\Test\IMG4.jpg")
      End Sub
    End Class
    

    Of course you can change the Picture.Image Event in My.Ressources ..

    But basically that what yiu where asking for. So I hope.


    Liebe Grüße Stefan | Cheers Stefan I'm using VB 2008 Express
    • Marked as answer by Lord Odyssey Monday, December 13, 2010 8:54 AM
    Saturday, December 11, 2010 7:38 PM
  • Maybe I didn't understand you also correct now, but lets have a new try.

    You want to create a new control while the program is running. This control shoul look like another control (for first time?)

     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim NPB As New PictureBox
        Me.Controls.Add(NPB)
        Dim OPB As Object = Me.PictureBox1
        NPB.Height = OPB.Height
        NPB.Left = OPB.Left
        NPB.Width = OPB.Width
        NPB.Top = OPB.Top + PictureBox1.Height + 5
        NPB.Image = OPB.Image
    
      End Sub
    
    I put it on a ButtonClick Event now. Hope this is what you are looking for.


    Liebe Grüße Stefan | Cheers Stefan I'm using VB 2008 Express
    • Marked as answer by Lord Odyssey Monday, December 13, 2010 8:54 AM
    Saturday, December 11, 2010 8:05 PM
  • Hi Lord Odyssey

     How do I make a custom Control?
            http://social.msdn.microsoft.com/forums/en-us/Vsexpressvb/thread/6C0DB9A2-E539-49C7-9292-1246BAA5F861
            Question about Properties in Custom Controls
            http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvb/thread/d322ddde-d102-4dce-b2d2-81fc27b5edd3
            Walkthrough: Authoring a User Control with Visual Basic .NET
            http://msdn.microsoft.com/en-us/library/c316f119(vs.71).aspx
            The Code Project in C# Writing your Custom Control: step by step
            http://www.codeproject.com/KB/miscctrl/cutebutton.aspx
            Create a custom WPF control
            http://msdn.microsoft.com/en-us/library/cc295235.aspx
            Developing Custom ASP.NET Server Controls
            http://msdn.microsoft.com/en-us/library/zt27tfhy.aspx
            Control Authoring Overview
            http://msdn.microsoft.com/en-us/library/ms745025.aspx
            How to add control to the toolbox
            http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvb/thread/21283cb6-2898-4826-b2f4-1f777803330e

     


    Be nice and courteous always, with the measure you use, it will be measured to you. Give a person a fish, and you feed him/her for a day. Teach a person to fish, and you feed him/her for life. Thank you for your time and help. We always be greatful.
    • Proposed as answer by Giftzwockel Saturday, December 11, 2010 11:54 PM
    • Marked as answer by Lord Odyssey Monday, December 13, 2010 8:54 AM
    Saturday, December 11, 2010 8:58 PM
  • My mistake. My english is not very good so sometimes I misunderstand the question.

    For a better undserstanding:

    Which controls are existing?

    Which controls do you want create whith which event. Which toolbox we are talking about?

     


    Liebe Grüße Stefan | Cheers Stefan I'm using VB 2008 Express
    • Marked as answer by Lord Odyssey Monday, December 13, 2010 8:54 AM
    Saturday, December 11, 2010 9:23 PM
  • Just curious why, as you want to create a custom button, you feel it necessary to use a picturebox.

    Why not create a new class which inherits from button and add the necessary code to change the image.

    If you want to reuse this in other applications then create it as a class library and build it as a dll which you can then add to any project which needs it.

    • Marked as answer by Lord Odyssey Monday, December 13, 2010 8:53 AM
    Sunday, December 12, 2010 12:17 AM
  • I'm using Visual Basic 10 Express. I can't find the class library template.

    Then there is something wrong with your installation.  File | New Project should display a list of project types which includes Class Library.

    I've been told you can't make a dll file in vb10 express.

    You've been misinformed.

     

    • Marked as answer by Lord Odyssey Monday, December 13, 2010 8:53 AM
    Sunday, December 12, 2010 11:59 PM

All replies

  • Lord Odyssey;

    Try this:

    Public Class Form1
    
      Private Sub PictureBox1_MouseEnter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.MouseEnter
        Me.PictureBox1.Image = Image.FromFile("C:\Test\IMG1.jpg")
      End Sub
    
      Private Sub PictureBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        Me.PictureBox1.Image = Image.FromFile("C:\Test\IMG2.jpg")
      End Sub
    
      Private Sub PictureBox1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
        Me.PictureBox1.Image = Image.FromFile("C:\Test\IMG3.jpg")
      End Sub
    
      Private Sub PictureBox1_MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.MouseLeave
        Me.PictureBox1.Image = Image.FromFile("C:\Test\IMG4.jpg")
      End Sub
    End Class
    

    Of course you can change the Picture.Image Event in My.Ressources ..

    But basically that what yiu where asking for. So I hope.


    Liebe Grüße Stefan | Cheers Stefan I'm using VB 2008 Express
    • Marked as answer by Lord Odyssey Monday, December 13, 2010 8:54 AM
    Saturday, December 11, 2010 7:38 PM
  • Sorry I should have givin more info. I just gave a simple example. I know how to do those things.

    I want to be able to add the new custom control that I create from an existing control or a new control I create from scratch to the toolbox for my future projects.

    Saturday, December 11, 2010 7:45 PM
  • Maybe I didn't understand you also correct now, but lets have a new try.

    You want to create a new control while the program is running. This control shoul look like another control (for first time?)

     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim NPB As New PictureBox
        Me.Controls.Add(NPB)
        Dim OPB As Object = Me.PictureBox1
        NPB.Height = OPB.Height
        NPB.Left = OPB.Left
        NPB.Width = OPB.Width
        NPB.Top = OPB.Top + PictureBox1.Height + 5
        NPB.Image = OPB.Image
    
      End Sub
    
    I put it on a ButtonClick Event now. Hope this is what you are looking for.


    Liebe Grüße Stefan | Cheers Stefan I'm using VB 2008 Express
    • Marked as answer by Lord Odyssey Monday, December 13, 2010 8:54 AM
    Saturday, December 11, 2010 8:05 PM
  • You know how you create a new form and add controls to it at design time from the toolbox, I want to be able to design a new control and add it to the tool box so I can use it in my future programs.

    I want to create a new enhanced button control using the picturebox control. I've already written all of the code to draw the graphics on the button and all of the events to interact with the control. I just need to know how to add it to the toolbox for later use.

    Saturday, December 11, 2010 8:57 PM
  • Hi Lord Odyssey

     How do I make a custom Control?
            http://social.msdn.microsoft.com/forums/en-us/Vsexpressvb/thread/6C0DB9A2-E539-49C7-9292-1246BAA5F861
            Question about Properties in Custom Controls
            http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvb/thread/d322ddde-d102-4dce-b2d2-81fc27b5edd3
            Walkthrough: Authoring a User Control with Visual Basic .NET
            http://msdn.microsoft.com/en-us/library/c316f119(vs.71).aspx
            The Code Project in C# Writing your Custom Control: step by step
            http://www.codeproject.com/KB/miscctrl/cutebutton.aspx
            Create a custom WPF control
            http://msdn.microsoft.com/en-us/library/cc295235.aspx
            Developing Custom ASP.NET Server Controls
            http://msdn.microsoft.com/en-us/library/zt27tfhy.aspx
            Control Authoring Overview
            http://msdn.microsoft.com/en-us/library/ms745025.aspx
            How to add control to the toolbox
            http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvb/thread/21283cb6-2898-4826-b2f4-1f777803330e

     


    Be nice and courteous always, with the measure you use, it will be measured to you. Give a person a fish, and you feed him/her for a day. Teach a person to fish, and you feed him/her for life. Thank you for your time and help. We always be greatful.
    • Proposed as answer by Giftzwockel Saturday, December 11, 2010 11:54 PM
    • Marked as answer by Lord Odyssey Monday, December 13, 2010 8:54 AM
    Saturday, December 11, 2010 8:58 PM
  • My question was misleading, I should have asked "How do I create a custom control using a picturebox to add to the toolbox for future use"

    Sorry my bad :(

    Saturday, December 11, 2010 9:01 PM
  • Thanks for your reply VBStudy :)

    Looking thru the links you gave me I noticed I need the Windows Control Library Template. I'm using Visual Basic 10 Express and it doesn't have this :(

    So the only way I can create new controls to add to the tool box is to upgrade to Visual Basic 10 Professional?

    Saturday, December 11, 2010 9:21 PM
  • My mistake. My english is not very good so sometimes I misunderstand the question.

    For a better undserstanding:

    Which controls are existing?

    Which controls do you want create whith which event. Which toolbox we are talking about?

     


    Liebe Grüße Stefan | Cheers Stefan I'm using VB 2008 Express
    • Marked as answer by Lord Odyssey Monday, December 13, 2010 8:54 AM
    Saturday, December 11, 2010 9:23 PM
  • Thanks for you time Giftzwockel :)

    Both of your examples would work fine if I only needed to create the object in code.

    VBStudy sent me in the right direction.

    Saturday, December 11, 2010 9:25 PM
  • Just curious why, as you want to create a custom button, you feel it necessary to use a picturebox.

    Why not create a new class which inherits from button and add the necessary code to change the image.

    If you want to reuse this in other applications then create it as a class library and build it as a dll which you can then add to any project which needs it.

    • Marked as answer by Lord Odyssey Monday, December 13, 2010 8:53 AM
    Sunday, December 12, 2010 12:17 AM
  • Thanks Dave299 for your response. :)

     

    I'm using Visual Basic 10 Express. I can't find the class library template. I've been told you can't make a dll file in vb10 express.

    Is there a workaround for this?

    Sunday, December 12, 2010 9:49 PM
  • I'm using Visual Basic 10 Express. I can't find the class library template.

    Then there is something wrong with your installation.  File | New Project should display a list of project types which includes Class Library.

    I've been told you can't make a dll file in vb10 express.

    You've been misinformed.

     

    • Marked as answer by Lord Odyssey Monday, December 13, 2010 8:53 AM
    Sunday, December 12, 2010 11:59 PM
  • Thanks Dave for your help I found it.
    Monday, December 13, 2010 8:53 AM