C link label open new form
You want to know how to create a link label in C# that opens a new form.
Here's a simple example:
First, create a new form in your project. Let's call it Form2
.
Then, in your main form (Form1
), add a Label
control and set its Text
property to the text you want to display as a link. For example:
Label linkLabel = new Label();
linkLabel.Text = "Click here to open Form2";
linkLabel.AutoSize = true;
linkLabel.Location = new Point(10, 10);
linkLabel.LinkArea = new LinkArea(0, linkLabel.Text.Length);
linkLabel.LinkClicked += new LinkLabelLinkClickedEventHandler(linkLabel_LinkClicked);
this.Controls.Add(linkLabel);
In the code above, we create a new Label
control and set its Text
property to the text we want to display as a link. We also set the AutoSize
property to true
so that the label resizes to fit the text. We set the Location
property to a point on the form where we want the label to appear. We also set the LinkArea
property to a new LinkArea
object that specifies the range of characters in the label that should be treated as a link. Finally, we add the LinkClicked
event handler to the label.
Now, let's implement the LinkClicked
event handler:
private void linkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Form2 form2 = new Form2();
form2.Show();
}
In this event handler, we create a new instance of Form2
and call its Show
method to display the form.
That's it! Now, when you run your program and click on the label, it should open the new form.
Note that you can customize the appearance of the link label by using various properties, such as LinkColor
, VisitedLinkColor
, and LinkVisited
. You can also use the LinkLabel
control's AutoSize
property to control the size of the label.