5

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?

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
mowwwalker
  • 16,634
  • 25
  • 104
  • 157

4 Answers4

6

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);
}
keyboardP
  • 68,824
  • 13
  • 156
  • 205
  • Thanks, the link isn't working though when I click it. Also, could you update the answer to include that I can, as kishorejangid said, set the autoresize property to false to make it multi-lined. – mowwwalker Jan 01 '12 at 04:41
2

You may try RichTextBox control.

string text = "This is the extract of text located at http://www.google.com and http://www.yahoo.com";
richTextBox1.Text   = text;

richTextBox1.ReadOnly = true;
richTextBox1.LinkClicked += (sa, ea) =>
{
 System.Diagnostics.Process.Start(ea.LinkText);
};
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
1

You can use the normal label and make the AutoSize property as false. And then adjust your width and height it will wrap by it self

Kishore Kumar
  • 12,675
  • 27
  • 97
  • 154
0

I assume you are using doing a windows application, and not a web application.

In C# you can create a normal textbox by dragging and dropping it onto your form, change its property to multi-line, and make it read only. Thats what I always do.

As for adding a link to the text without a linklabel. There is a way to add links to textboxes. You can check out a pretty good tutorial at http://www.codeproject.com/KB/miscctrl/LinkTextBox.aspx/

Johnrad
  • 2,637
  • 18
  • 58
  • 98