Solved

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

Posted on 2010-11-15
18
1,029 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
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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

867 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

16 Experts available now in Live!

Get 1:1 Help Now