 |
Windows Controls: The Link Label |
|
|
In the past, to add an internet or email link to a form,
there were many steps to follow. As the internet and email had become an
integral part of the culture, it was also time to have an appropriate
control adapted for this need. To address this issue, a control called
LinkLabel is available from the .NET Framework.
|
|
Characteristics of the Link Label
|
|
As its name indicates, the LinkLabel control
allows you to create a link. If you have done web development, you would
know that a link can be made fancy by varying its colors. A link typically
uses three different colors (actually, if you use cascading style sheet, you
can add a fourth color as hover):
- Link: This is the general color used on a link that a user has not
clicked before. The LinkLabel class defines this color through
the LinkColor property.
- Active Link: When the user clicks a link, its target typically opens
if everything is alright. This causes the link to change its color
status. The color of an active link is represented in the LinkLabel
class with the ActiveLinkColor property.
- Visited: When a user comes to a page or the document that provides a
link, you can indicate whether the link was previously accessed or not.
This is done by displaying a color other than the link color. The color
of a previously visited link is represented in the LinkLabel
class by the VisitedLinkColor property.
After creating a link, when the user accesses it, you
can define some behavior the link would exhibit to the user, such as getting
underlined or not underlined when the mouse passes over the link. The
behaviors of a link can be controlled through the LinkBehavior
property whose values are AlwaysUnderline, HoverUnderline,
NeverUnderline, or the application would refer to Internet Options
dialog box of Control Panel.
A link is typically meant to help the user switch to a
different page or document when clicked. In some cases and at a particular
time, you may not want the user to be able to click a link. In this case you
can disable it. If you decide to disable a link, you can change its color
using the DisabledLinkColor property. On the other hand, if the user
is not able to access a link, that is, if a link is disabled, you can find
out the color of that link from the DisabledLinkColor property.
Here is an example:
private void linkLabel1_LinkClicked(object sender,
LinkLabelLinkClickedEventArgs e)
{
System.Diagnostics.Process.Start(
@"C:\Program Files\Internet Explorer\iexplore.exe",
"http://www.functionx.com/vccli");
}
|
Link Labels and Data Selection
|
|
As you may know already, the class that manages the link
label control is directly derived from Label. This means
that the binding functionality is primarily the same as for the label.
Furthermore, if you want the link label to apply its defined behavior, you
should (must) specify what it should do when it gets clicked. Here are
examples:
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Data.SqlClient;
public class Exercise : System.Windows.Forms.Form
{
Label lblEmployeeName;
LinkLabel lblEmailAddress;
LinkLabel lblRésumé;
WebBrowser wbRésumé;
Button btnBinder;
Button btnCreateDatabase;
public Exercise()
{
InitializeComponent();
}
void InitializeComponent()
{
btnCreateDatabase = new Button();
btnCreateDatabase.Text = "Create Database";
btnCreateDatabase.Location = new Point(12, 12);
btnCreateDatabase.Width = 120;
btnCreateDatabase.Click += new EventHandler(btnCreateDatabaseClick);
btnBinder = new Button();
btnBinder.Text = "Bind";
btnBinder.Location = new Point(140, 12);
btnBinder.Click += new EventHandler(btnBinderClick);
lblEmployeeName = new Label();
lblEmployeeName.AutoSize = true;
lblEmployeeName.Text = "Customer Name";
lblEmployeeName.Location = new Point(12, 44);
lblEmailAddress = new LinkLabel();
lblEmailAddress.Text = "Email Address";
lblEmailAddress.AutoSize = true;
lblEmailAddress.Location = new Point(12, 70);
lblRésumé = new LinkLabel();
lblRésumé.Text = "Résumé";
lblRésumé.AutoSize = true;
lblRésumé.Location = new Point(12, 96);
lblRésumé.LinkClicked += new LinkLabelLinkClickedEventHandler(lblRésuméClick);
wbRésumé = new WebBrowser();
wbRésumé.Location = new Point(12, 122);
Text = "Database Exercise";
Controls.Add(lblEmployeeName);
Controls.Add(lblEmailAddress);
Controls.Add(lblRésumé);
Controls.Add(btnBinder);
Controls.Add(btnCreateDatabase);
Controls.Add(wbRésumé);
StartPosition = FormStartPosition.CenterScreen;
Size = new System.Drawing.Size(280, 400);
}
void btnCreateDatabaseClick(object sender, EventArgs e)
{
using (SqlConnection cntExercise =
new SqlConnection("Data Source=(local); " +
"Integrated Security='SSPI';"))
{
SqlCommand cmdExercise =
new SqlCommand("IF EXISTS (SELECT name " +
"FROM sys.databases WHERE name = N'Exercsie1' " +
") " +
"DROP DATABASE Exercsie1; " +
"CREATE DATABASE Exercsie1", cntExercise);
cntExercise.Open();
cmdExercise.ExecuteNonQuery();
}
using (SqlConnection cntExercise =
new SqlConnection("Data Source=(local); " +
"Database='Exercsie1'; " +
"Integrated Security='SSPI';"))
{
SqlCommand cmdExercise =
new SqlCommand("CREATE TABLE Employees(AccountNumber nvarchar(8), " +
"FirstName nvarchar(24), LastName nvarchar(24), " +
"EmployeeName AS LastName + N', ' + FirstName," +
"EmailAddress nvarchar(50), Résumé nvarchar(50));", cntExercise);
cntExercise.Open();
cmdExercise.ExecuteNonQuery();
}
using (SqlConnection cntExercise =
new SqlConnection("Data Source=(local); Database='Exercsie1'; " +
"Integrated Security='SSPI';"))
{
SqlCommand cmdExercise =
new SqlCommand("INSERT INTO Employees " +
"VALUES(N'927049', 'Aaron', 'Swanson', 'aswanson@lambdasquare.net', 'http://www.functionx.com')," +
" (N'804070', 'Justine', 'Aronson', 'jaronson@lambdasquare.net', 'http://www.functionx.com')," +
" (N'284825', 'Paul', 'DaCosta', 'pdacosta@lambdasquare.net', 'http://www.functionx.com')," +
" (N'380408', 'Desmond', 'Perez', 'dperez@lambdasquare.net', 'http://www.functionx.com');",
cntExercise);
cntExercise.Open();
cmdExercise.ExecuteNonQuery();
}
}
private void btnBinderClick(object sender, EventArgs e)
{
using (SqlConnection cntExercise =
new SqlConnection("Data Source=(local);" +
"Database='Exercsie1';" +
"Integrated Security=SSPI;"))
{
SqlCommand cmdExercise = new SqlCommand("SELECT ALL * FROM Employees;",
cntExercise);
SqlDataAdapter sdaExercise = new SqlDataAdapter(cmdExercise);
DataSet dsEmployees = new DataSet("EmployeesSet");
cntExercise.Open();
sdaExercise.Fill(dsEmployees);
lblEmployeeName.DataBindings.Add(new Binding("Text", dsEmployees.Tables[0], "EmployeeName"));
lblEmailAddress.DataBindings.Add(new Binding("Text", dsEmployees.Tables[0], "EmailAddress"));
lblRésumé.DataBindings.Add(new Binding("Text", dsEmployees.Tables[0], "Résumé"));
}
}
private void lblRésuméClick(object sender, LinkLabelLinkClickedEventArgs e)
{
using (SqlConnection cntExercise =
new SqlConnection("Data Source=(local);" +
"Database='Exercsie1';" +
"Integrated Security=SSPI;"))
{
SqlCommand cmdExercise = new SqlCommand("SELECT ALL * FROM Employees;",
cntExercise);
cntExercise.Open();
SqlDataReader rdrExercise = cmdExercise.ExecuteReader();
while (rdrExercise.Read())
wbRésumé.Navigate(rdrExercise[3].ToString());
}
}
}
public class Program
{
[STAThread]
static int Main()
{
System.Windows.Forms.Application.Run(new Exercise());
return 0;
}
}
|