言わずと知れた Label です。
Label クラスの解説 にはこう書いてあります。
[引用]
通常、Label コントロールは、コントロールについて説明するテキストを表示するために使用されます。
[/引用]
はい。そうですね。確かにそういう使い方をしたのがほとんどでございます。
そして、Label のメンバを眺めるとほとんど Control を継承したものばかりなので、
今回触ってるもののほとんどが Control を継承しているものであれば、別に Label でなくても出来るものばかりです。
■参考文献
Label コントロール (Windows フォーム)
Label クラス
■実行画像
AutoEllipsis を True にしたラベルにカーソルを持っていったところ
Public Class LabelTest
Private Enum Action
BringToFront
SendToBack
End Enum
Private Sub LabelTest_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' AutoEllipsis を使ってみる
With Me.Label1
.AutoSize = False
.AutoEllipsis = True
.Text = "longlonglonglonglonglonglonglonglonglongtext"
End With
' BorderStyle を変える
With Me.Label2
.Text = .BorderStyle.ToString()
.BackColor = Color.LightBlue
End With
With Me.ComboBox1
.DropDownStyle = ComboBoxStyle.DropDownList
With .Items
.Add(BorderStyle.Fixed3D)
.Add(BorderStyle.FixedSingle)
.Add(BorderStyle.None)
End With
.SelectedItem = Me.Label2.BorderStyle
AddHandler .SelectedIndexChanged, AddressOf BorderStyleChange
End With
' BringToFront・SendToBack
Dim lb As Label = New Label()
lb.BackColor = Color.Salmon
lb.Location = New Point(Me.Label3.Location.X, Me.Label3.Location.Y)
lb.AutoSize = False
lb.Text = "test"
' AutoSize ではなくて PreferredWidth と PreferredHeight を使ってみる
lb.Size = New Size(lb.PreferredWidth, lb.PreferredHeight)
Me.Controls.Add(lb)
With Me.ComboBox2
.DropDownStyle = ComboBoxStyle.DropDownList
With .Items
.Add(Action.BringToFront)
.Add(Action.SendToBack)
End With
.SelectedIndex = -1
AddHandler .SelectedIndexChanged, AddressOf ActionChange
End With
' ImageAlign・TextAlign
With Me.Label4
.Image = My.Resources.中さん
.AutoSize = False
.Size = New Size(My.Resources.中さん.Width + 20, My.Resources.中さん.Height + 20)
.BackColor = Color.LightGreen
End With
Me.SetAlignmentToText()
With Me.ComboBox3
.DropDownStyle = ComboBoxStyle.DropDownList
With .Items
.Add(ContentAlignment.BottomCenter)
.Add(ContentAlignment.BottomLeft)
.Add(ContentAlignment.BottomRight)
.Add(ContentAlignment.MiddleCenter)
.Add(ContentAlignment.MiddleLeft)
.Add(ContentAlignment.MiddleRight)
.Add(ContentAlignment.TopCenter)
.Add(ContentAlignment.TopLeft)
.Add(ContentAlignment.TopRight)
End With
.SelectedItem = Me.Label4.ImageAlign
AddHandler .SelectedIndexChanged, AddressOf ImageAlignmentChange
End With
With Me.ComboBox4
.DropDownStyle = ComboBoxStyle.DropDownList
Dim enm As IEnumerator = Me.ComboBox3.Items.GetEnumerator()
While enm.MoveNext
.Items.Add(enm.Current)
End While
.SelectedItem = Me.Label4.TextAlign
AddHandler .SelectedIndexChanged, AddressOf TextAlignmentChange
End With
' ニーモニックキーを使う
With Me.Label5
.UseMnemonic = True
.Text = "&test"
End With
End Sub
Private Sub BorderStyleChange(ByVal sender As Object, ByVal e As System.EventArgs)
Me.Label2.BorderStyle = DirectCast(DirectCast(sender, ComboBox).SelectedItem, BorderStyle)
Me.Label2.Text = Me.Label2.BorderStyle.ToString()
End Sub
Private Sub ActionChange(ByVal sender As Object, ByVal e As System.EventArgs)
Dim ac As Action = DirectCast(DirectCast(sender, ComboBox).SelectedItem, Action)
If ac = Action.BringToFront Then
Me.Label3.BringToFront()
Else
Me.Label3.SendToBack()
End If
End Sub
Private Sub ImageAlignmentChange(ByVal sender As Object, ByVal e As System.EventArgs)
Me.Label4.ImageAlign = DirectCast(DirectCast(sender, ComboBox).SelectedItem, ContentAlignment)
Me.SetAlignmentToText()
End Sub
Private Sub TextAlignmentChange(ByVal sender As Object, ByVal e As System.EventArgs)
Me.Label4.TextAlign = DirectCast(DirectCast(sender, ComboBox).SelectedItem, ContentAlignment)
Me.SetAlignmentToText()
End Sub
Private Sub SetAlignmentToText()
Me.Label4.Text = "Image:" & Me.Label4.ImageAlign.ToString() & ControlChars.NewLine & _
"Text:" & Me.Label4.TextAlign.ToString()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Using g As Graphics = Me.Label5.CreateGraphics()
Using pen As System.Drawing.Pen = New Pen(Color.Orange, 1.0F)
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDot
' 線を引く
g.DrawLine(pen, 0.0F, CShort(Me.Label5.Height / 2), CShort(Me.Width), CShort(Me.Label5.Height / 2))
End Using
End Using
End Sub
End Class