VB.Net/GUI/LinkLabel
Содержание
- 1 Click LinkLabel to Open website in your Default Web Browser
- 2 Set different URL for one LinkLabel
- 3 Use LinkLabel and load the link in your system default browser
- 4 Using LinkLabels to create hyperlinks: browse C:\ drive
- 5 Using LinkLabels to create hyperlinks: Open Browser
- 6 Using LinkLabels to create hyperlinks: Open Notepad
Click LinkLabel to Open website in your Default Web Browser
<source lang="vbnet"> Imports System Imports System.Windows.Forms
Public Class MainClass
Shared Sub Main() Dim form1 As Form = New Form1 Application.Run(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 lnkBuy As System.Windows.Forms.LinkLabel Friend WithEvents lnkWebSite As System.Windows.Forms.LinkLabel <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.lnkBuy = New System.Windows.Forms.LinkLabel() Me.lnkWebSite = New System.Windows.Forms.LinkLabel() Me.SuspendLayout() " "lnkBuy " Me.lnkBuy.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.lnkBuy.LinkArea = New System.Windows.Forms.LinkArea(0, 0) Me.lnkBuy.Location = New System.Drawing.Point(16, 60) Me.lnkBuy.Name = "lnkBuy" Me.lnkBuy.Size = New System.Drawing.Size(244, 48) Me.lnkBuy.TabIndex = 3 Me.lnkBuy.Text = "www.vbex.ru" " "lnkWebSite " Me.lnkWebSite.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.lnkWebSite.LinkArea = New System.Windows.Forms.LinkArea(0, 0) Me.lnkWebSite.Location = New System.Drawing.Point(16, 16) Me.lnkWebSite.Name = "lnkWebSite" Me.lnkWebSite.Size = New System.Drawing.Size(248, 36) Me.lnkWebSite.TabIndex = 2 Me.lnkWebSite.Text = "The whole line is a LinkLabel" " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 150) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.lnkBuy, Me.lnkWebSite}) Me.Name = "Form1" Me.Text = "Link Examples" Me.ResumeLayout(False) End Sub
- End Region
Private Sub lnkBuy_LinkClicked(ByVal sender As Object, _ ByVal e As LinkLabelLinkClickedEventArgs) Handles lnkBuy.LinkClicked, _ lnkWebSite.LinkClicked e.Link.Visited = True System.Diagnostics.Process.Start(CType(e.Link.LinkData, String)) End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load lnkBuy.Links.Add(0, 2, "http://www.vbex.ru") lnkBuy.Links.Add(6, 10, "http://www.vbex.ru") lnkWebSite.Links.Add(0, 10, "http://www.vbex.ru") End Sub
End Class
</source>
Set different URL for one LinkLabel
<source lang="vbnet"> Imports System Imports System.Data Imports System.Windows.Forms Imports System.Drawing Imports System.Diagnostics Public Class MainClass
Shared Sub Main() Dim form1 As Form = New Form1 Application.Run(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 Dim new_link As System.Windows.Forms.LinkLabel.Link llblPangram.Links.Clear() new_link = llblPangram.Links.Add(16, 3) new_link.LinkData = "http://www.vbex.ru" new_link = llblPangram.Links.Add(40, 3) new_link.LinkData = "http://www.vbex.ru" End Sub " Display the URL associated with the clicked link. Private Sub llblPangram_LinkClicked(ByVal sender As System.Object, _ ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) _ Handles llblPangram.LinkClicked System.Diagnostics.Process.Start(e.Link.LinkData.ToString) 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.llblPangram = New System.Windows.Forms.LinkLabel Me.SuspendLayout() " "llblPangram " Me.llblPangram.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.llblPangram.Location = New System.Drawing.Point(8, 80) Me.llblPangram.Name = "llblPangram" Me.llblPangram.Size = New System.Drawing.Size(296, 24) Me.llblPangram.TabIndex = 1 Me.llblPangram.TabStop = True Me.llblPangram.Text = "The quick brown fox jumps over the lazy dog." " "Form1 " Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(325, 181) Me.Controls.Add(Me.llblPangram) Me.Name = "Form1" Me.Text = "UseLinkLabel" Me.ResumeLayout(False) Me.PerformLayout() End Sub Friend WithEvents llblPangram As System.Windows.Forms.LinkLabel
End Class
</source>
Use LinkLabel and load the link in your system default browser
<source lang="vbnet"> Imports System Imports System.Data Imports System.Windows.Forms Imports System.Drawing Imports System.Diagnostics Public Class MainClass
Shared Sub Main() Dim form1 As Form = New Form1 Application.Run(form1) End Sub
End Class
Public Class Form1
Private Sub llblBookUrl_LinkClicked(ByVal sender As System.Object, _ ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) _ Handles llblBookUrl.LinkClicked System.Diagnostics.Process.Start(llblBookUrl.Tag.ToString) 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.llblBookUrl = New System.Windows.Forms.LinkLabel Me.SuspendLayout() " "llblBookUrl " Me.llblBookUrl.AutoSize = True Me.llblBookUrl.Font = New System.Drawing.Font("Microsoft Sans Serif", 14.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.llblBookUrl.Location = New System.Drawing.Point(24, 24) Me.llblBookUrl.Name = "llblBookUrl" Me.llblBookUrl.Size = New System.Drawing.Size(239, 24) Me.llblBookUrl.TabIndex = 0 Me.llblBookUrl.TabStop = True Me.llblBookUrl.Tag = "http://www.java.ru" Me.llblBookUrl.Text = "Visit www.vbex.ru!" " "Form1 " Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(325, 181) Me.Controls.Add(Me.llblBookUrl) Me.Name = "Form1" Me.Text = "UseLinkLabel" Me.ResumeLayout(False) Me.PerformLayout() End Sub Friend WithEvents llblBookUrl As System.Windows.Forms.LinkLabel
End Class
</source>
Using LinkLabels to create hyperlinks: browse C:\ drive
<source lang="vbnet"> Imports System Imports System.Drawing Imports System.Windows.Forms Public Class MainClass
Shared Sub Main() Dim myform As Form = New FrmLinkLabel() Application.Run(myform) End Sub " Main
End Class Public Class FrmLinkLabel
Inherits 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 " linklabels to C: drive, www.deitel.ru and Notepad Friend WithEvents lnklblCDrive As LinkLabel "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.lnklblCDrive = New LinkLabel() Me.SuspendLayout() " "lnklblCDrive " Me.lnklblCDrive.Location = New System.Drawing.Point(72, 16) Me.lnklblCDrive.Name = "lnklblCDrive" Me.lnklblCDrive.TabIndex = 0 Me.lnklblCDrive.TabStop = True Me.lnklblCDrive.Text = "Click to browse C:\" " "FrmLinkLabel " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(264, 149) Me.Controls.AddRange(New Control() {Me.lnklblCDrive}) Me.Name = "FrmLinkLabel" Me.Text = "LinkLabelTest" Me.ResumeLayout(False) End Sub
- End Region
" browse C:\ drive Private Sub lnklblCDrive_LinkClicked _ (ByVal sender As System.Object, ByVal e As _ System.Windows.Forms.LinkLabelLinkClickedEventArgs) _ Handles lnklblCDrive.LinkClicked lnklblCDrive.LinkVisited = True System.Diagnostics.Process.Start("C:\") End Sub " lnklblCDrive
End Class
</source>
Using LinkLabels to create hyperlinks: Open Browser
<source lang="vbnet"> Imports System Imports System.Drawing Imports System.Windows.Forms Public Class MainClass
Shared Sub Main() Dim myform As Form = New FrmLinkLabel() Application.Run(myform) End Sub " Main
End Class Public Class FrmLinkLabel
Inherits 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 " linklabels to C: drive, www.deitel.ru and Notepad Friend WithEvents linkLabel As LinkLabel "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.linkLabel = New LinkLabel() Me.SuspendLayout() " "linkLabel " Me.linkLabel.Location = New System.Drawing.Point(72, 16) Me.linkLabel.Name = "linkLabel" Me.linkLabel.TabIndex = 0 Me.linkLabel.TabStop = True Me.linkLabel.Text = "http://www.vbex.ru" " "FrmLinkLabel " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(364, 149) Me.Controls.AddRange(New Control() {Me.linkLabel}) Me.Name = "FrmLinkLabel" Me.Text = "LinkLabelTest" Me.ResumeLayout(False) End Sub
- End Region
" browse C:\ drive Private Sub linkLabel_LinkClicked _ (ByVal sender As System.Object, ByVal e As _ System.Windows.Forms.LinkLabelLinkClickedEventArgs) _ Handles linkLabel.LinkClicked linkLabel.LinkVisited = True System.Diagnostics.Process.Start _ ("IExplore", "http://www.vbex.ru") End Sub " linkLabel
End Class
</source>
Using LinkLabels to create hyperlinks: Open Notepad
<source lang="vbnet"> Imports System Imports System.Drawing Imports System.Windows.Forms Public Class MainClass
Shared Sub Main() Dim myform As Form = New FrmLinkLabel() Application.Run(myform) End Sub " Main
End Class Public Class FrmLinkLabel
Inherits 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 " linklabels to C: drive, www.deitel.ru and Notepad Friend WithEvents linkLabel As LinkLabel "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.linkLabel = New LinkLabel() Me.SuspendLayout() " "linkLabel " Me.linkLabel.Location = New System.Drawing.Point(72, 16) Me.linkLabel.Name = "linkLabel" Me.linkLabel.TabIndex = 0 Me.linkLabel.TabStop = True Me.linkLabel.Text = "Open Notepad" " "FrmLinkLabel " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(364, 149) Me.Controls.AddRange(New Control() {Me.linkLabel}) Me.Name = "FrmLinkLabel" Me.Text = "LinkLabelTest" Me.ResumeLayout(False) End Sub
- End Region
" browse C:\ drive Private Sub linkLabel_LinkClicked _ (ByVal sender As System.Object, ByVal e As _ System.Windows.Forms.LinkLabelLinkClickedEventArgs) _ Handles linkLabel.LinkClicked linkLabel.LinkVisited = True System.Diagnostics.Process.Start("notepad") End Sub " linkLabel
End Class
</source>