Link to home
Create AccountLog in
Avatar of GroganJ
GroganJ

asked on

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.
Avatar of danora
danora

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
Avatar of GroganJ

ASKER

Hi danora,

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

John.
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
Avatar of GroganJ

ASKER

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.
Avatar of Carl Tawn
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).
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.
Avatar of GroganJ

ASKER

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?
I thought this was a Web application, seeing as you tagged it in the ASP.Net zone. Is it actually a WinForms project?
Hi GroganJ

I am afraid i am out of ideas, hope you find your answer from someone else.
Avatar of GroganJ

ASKER

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#.
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
Avatar of GroganJ

ASKER

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.
Avatar of GroganJ

ASKER

Coolleomod - thank you.
try using RichTextBox
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.
ASKER CERTIFIED SOLUTION
Avatar of GroganJ
GroganJ

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of GroganJ

ASKER

It worked!