How do I add a hyperlink to a textbox in C#?

Hi All,

I have a textbox on a form and I want to add some text with a hyperlink. This is a C# application. If I get the text and the address for the hyperlink from a database, how can I format this for pasting to the textbox?

Thanks,
John.
GroganJAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
GroganJConnect With a Mentor Author Commented:
OK, I finally got it solved.

On the windows Form, I used a Webrowser to display the text and html link. The key to getting it to display is to use the webBrowser.Refresh() method once I've written to the webrowser, then an Application.DoEvents() to finish it off. It now displays correctly in the webrowser.

Thanks to everyone for their thoughts and input.
John.
0
 
danoraCommented:
Hi GroganJ

Found some info for you hope this helps

In Label.Text or TextBox.Text you should write the link in html code like the following:

<A href="chapter2.html">TEST LINK</A> This is a test

will result in:
TEST LINK (<<being the hyperlink) This is a test
0
 
GroganJAuthor Commented:
Hi danora,

How can I make the text 'This is a test' be the hyperlink rather than have it appear first?

John.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
danoraCommented:
Hi John,

Unfortunately i am no expert and am admittedly taking a stab in the dark, don't know if anyone else can shed some light on this? This is what i would try though :)

TEST LINK <A href="chapter2.html">This is a test</A>

Dan
0
 
GroganJAuthor Commented:
Hi danora,

This is what gets printed in the textbox ...

<A href='chap2.html'>http://www.insidehousing.co.uk/ihstory.aspx?storycode=6512431</A>Scots urged not to slash housing budget

John.
0
 
Carl TawnSystems and Integration DeveloperCommented:
You can't put an active hyperlink into a textbox. Textboxes only display textual, non-active, content.

You care either going to have to display it as an actual hyperlink, or style your link so it looks like a textbox (although that is liable to confuse users).
0
 
danoraCommented:
Hi John,

As carl tawn has just said i have found this info out aswell, please see below for some possible solutions

A Textbox does not have hyperlinks/Hyperlinking. This is not possible outside word documents anyway (even the rich text box will not jump you to an external control..).

3 different ways you can do this..
1. Redesign your form.. In many years of programming I have never seen any justification for more than 1 page of controls. Reasons not to have pages of controls include slow performance, screen flashing, memory hogging, crashes, poor maintainability etc.

Use a start page to restrict users to a subset of the controls, use a tab control, page control, menu item etc to load only the controls required.

2. Create your form as a dynamic HTML web page and embed it in your windows application.

3. If you really must have this and are familiar with graphics programming you can also:
Draw the strings on a panel object and trace the MouseDown event. When the user clicks on the panel then use the xy position to determine which item they clicked on. Use the MouseMove method to change the cursor to a hand when they are over valid text. Check ControlExit event and cancel hand cursor if it leaves the control.
0
 
GroganJAuthor Commented:
OK, I've changed the textbox to a web browser. I then use the following code:

string eMail;

sEmail = @"<html><head> Hello World </head><body>The body </body> </html>";
WebBrowser.DocumentText = sEmail;

But, it doesn't display anything in the webrowser.

Any ideas what I'm missing?
0
 
Carl TawnSystems and Integration DeveloperCommented:
I thought this was a Web application, seeing as you tagged it in the ASP.Net zone. Is it actually a WinForms project?
0
 
danoraCommented:
Hi GroganJ

I am afraid i am out of ideas, hope you find your answer from someone else.
0
 
GroganJAuthor Commented:
Carl,
I must have clicked that in error - I have asked the mods to remove the reference to ASP.NET.
This is a winforms application that I'm building in C#.
0
 
Carl TawnSystems and Integration DeveloperCommented:
Using a LinkLabel may be an option for you. You can set its Text property and it will display like a Hyperlink, you can then handle the click event in order to navigate to the associated url.

Sample here:

    http://msdn.microsoft.com/en-us/library/aa288420(VS.71).aspx
0
 
GroganJAuthor Commented:
Let me take a step back and explain what I'm trying to achieve...

Every morning, we send out an email with a number of news articles - the article title is a hyperlink to the article and there is a line or 2 with a summary underneath.

I'm trying to automate the generation of this email, but in a textbox/webrowser/whatever where I can just copy and paste into an email. Alternatively, can I fire up Outlook and generate an email body if that's easier than what I'm trying to achieve?

John.
0
 
GroganJAuthor Commented:
Coolleomod - thank you.
0
 
sufianmehmoodCommented:
try using RichTextBox
0
 
Carl TawnSystems and Integration DeveloperCommented:
Dumping the code straight into the emai will be easier, because that way you can just dump HTML straight into the output. There is no benefit in trying to write it to a WinForm first.
0
 
GroganJAuthor Commented:
It worked!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.