Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Text areas and hyperlinks?

I have two quick, easy questions on C# in Visual Studio. First, is there anything like the label, but for an area of text in the program? I would like to have multiple lines of text in my program, but can only seem to accomplish it with a DotNetBar label with wordwrap turned on.

Second, is there any way to have a hyperlink in the middle of the text without using a link label? If I wanted to generate text like "An update is available, please visit http://example.com to download it!", is it possible to make the link clickable without having to position a link label in the middle of the text?

like image 797
mowwwalker Avatar asked Jan 01 '12 04:01

mowwwalker


1 Answers

You can use a LinkLabel and set its LinkArea property:

 //LinkArea (start index, length)
 myLinkLabel.LinkArea = new LinkArea(37, 18);
 myLinkLabel.Text = "An update is available, please visit http://example.com to download it!";

The above will make the http://example.com a link whilst the rest of the text in normal.

Edit to answer comment: There are various ways of handling the link. One way is to give the link a description (the URL) and then launch the URL using Process.Start.

myLinkLabel.LinkArea = new System.Windows.Forms.LinkArea(37, 18);
myLinkLabel.LinkClicked += new LinkLabelLinkClickedEventHandler(myLinkLabel_LinkClicked);
myLinkLabel.Text = "An update is available, please visit http://example.com to download it!";       
myLinkLabel.Links[0].Description = "http://example.com";

And the event handler can read the description and launch the site:

void myLinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
    Process.Start(e.Link.Description);
}
like image 50
keyboardP Avatar answered Oct 04 '22 16:10

keyboardP