locked
getting link in richtextbox to work RRS feed

  • Question

  • User693597704 posted

    I am working on a project that requires me to open a webpage when the user clicks a link in the richtextbox. I have tried multiple iterations and can not get  it to work.... The basically if I have the DetectURLs property is set to true..... the textbox is databound, but it does not work even when I make it not databound and enter the web address... I set a breakpoint inside the code right after the program shoul be entering and the breakpoint does not even trigger.

    I am using windows forms with MVS here is the code

     private void WebsiteText_TextChanged(object sender, EventArgs e)
            {
    
            }
           
    
     private void WebsiteText_LinkClicked(object sender, System.Windows.Forms.LinkClickedEventArgs e)
            {
                System.Diagnostics.Process.Start(e.LinkText);
                //open link with default application
            }

    Thursday, August 31, 2017 4:10 PM

Answers

  • User-1838255255 posted

    Hi steve03602,

    According to your description and code, I make a sample in my side, add the linklabel in the richtextbox, it works fine, please check:

    Sample Code:

     public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
                linkLabel2.Text = "Google";
            }
            public System.Diagnostics.Process p = new System.Diagnostics.Process();
    
            private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
            {
                // Call Process.Start method to open a browser  
                // with link text as URL.  
                p = System.Diagnostics.Process.Start("IExplore.exe", "http://www.google.com");
            }
        }

    Here is a tutorial about realize this, please check:

    How to: Display Web-Style Links with the Windows Forms RichTextBox Control:

    https://docs.microsoft.com/en-us/dotnet/framework/winforms/controls/how-to-display-web-style-links-with-the-windows-forms-richtextbox-control 

    Also I think this issue is related to the winform, so I think you could get a suitable help in this forum, please check:

    windows form forum:

    https://social.msdn.microsoft.com/Forums/windows/en-US/home?forum=winforms 

    Best Regards,

    Eric Du

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 1, 2017 3:30 AM

All replies

  • User-1838255255 posted

    Hi steve03602,

    According to your description and code, I make a sample in my side, add the linklabel in the richtextbox, it works fine, please check:

    Sample Code:

     public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
                linkLabel2.Text = "Google";
            }
            public System.Diagnostics.Process p = new System.Diagnostics.Process();
    
            private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
            {
                // Call Process.Start method to open a browser  
                // with link text as URL.  
                p = System.Diagnostics.Process.Start("IExplore.exe", "http://www.google.com");
            }
        }

    Here is a tutorial about realize this, please check:

    How to: Display Web-Style Links with the Windows Forms RichTextBox Control:

    https://docs.microsoft.com/en-us/dotnet/framework/winforms/controls/how-to-display-web-style-links-with-the-windows-forms-richtextbox-control 

    Also I think this issue is related to the winform, so I think you could get a suitable help in this forum, please check:

    windows form forum:

    https://social.msdn.microsoft.com/Forums/windows/en-US/home?forum=winforms 

    Best Regards,

    Eric Du

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 1, 2017 3:30 AM
  • User693597704 posted

    Bingo that did it I forgot to create new global instance of the process object... I also got it opening the default browser instead of forcing internet exploder this way

    public System.Diagnostics.Process p = new System.Diagnostics.Process();
            private void linkLabel1_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
            {
                 p = System.Diagnostics.Process.Start(linkLabel1.Text);
             }

    Friday, September 1, 2017 4:07 AM