kobra
kobra

Reputation: 4963

Unhandled exception when Linklabel is clicked C# Winform

I am getting a strange unhandled exception when I click the linklabel which should open a form. I have tried to put the code in linklabel_click event handler in try-catch block, but I still get the error below.

See the end of this message for details on invoking just-in-time (JIT) debugging instead of this dialog box.
************** Exception Text ************** System.ComponentModel.Win32Exception: The system cannot find the file specified at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo) at System.Diagnostics.Process.Start()
at System.Diagnostics.Process.Start(ProcessStartInfo startInfo) at System.Diagnostics.Process.Start(String fileName) at InfoCapsule.FrmLink.llblHelp_LinkClicked(Object sender, LinkLabelLinkClickedEventArgs e) at System.Windows.Forms.LinkLabel.OnLinkClicked(LinkLabelLinkClickedEventArgs e) at System.Windows.Forms.LinkLabel.OnMouseUp(MouseEventArgs e) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, 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) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

The code for linklabel_click is as under.

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;
            }

Can you please help me in debugging.

Upvotes: 2

Views: 2436

Answers (2)

shahkalpesh
shahkalpesh

Reputation: 33476

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

EDIT: Linklabel is meant to act like a hyperlink. It should not be used to open a form inside the application

EDIT2: What is the target for the link? Try setting it to an appropriate URL & see what happens. If it is a proper URL, it should open the form alongwith the URL, I guess.

EDIT3: Put this inside the main method of a console app & see what happens.

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

I think, you should put the path correctly to make sure that the exception doesn't occur.
As I said before, what is the link's target?

EDIT4: I am sorry for the confusion. MusiGenesis is right. It is a plain link, which cannot execute on its own. Find inside your code for Process.Start method call.

I will suggest re-building the project. Did you have/had code before that made a call to Process.Start?

On a side note, see if you have more than 1 event handlers registered to handle the click.

Upvotes: 1

MusiGenesis
MusiGenesis

Reputation: 75296

I just tested this with a WebBrowser control, and you can navigate to a local file without bothering with the Uri class at all. This code should work for you:

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

Uri's are kind of quirky sometimes, although I've never seen them throw an uncatchable exception before (although it might be the WebBrowser throwing the exception - I dunno).

Make sure when you run this code that "help.html" is actually in the application's startup folder, or the WebBrowser will display a "this page cannot be displayed ..." message. If you're running your application from Visual Studio, the Application.StartupPath will be in your project's folder, in the "\bin\Debug\" or the "\bin\Release\" sub-folder (depending on whether you're running it in Debug or Release mode).

Upvotes: 2

Related Questions