Solved

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

Posted on 2010-11-15
18
1,055 Views
Last Modified: 2012-05-10
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.
0
Comment
Question by:GroganJ
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 4
  • 4
  • +1
18 Comments
 
LVL 3

Expert Comment

by:danora
ID: 34138008
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
 

Author Comment

by:GroganJ
ID: 34138084
Hi danora,

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

John.
0
 
LVL 3

Expert Comment

by:danora
ID: 34138113
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:GroganJ
ID: 34138161
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
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34138206
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
 
LVL 3

Expert Comment

by:danora
ID: 34138223
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
 

Author Comment

by:GroganJ
ID: 34138364
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
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34138446
I thought this was a Web application, seeing as you tagged it in the ASP.Net zone. Is it actually a WinForms project?
0
 
LVL 3

Expert Comment

by:danora
ID: 34138460
Hi GroganJ

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

Author Comment

by:GroganJ
ID: 34138498
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
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34138571
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
 

Author Comment

by:GroganJ
ID: 34142842
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
 

Author Comment

by:GroganJ
ID: 34142845
Coolleomod - thank you.
0
 
LVL 3

Expert Comment

by:sufianmehmood
ID: 34142886
try using RichTextBox
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34143030
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
 

Accepted Solution

by:
GroganJ earned 0 total points
ID: 34143953
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
 

Author Closing Comment

by:GroganJ
ID: 34740379
It worked!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Problem to copy file 14 91
parse string in c# 5 41
C#: How do I test for null objects and a dub a value? 2 37
How do you think this website does searches? 5 34
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question