Text areas and hyperlinks?

萝らか妹 提交于 2019-12-14 03:47:44

问题


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?


回答1:


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);
}



回答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);
};



回答3:


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




回答4:


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/



来源:https://stackoverflow.com/questions/8691783/text-areas-and-hyperlinks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!