Solved

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

Posted on 2010-11-15
18
1,013 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
  • 8
  • 4
  • 4
  • +1
18 Comments
 
LVL 3

Expert Comment

by:danora
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:GroganJ
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
I thought this was a Web application, seeing as you tagged it in the ASP.Net zone. Is it actually a WinForms project?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 3

Expert Comment

by:danora
Comment Utility
Hi GroganJ

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

Author Comment

by:GroganJ
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Coolleomod - thank you.
0
 
LVL 3

Expert Comment

by:sufianmehmood
Comment Utility
try using RichTextBox
0
 
LVL 52

Expert Comment

by:Carl Tawn
Comment Utility
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
Comment Utility
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
Comment Utility
It worked!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now