Custom Controls--picture buttons

Source: Internet
Author: User
Button | Control Picture button:

Idea: Simply put a button control on a PictureBox control and add the button to the PictureBox (make sure to drag the PictureBox first, then drag the button) Set the background color of this button (this time is relative to PictureBox) to be transparent.

Imports System.ComponentModel

Public Class Picturebutton

Inherits System.Windows.Forms.UserControl

#Region "code generated by the Windows forms Designer"

' UserControl overrides dispose to clean up the list of components.

Protected Overloads Overrides Sub Dispose (ByVal disposing as Boolean)

If disposing Then

If not (components are nothing) Then

Components. Dispose ()

End If

End If

Mybase.dispose (disposing)

End Sub

' Required by the Windows Forms Designer

Private Components as System.ComponentModel.IContainer

' NOTE: The following procedure is required by the Windows Forms Designer

' You can use the Windows Forms Designer to modify this procedure.

' Do not modify it using the Code Editor.

Friend WithEvents PictureBox1 as System.Windows.Forms.PictureBox

Friend WithEvents Button1 as System.Windows.Forms.Button

<system.diagnostics.debuggerstepthrough () > Private Sub InitializeComponent ()

Me.picturebox1 = New System.Windows.Forms.PictureBox ()

Me.button1 = New System.Windows.Forms.Button ()

Me.suspendlayout ()

'

' PictureBox1

'

Me.PictureBox1.Name = "PictureBox1"

Me.PictureBox1.Size = New System.Drawing.Size (136, 40)

Me.PictureBox1.TabIndex = 0

Me.PictureBox1.TabStop = False

'

' Button1

'

Me.Button1.Name = "Button1"

Me.Button1.TabIndex = 1

Me.Button1.Text = "Button1"

'

' Picturebutton

'

Me.Controls.AddRange (New System.Windows.Forms.Control () {me.button1, me.picturebox1})

Me.Name = "Picturebutton"

Me.resumelayout (False)

End Sub

#End Region

Public Sub New ()

MyBase.New ()

' This call is required by the Windows Forms Designer.

InitializeComponent ()

' Add any initialization after the InitializeComponent () call

Me.Button1.Width = 100 ' Sets the initial size of the button

Me.Button1.Height = 23

Me.Button1.BackColor = color.transparent ' background color transparent

Me.Button1.ForeColor = Color.Black

ME.PICTUREBOX1.CONTROLS.ADD (Me.button1)

End Sub

Private M_text as String ' Set button title

Private A as Integer

' Private m_image as Image

<description ("PictureBox picture.") ") > _

Public property image () as Image

Get

Return Me.PictureBox1.Image

End Get

Set (ByVal Value as image)

Me.PictureBox1.Image = Value

Invalidate ()

End Set

End Property

Shadows Property ForeColor () as Color

Get

Return Me.Button1.ForeColor

End Get

Set (ByVal Value as Color)

Me.Button1.ForeColor = Value

Invalidate ()

End Set

End Property

Shadows Sub Resetforecolor ()

Me.Button1.ForeColor = Systemcolors.controltext

End Sub

'////

' Button's Click event

Event Btnclick (ByVal sender as Object, ByVal e as System.EventArgs)

Private Sub button1_click (ByVal sender as Object, ByVal e as System.EventArgs) Handles Button1.Click

RaiseEvent Btnclick (Me, E)

End Sub

'////

' When the control is resized, the control needs to be redrawn so that the child controls are beautifully ranked

Private Sub filetextbox_resize (ByVal sender as Object, ByVal e as System.EventArgs) Handles mybase.resize

Redrawcontrols ()

End Sub

' The child control automatically continues the container's Font property, so the control will also be redrawn when changing the container's Font property

Protected Overrides Sub onfontchanged (ByVal e as System.EventArgs)

' Jeankie Control update text box

Mybase.onfontchanged (e)

' Redraw control

Redrawcontrols ()

End Sub

' Redraw control

Private Sub Redrawcontrols ()

' Control width

Dim width as Integer = Me.ClientRectangle.Width ' Get work area width

' Height of the button to determine the height of the control

Dim btnside as Integer = Button1.height

Dim btnwidth as Integer = Button1.width

If Me.ClientRectangle.Height <> btnside Then

' Set the size of the control's work area

' Me.setclientsizecore (Btnwidth, Btnside)

Me.setclientsizecore (width, btnside) ' The width of the workspace is used here because: Buttons and PictureBox can adjust width

' The above statement fires a nested resize event, so you need to exit immediately, and if you do not exit, you will repeatedly call into the dead loop

Exit Sub

End If

' Resize child controls

' Txt.setbounds (0, 0, width, btnside)

' Btn.setbounds (width-19, 2, btnSide-4)

Me.PictureBox1.SetBounds (0, 0, width, btnside)

Me.PictureBox1.SizeMode = Pictureboxsizemode.stretchimage

Me.Button1.SetBounds (0, 0, width, btnside)

End Sub End Class



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.