Unhandled exception when clicking Linklabel C # Winform

I am getting a weird unhandled exception when I click on a link that should open a form. I tried to put the code in my linklabel_click event handler in a try-catch block, but I still get below error.

See the end of this post for details on JIT the dialog box instead.
************** Exception Text ************** System.ComponentModel.Win32Exception: The system cannot find the file specified in System.Diagnostics.Process.StartWithShellExecuteEx (ProcessStartInfo startInfo) in System.Diagnostics.Process.Start ()
at System.Diagnostics.Process.Start (ProcessStartInfo startInfo) at System.Diagnostics.Process.Start (String fileName) at InfoCapsule.FrmLink.llblHelp_LinkClicked (Sender Object, LinkLabelLinkClickedEventArgs is System.Windows.FormgsLinkLabel.Args is Windows.Forms.LinkLabel.OnMouseUp (MouseEventArgs is System.Windows.Forms.Control.WmMouseUp (Message & m, MouseButtons, Int32 clicks) at System.Windows.Forms.Control.WndProc (Message & m) at System.Windows. Forms.Label.WndProc (Message & m) at System.Windows.Forms.LinkLabel.WndProc (Message & msg) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage (Message & m) at System.Windows.Forms.Control .ControlNativeWindow.WndProc (Message & m) when System.Windows.Forms.NativeWindow.Callback (IntPtr hWnd, Int32 msg,IntPtr wparam, IntPtr LPARAM)

Code for linklabel_click as below.

private void llblHelp_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
    try
    {
        refFrmHelp = new FrmHelp(this);
        refFrmHelp.Show();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}

      

Code inside FrmHelp

            String sitePath = null;
            try
            {
                sitePath = "file:///" + Application.StartupPath + "\\help.html";
                //sitePath = sitePath.Replace("\\", "/");
                MessageBox.Show(sitePath);
                Uri path = new Uri(sitePath);
                wbHelp.Navigate(path);
            }
            catch (UriFormatException ex)
            {
                MessageBox.Show(ex.ToString() + "\nSite Path: " + sitePath);
                return false;
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.ToString() + "\nSite Path: " + sitePath);
                return false;
            }

      

Could you help me debug.

+2


source to share


2 answers


I just tested this with a WebBrowser control and you can navigate to your local file without worrying about the Uri class. This code should work for you:

string sitePath = Application.StartupPath + @"\help.html";
wbHelp.Navigate(sitePath);

      



The Uri are sometimes dodgy, although I've never seen them throw an elusive exception before (though maybe WebBrowser is throwing an exception - I don't know).

Make sure that when you run this code that "help.html" is actually in the application launch folder, or WebBrowser will display the message "This page cannot be displayed ...". If you run the application from Visual Studio, Application.StartupPath will be located in the project folder in the "\ bin \ Debug \" or "\ bin \ Release \" folder (depending on whether you run it in debug or release mode).

+2


source


Looking at the exception it appears that you are providing a link to a local / network location, which is not a valid path.

EDIT: Linklabel is designed to work like a hyperlink. It cannot be used to open a form inside an application.

EDIT2: What is the purpose of the link? Try adjusting it to the appropriate url and see what happens. If it's the correct url it should open the form along with the url I guess. Strike>

EDIT3: Place this inside the main method of your console application and see what happens.

    try
    {
        Process.Start("c:\\calc.exe");
    }
    catch (Exception e)
    {
        Console.WriteLine("exception caught: " + e);
    }

      



I think you have to give the path correctly to make sure the exception doesn't exist.
As I said, what is the purpose of the link?

EDIT4: I am sorry for the confusion. MusiGenesis is right. This is a simple link that cannot execute on its own. Find inside the code to call the method Process.Start

.

I suggest rebuilding the project. Did you / did you have a code before calling Process.Start

?

Note that you have more than 1 event handler to handle the click .

+1


source







All Articles