0

I am appending the string failing_client to a RichTextBox control named report_output_box in a WinForms app using C#.

Is there a to make that string clickable inside the RichTextBox control, then pass a string to the onclick method?

What are my options here?

Update: I need to be more specific. I do not want to open a URL on the OnClick action, but instead, call a function.

Sample Code:

//... code before this
//failing_client_list is list of servers
foreach (string failing_client in failing_client_list)
{

      //link onclick will call  a method w/ a string argument

       report_output_box.LinkClicked += new  
                             LinkClickedEventHandler(open_inet_window(failing_client));
       report_output_box.AppendText(failing_client + "\n");


}

    //code after...


//method it would call
private void open_inet_window(object sender, EventArgs e, string failing_client)
{
      //create a window object (w/ window consturctor) and then open it
      inet_clients_window inizzy_window = new inet_clients_window(failing_client);
      inizzy_window.Show();
}

Thanks!

sirbradwick
  • 61
  • 1
  • 10
  • Why can't you check to see if the RTB has data in it then pass that string to the onclick? – Brian Jan 02 '14 at 20:51
  • You could add a hyperlink as in here:[richtextbox-control-making-non-urls-hyperlinks][1]. [1]: http://stackoverflow.com/questions/1526857/richtextbox-control-making-non-urls-hyperlinks – NoChance Jan 02 '14 at 20:55

1 Answers1

0

Please take a look at: http://www.codeproject.com/Articles/9196/Links-with-arbitrary-text-in-a-RichTextBox

The article describes a way to create custom links in a RichTextBox that do not start with http:// or ftp://.

Generally however, if you want to add links that do start with http://, then all you need to do is make sure that the property DetectUrls of the RichTextBox is set to true, then you must add a handler for the LinkClicked event.

Doing a little search on StackOverFlow, you will find a very good source: How can I make a hyperlink work in a RichTextBox?

Here is a quick example I made, notice that throug e.LinkText you can create a Switch() statement for example to then decide on what to do depending on the link URL value?

namespace WindowsFormsApplication11
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            richTextBox1.LinkClicked += new LinkClickedEventHandler(richTextBox1_LinkClicked);
        }

        void richTextBox1_LinkClicked(object sender, LinkClickedEventArgs e)
        {
            MessageBox.Show("You clicked the link: " + e.LinkText);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            richTextBox1.AppendText("This is link1: http://www.google.com");
            richTextBox1.AppendText("\nThis is link2: http://www.stackoverflow.com");
        }      
    }
}
Community
  • 1
  • 1
lucidgold
  • 4,432
  • 5
  • 31
  • 51
  • lucidgold, is there a way to pass a string to richTextBox1_LinkClicked ? – sirbradwick Jan 02 '14 at 21:10
  • @BradCanterbury:if you need to append some custom text and expect RichTextBox LinkClicked to understand what the "URL" or the value of the custom text is then you must create your own "Richer" TextBox control. When you append the value of the string failing_client how can you expect the RichTextBox (RTB) to parse the qualified value? Can you give me an example of what failing_client looks like? – lucidgold Jan 02 '14 at 21:31
  • Apologies for never replying. The failing client object would just be a string with a server name as its value. @lucidgold – sirbradwick Apr 16 '17 at 21:36