Launching Default Browser

ok here is my situation

i have a richtextbox with a url in it

i want it to be able to left click it once, it opens the default browser (whether it be ie, firefox, etc.), and goes to the url the user clicked.

i have searched the forums and nothing i saw could help me or was the same situation im in.

any ideas




Answer this question

Launching Default Browser

  • Skynet_Developer

    wow now im getting:

    "PInvokeStackImbalance was detected
    Message: A call to PInvoke function 'Project1!Project1.Form1::ShellExecute' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.
    "



  • Kenneth Kwok

    Alex i get the following error whenever i click on the link while i'm debugging:

    "Unable to find an entry point named 'ShellExecute' in DLL 'shell32.dll'."

    i even declared it in my main form and in a seperate module (one or the other, i didnt do both at the same time)

    and i get that error. any idea why

    it highlights:

    "A = shellexecute(0, "open", RichTextBox1.Text, 0, 0, 1)"



  • Alexander Ivaniukovich - MSFT

    My bad - I gave you the wrong definition, that declare would be for VB6 - I've included a corrected version below.

    In any case, this link has instructions to get a link to be recognized in a rich text box. (You need to set a property on the rtf box to detect links, and handle the LinkClicked event of the box to launch it.). You can use the declare statement to launch the default browser with the actual link.

    http://msdn2.microsoft.com/en-us/library/f591a55w.aspx

    Private Declare Function ShellExecute _

    Lib "shell32.dll" _

    Alias "ShellExecuteA" ( _

    ByVal hwnd As Integer, _

    ByVal lpOperation As String, _

    ByVal lpFile As String, _

    ByVal lpParameters As String, _

    ByVal lpDirectory As String, _

    ByVal nShowCmd As Integer) _

    As Long

    Private Sub RichTextBox1_LinkClicked(ByVal sender As Object, ByVal e As System.Windows.Forms.LinkClickedEventArgs) Handles RichTextBox1.LinkClicked

    Dim r As Long

    r = ShellExecute(0, "open", e.LinkText, 0, 0, 1)

    End Sub



  • David McIntosh

    Try this:

    (Assuming you have a richtextbox named richtextbox1 with an url on it, add this code to your form)

    Private Declare Function ShellExecute _
    Lib "shell32.dll" _
    Alias "ShellExecuteA" ( _
    ByVal hwnd As Long, _
    ByVal lpOperation As String, _
    ByVal lpFile As String, _
    ByVal lpParameters As String, _
    ByVal lpDirectory As String, _
    ByVal nShowCmd As Long) _
    As Long

    Private Sub RichTextBox1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RichTextBox1.MouseClick

    Dim r As Long

    r = ShellExecute(0, "open", RichTextBox1.Text, 0, 0, 1)

    End Sub

    See this link for more info on the ShellExecute windows api:

    http://support.microsoft.com/default.aspx scid=kb;en-us;224816

    And

    http://msdn.microsoft.com/library/default.asp url=/library/en-us/shellcc/platform/shell/reference/functions/shellexecute.asp

    Let me know if that works.



  • Balamgn

    In VB.Net the richtextbox has a LinkClicked event inside you can use the Process.Start(e.LinkText)
  • JamieB

    also on another note, it launches the browser when i click ANYWHERE on the richtextbox. i only want it to launch it when you click on a url, and launch it to that specific url.

    so if the richtextbox reads http://www.google.com/, you click on only the url, it launches it to that website and maybe 2 lines down another url might be there (http://www.microsoft.com/), you click on that only and it launches to the website according so.



  • FEW62

    dim s as string = "http://www.microsoft.com"

    Process.Start(s)

    This should start up the default browser on the machine with the given URL.

    Obviously you would populate this from the textbox.text property


  • Launching Default Browser