VB.Net Tutorial/GUI/Label — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 15:57, 26 мая 2010
Содержание
Add image to Label
<source lang="vbnet">imports System imports System.Drawing imports System.Windows.Forms public class LabelImage: inherits Form
public sub New() Size = new Size(300,250) dim img as Image = Image.FromFile("yourfile.jpg") dim lblImage as Label = new Label() lblImage.Parent = me lblImage.Location = new Point(0, 0) lblImage.Image = img lblImage.Anchor = AnchorStyles.Top or AnchorStyles.Right lblImage.Size = new Size(img.Width, img.Height) end sub public shared sub Main() Application.Run(new LabelImage()) end sub
end class</source>
Change Label font
<source lang="vbnet">Imports System.Windows.Forms Imports System.Drawing Imports System.Drawing.Drawing2D public class CheckBoxEvent
public Shared Sub Main Application.Run(New FrmCheckBox) End Sub
End class
Public Class FrmCheckBox
Inherits System.Windows.Forms.Form
- Region " Windows Form Designer generated code "
Public Sub New() MyBase.New() "This call is required by the Windows Form Designer. InitializeComponent() "Add any initialization after the InitializeComponent() call End Sub "Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub Friend WithEvents chkItalic As System.Windows.Forms.CheckBox Friend WithEvents lblOutput As System.Windows.Forms.Label Friend WithEvents chkBold As System.Windows.Forms.CheckBox "Required by the Windows Form Designer Private components As System.ruponentModel.Container "NOTE: The following procedure is required by the Windows Form Designer "It can be modified using the Windows Form Designer. "Do not modify it using the code editor. <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.lblOutput = New System.Windows.Forms.Label() Me.chkItalic = New System.Windows.Forms.CheckBox() Me.chkBold = New System.Windows.Forms.CheckBox() Me.SuspendLayout() " "lblOutput " Me.lblOutput.Font = New System.Drawing.Font("Microsoft Sans Serif", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.lblOutput.Location = New System.Drawing.Point(48, 24) Me.lblOutput.Name = "lblOutput" Me.lblOutput.Size = New System.Drawing.Size(208, 24) Me.lblOutput.TabIndex = 0 Me.lblOutput.Text = "Watch the font style change" " "chkItalic " Me.chkItalic.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.chkItalic.Location = New System.Drawing.Point(152, 64) Me.chkItalic.Name = "chkItalic" Me.chkItalic.Size = New System.Drawing.Size(64, 24) Me.chkItalic.TabIndex = 2 Me.chkItalic.Text = "Italic" " "chkBold " Me.chkBold.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.chkBold.Location = New System.Drawing.Point(80, 64) Me.chkBold.Name = "chkBold" Me.chkBold.Size = New System.Drawing.Size(56, 24) Me.chkBold.TabIndex = 1 Me.chkBold.Text = "Bold" " "FrmCheckBox " Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14) Me.ClientSize = New System.Drawing.Size(292, 117) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.chkItalic, Me.chkBold, Me.lblOutput}) Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 9!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Name = "FrmCheckBox" Me.Text = "CheckBoxTest" Me.ResumeLayout(False) End Sub
- End Region
Private Sub chkItalic_CheckedChanged _ (ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles chkItalic.CheckedChanged lblOutput.Font = New Font("Microsoft Sans Serif", _ lblOutput.Font.Size, lblOutput.Font.Style Xor FontStyle.Italic) End Sub Private Sub chkBold_CheckedChanged _ (ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles chkBold.CheckedChanged lblOutput.Font = New Font("Microsoft Sans Serif", _ lblOutput.Font.Size, lblOutput.Font.Style Xor FontStyle.Bold) End Sub
End Class</source>
Change Label text and width in Button action
<source lang="vbnet">Imports System.Windows.Forms public class LabelWidthTextSettingInButtonAction
public Shared Sub Main Application.Run(New frmFirstProgram) End Sub
End class Public Class frmFirstProgram
Inherits System.Windows.Forms.Form
- Region " Windows Form Designer generated code "
Public Sub New() MyBase.New() "This call is required by the Windows Form Designer. InitializeComponent() "Add any initialization after the InitializeComponent() call End Sub "Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub "Required by the Windows Form Designer Private components As System.ruponentModel.IContainer "NOTE: The following procedure is required by the Windows Form Designer "It can be modified using the Windows Form Designer. "Do not modify it using the code editor. Friend WithEvents lblName As System.Windows.Forms.Label Friend WithEvents btnChange As System.Windows.Forms.Button Friend WithEvents btnExit As System.Windows.Forms.Button <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.lblName = New System.Windows.Forms.Label() Me.btnChange = New System.Windows.Forms.Button() Me.btnExit = New System.Windows.Forms.Button() Me.SuspendLayout() " "lblName " Me.lblName.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.lblName.Location = New System.Drawing.Point(16, 16) Me.lblName.Name = "lblName" Me.lblName.Size = New System.Drawing.Size(104, 24) Me.lblName.TabIndex = 0 Me.lblName.Text = "test" Me.lblName.TextAlign = System.Drawing.ContentAlignment.MiddleCenter " "btnChange " Me.btnChange.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.btnChange.Location = New System.Drawing.Point(16, 176) Me.btnChange.Name = "btnChange" Me.btnChange.TabIndex = 1 Me.btnChange.Text = "&Change" " "btnExit " Me.btnExit.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.btnExit.Location = New System.Drawing.Point(176, 176) Me.btnExit.Name = "btnExit" Me.btnExit.TabIndex = 2 Me.btnExit.Text = "E&xit" " "frmFirstProgram " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 273) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnExit, Me.btnChange, Me.lblName}) Me.Name = "frmFirstProgram" Me.Text = "My First Program" Me.ResumeLayout(False) End Sub
- End Region
Private Sub btnChange_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChange.Click lblName.Width = 200 lblName.Text = "Some text" End Sub Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click frmFirstProgram.ActiveForm.Dispose() End Sub
End Class</source>
Label mouse click event
<source lang="vbnet">Imports System.Windows.Forms public class GenericEventHandler
public Shared Sub Main Application.Run(New FrmSimple) End Sub
End class
Public Class FrmSimple
Inherits System.Windows.Forms.Form
- Region " Windows Form Designer generated code "
Public Sub New() MyBase.New() " This call is required by the Windows Form Designer. InitializeComponent()
" Add any initialization after the " InitializeComponent() call End Sub " New " Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose( _ ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub " Dispose Friend WithEvents lblOutput As System.Windows.Forms.Label " Required by the Windows Form Designer Private components As System.ruponentModel.Container " NOTE: The following procedure is required by " the Windows Form Designer. " It can be modified using the Windows Form Designer. " Do not modify it using the code editor. <System.Diagnostics.DebuggerStepThrough()> _ Private Sub InitializeComponent() Me.lblOutput = New System.Windows.Forms.Label() Me.SuspendLayout() " "lblOutput " Me.lblOutput.Location = New System.Drawing.Point(16, 16) Me.lblOutput.Name = "lblOutput" Me.lblOutput.Size = New System.Drawing.Size(152, 80) Me.lblOutput.TabIndex = 0 Me.lblOutput.Text = "Click Me!" Me.lblOutput.TextAlign = System.Drawing.ContentAlignment.MiddleCenter " "FrmSimple " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(176, 101) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.lblOutput}) Me.Name = "FrmSimple" Me.Text = "SimpleEventExample" Me.ResumeLayout(False) End Sub
- End Region
Private Sub lblOutput_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles lblOutput.Click MessageBox.Show("Label was clicked") End Sub
End Class</source>
Label TextAlign
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class TextAlignLabel
public Shared Sub Main Application.Run(New Form1) End Sub
End class Public Class Form1
Inherits System.Windows.Forms.Form
- Region " Windows Form Designer generated code "
Public Sub New() MyBase.New() "This call is required by the Windows Form Designer. InitializeComponent() "Add any initialization after the InitializeComponent() call End Sub "Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub "Required by the Windows Form Designer Private components As System.ruponentModel.IContainer "NOTE: The following procedure is required by the Windows Form Designer "It can be modified using the Windows Form Designer. "Do not modify it using the code editor. Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents Label2 As System.Windows.Forms.Label Friend WithEvents Label3 As System.Windows.Forms.Label Friend WithEvents Label4 As System.Windows.Forms.Label Friend WithEvents Label5 As System.Windows.Forms.Label Friend WithEvents Label6 As System.Windows.Forms.Label Friend WithEvents Label7 As System.Windows.Forms.Label Friend WithEvents Label8 As System.Windows.Forms.Label Friend WithEvents Label9 As System.Windows.Forms.Label Friend WithEvents Label10 As System.Windows.Forms.Label <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.Label1 = New System.Windows.Forms.Label Me.Label2 = New System.Windows.Forms.Label Me.Label3 = New System.Windows.Forms.Label Me.Label4 = New System.Windows.Forms.Label Me.Label5 = New System.Windows.Forms.Label Me.Label6 = New System.Windows.Forms.Label Me.Label7 = New System.Windows.Forms.Label Me.Label8 = New System.Windows.Forms.Label Me.Label9 = New System.Windows.Forms.Label Me.Label10 = New System.Windows.Forms.Label Me.SuspendLayout() " "Label1 " Me.Label1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle Me.Label1.Location = New System.Drawing.Point(16, 56) Me.Label1.Name = "Label1" Me.Label1.TabIndex = 0 Me.Label1.Text = "Label1" " "Label2 " Me.Label2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle Me.Label2.Location = New System.Drawing.Point(136, 56) Me.Label2.Name = "Label2" Me.Label2.TabIndex = 1 Me.Label2.Text = "Label2" Me.Label2.TextAlign = System.Drawing.ContentAlignment.TopCenter " "Label3 " Me.Label3.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle Me.Label3.Location = New System.Drawing.Point(256, 56) Me.Label3.Name = "Label3" Me.Label3.TabIndex = 2 Me.Label3.Text = "Label3" Me.Label3.TextAlign = System.Drawing.ContentAlignment.TopRight " "Label4 " Me.Label4.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle Me.Label4.Location = New System.Drawing.Point(16, 112) Me.Label4.Name = "Label4" Me.Label4.TabIndex = 3 Me.Label4.Text = "Label4" Me.Label4.TextAlign = System.Drawing.ContentAlignment.MiddleLeft " "Label5 " Me.Label5.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle Me.Label5.Location = New System.Drawing.Point(136, 112) Me.Label5.Name = "Label5" Me.Label5.TabIndex = 4 Me.Label5.Text = "Label5" Me.Label5.TextAlign = System.Drawing.ContentAlignment.MiddleCenter " "Label6 " Me.Label6.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle Me.Label6.Location = New System.Drawing.Point(256, 112) Me.Label6.Name = "Label6" Me.Label6.TabIndex = 5 Me.Label6.Text = "Label6" Me.Label6.TextAlign = System.Drawing.ContentAlignment.MiddleRight " "Label7 " Me.Label7.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle Me.Label7.Location = New System.Drawing.Point(16, 168) Me.Label7.Name = "Label7" Me.Label7.TabIndex = 6 Me.Label7.Text = "Label7" Me.Label7.TextAlign = System.Drawing.ContentAlignment.BottomLeft " "Label8 " Me.Label8.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle Me.Label8.Location = New System.Drawing.Point(136, 168) Me.Label8.Name = "Label8" Me.Label8.TabIndex = 7 Me.Label8.Text = "Label8" Me.Label8.TextAlign = System.Drawing.ContentAlignment.BottomCenter " "Label9 " Me.Label9.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle Me.Label9.Location = New System.Drawing.Point(256, 168) Me.Label9.Name = "Label9" Me.Label9.TabIndex = 8 Me.Label9.Text = "Label9" Me.Label9.TextAlign = System.Drawing.ContentAlignment.BottomRight " "Label10 " Me.Label10.Font = New System.Drawing.Font("Microsoft Sans Serif", 24.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label10.Location = New System.Drawing.Point(0, 0) Me.Label10.Name = "Label10" Me.Label10.Size = New System.Drawing.Size(192, 48) Me.Label10.TabIndex = 9 Me.Label10.Text = "Labels" " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(376, 229) Me.Controls.Add(Me.Label10) Me.Controls.Add(Me.Label9) Me.Controls.Add(Me.Label8) Me.Controls.Add(Me.Label7) Me.Controls.Add(Me.Label6) Me.Controls.Add(Me.Label5) Me.Controls.Add(Me.Label4) Me.Controls.Add(Me.Label3) Me.Controls.Add(Me.Label2) Me.Controls.Add(Me.Label1) Me.Name = "Form1" Me.Text = "Form1" Me.ResumeLayout(False) End Sub
- End Region
End Class</source>
Multiline Label
<source lang="vbnet">Imports System.Windows.Forms public class MultilineLabel
public Shared Sub Main Application.Run(New Form1) End Sub
End class Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load lblInstructions.Text = "Print this message and either:" & vbCrLf & _ " - Mail it to the recipient" & vbCrLf & _ " - Fax it to the recipient" & vbCrLf & _ " - Throw it away" End Sub
End Class <Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _ Partial Public Class Form1
Inherits System.Windows.Forms.Form "Form overrides dispose to clean up the component list. <System.Diagnostics.DebuggerNonUserCode()> _ Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing AndAlso components IsNot Nothing Then components.Dispose() End If MyBase.Dispose(disposing) End Sub "Required by the Windows Form Designer Private components As System.ruponentModel.IContainer "NOTE: The following procedure is required by the Windows Form Designer "It can be modified using the Windows Form Designer. "Do not modify it using the code editor. <System.Diagnostics.DebuggerStepThrough()> _ Private Sub InitializeComponent() Me.lblInstructions = New System.Windows.Forms.Label Me.SuspendLayout() " "lblInstructions " Me.lblInstructions.AutoSize = True Me.lblInstructions.Location = New System.Drawing.Point(8, 8) Me.lblInstructions.Name = "lblInstructions" Me.lblInstructions.Size = New System.Drawing.Size(67, 13) Me.lblInstructions.TabIndex = 0 Me.lblInstructions.Text = "lblInstructions" " "Form1 " Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(286, 116) Me.Controls.Add(Me.lblInstructions) Me.Name = "Form1" Me.Text = "MultiLineLabel" Me.ResumeLayout(False) Me.PerformLayout() End Sub Friend WithEvents lblInstructions As System.Windows.Forms.Label
End Class</source>
Set Label"s ImageList
<source lang="vbnet">imports System imports System.Drawing imports System.Windows.Forms public class ImageLists : inherits Form
dim imgList as ImageList = new ImageList() dim lbl as Label dim lnk as LinkLabel dim btn as Button dim nmbrUpDown as NumericUpDown public sub New() Size = new Size(300,300) dim img as Image dim i as integer dim arFiles as string() = {"1.ico","2.ico","3.ico","4.ico"} for i = 0 to arFiles.Length - 1 img = Image.FromFile(arFiles(i)) imgList.Images.Add(img) next imgList.ImageSize = new Size(32, 32) img = Image.FromFile("5.ico") imgList.Images(imgList.Images.Count - 1) = img lbl = new Label() lbl.Parent = me lbl.Text = "Label" lbl.Location = new Point(0,0) lbl.Size = new Size (200,20) lbl.BorderStyle = BorderStyle.Fixed3D lbl.ImageList = imgList lbl.ImageIndex = 0 lbl.ImageAlign = ContentAlignment.MiddleRight " Create numeric updown to select the image nmbrUpDown = new NumericUpDown() nmbrUpDown.Parent = me nmbrUpDown.Location = new Point(0, 60) nmbrUpDown.Value = 0 nmbrUpDown.Minimum = 0 nmbrUpDown.Maximum = imgList.Images.Count - 1 nmbrUpDown.Width = 50 nmbrUpDown.ReadOnly = true AddHandler nmbrUpDown.ValueChanged,AddressOf nmbrUpDown_ValueChanged end sub public shared sub Main() Application.Run(new ImageLists()) end sub private sub nmbrUpDown_ValueChanged(ByVal sender as object,ByVal e as EventArgs) dim n as NumericUpDown = CType(sender, NumericUpDown) lbl.ImageIndex = CType(n.Value, Integer) end sub
end class</source>
Use Color dialog to set the Label foreground color
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class UseColorDialogToSetLabelForeGroundColor
public Shared Sub Main Application.Run(New Form1) End Sub
End class Public Class Form1
Inherits System.Windows.Forms.Form Public Sub New() MyBase.New() InitializeComponent() End Sub Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub Private components As System.ruponentModel.IContainer Friend WithEvents Button1 As System.Windows.Forms.Button Friend WithEvents ColorDialog1 As System.Windows.Forms.ColorDialog Friend WithEvents Label1 As System.Windows.Forms.Label <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.Button1 = New System.Windows.Forms.Button Me.ColorDialog1 = New System.Windows.Forms.ColorDialog Me.Label1 = New System.Windows.Forms.Label Me.SuspendLayout() " "Button1 " Me.Button1.Location = New System.Drawing.Point(88, 152) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing.Size(96, 32) Me.Button1.TabIndex = 1 Me.Button1.Text = "Change Color" " "Label1 " Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 27.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label1.Location = New System.Drawing.Point(72, 24) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(120, 104) Me.Label1.TabIndex = 2 Me.Label1.Text = "www.vbex.ru" " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(256, 214) Me.Controls.Add(Me.Label1) Me.Controls.Add(Me.Button1) Me.Name = "Form1" Me.Text = "Color Setting" Me.ResumeLayout(False) End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ColorDialog1.ShowDialog() Label1.ForeColor = ColorDialog1.Color End Sub
End Class</source>