Freigeben über


How many times do I have to tell you, the right tool for the right job!

How many times have you used one tool for a job only to have the requirements change and you then realize the tool doesn't support the new requirements?  It happens very often!  Let's consider this requirement for a WinForms application:

REQUIREMENT: On any error, display the error message as richly formatted text and allow for URLs to take the user to additional assistance.

No problem, there is a RichTextBox control in .Net that will do this for you.  You make up all the text as RTF, load it at run-time as needed, follow the guidance on MSDN here for linking to the web and you have a nifty dialog that will satisfy the above requirement in a very short period of time.

You then receive some additional information about the requirement:

REQUIREMENT: On any error, display the error message as richly formatted text and allow for arbitrary text to act as a hyperlink to take the user to additional assistance.

No problem - you just modify your RFT to create the hyperlinks and you're all set.  Then you see the result:

Wait a minute.  That's not what's in my RTF file.  You specified this:

Unfortunately the RichTextBox control doesn't handle the anchor tag the way you can if you were authoring this as HTML.  So, what are you supposed to do?  There are some solutions on the web, for modifying the RichTextBox control, but you want to avoid licensing issues.  This is where I see Scotty from Star Trek saying, 'How many times do I have to tell you, the right tool for the right job!'.  If you need links to look like they would on a web page, then create a WebBrowser control to display the text instead of the RichTextBox control!  It couldn't be simpler.  Sway out the controls and to handle the user clicking on the link generically, you only need to write a couple of lines of code. 

In your form load method, add the event handler:

webBrowser1.Navigating += new WebBrowserNavigatingEventHandler(webBrowser1_Navigating);

And your event handler should have this code:

private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)

{

   // Override the navigation property and just bring up a web browser

     System.Diagnostics.Process.Start("IEXPLORE.EXE", e.Url.ToString());

   // Stop any navigation in our control

      e.Cancel = true;

}

Here is the final result.  Looks nice doesn't it?