Select to view content in your preferred language

arcmap add-in open csv open file dialog crash when click "Cancel" or close the dialog

2589
5
Jump to solution
09-21-2014 09:19 PM
LiYao
by
Deactivated User

Hello everyone,

I am using vb.net arcmap 10.2.2 add-in.

I developed a function to allow user open a csv file from local and other functions on the opened csv file. But when I click "Cancel" on the dialog or just close the dialog, the arcmap just crash.

I want to check what cause the issue?

The code isas follows:

  Dim openFileDialog1 As New OpenFileDialog
  Dim fileName As String
  openFileDialog1.Filter = "Comma delimited|*.csv"
  openFileDialog1.Title = "Open a csv File"
  openFileDialog1.ShowDialog()
  fileName = openFileDialog1.FileName

Can please tell me the error is caused by my code or arcmap issue? Thanks a lot.

0 Kudos
1 Solution

Accepted Solutions
FrankHellwich1
Occasional Contributor

hm, difficult to diagnose by this small snippet. I guess the second call is caused by the code we do not see here. Maybe it's an (mouse-)event-handler that is triggered accidentally or something like that. You should be able to diagnose this by:

1. add an else-statement to handle cancel and close

2. place a breakpoint here

3. run your plugin in debug-mode

4. use your plugin and cancel the file selection -> you should reach your breapoint

5. check the call-stack - this should be the normal program flow

6. continue execution

7. maybe the debugger stops again - now check the callstack: who calls your function

If you can not reproduce your error this way this could be caused by the first debugger break. Try something like this (sorry - c#):

private bool _alreadyCanceled = false;

private void button1_Click(object sender, EventArgs e)

{

    var ofd = new OpenFileDialog();

    ofd.Filter = @"Comma delimited|*.csv";

    ofd.Title = @"Open a Test";

    if(ofd.ShowDialog() == DialogResult.OK)

    {

        var test = ofd.FileName;

        Debug.WriteLine(string.Format("Filename: {0}",test));

    }

    else

    {

        if(_alreadyCanceled)

        {

            int i = 1; //place your breakpoint here - who is calling ?

        }

        _alreadyCanceled = true;

    }

}

View solution in original post

0 Kudos
5 Replies
FrankHellwich1
Occasional Contributor

Hi Li,

"normally" this code should work under an WinForms-environment. But: you'd better check the result of the function ShowDialog - you should only work with the value of FileName if ShowDialog returns DialogResult.OK.

Frank

0 Kudos
LiYao
by
Deactivated User

Hi Frank,

Thanks a lot for the quick reply.

I use the following to check the result of the function ShowDialog:

Dim result As DialogResult = openFileDialog1.ShowDialog()

If result = Windows.Forms.DialogResult.OK

But I have no idea what to do after that. One strange thing is for the cancel and close the openFileDialog1 will present once again, on second time cancel or close the openFileDialog1 will crash.

0 Kudos
FrankHellwich1
Occasional Contributor

hm, difficult to diagnose by this small snippet. I guess the second call is caused by the code we do not see here. Maybe it's an (mouse-)event-handler that is triggered accidentally or something like that. You should be able to diagnose this by:

1. add an else-statement to handle cancel and close

2. place a breakpoint here

3. run your plugin in debug-mode

4. use your plugin and cancel the file selection -> you should reach your breapoint

5. check the call-stack - this should be the normal program flow

6. continue execution

7. maybe the debugger stops again - now check the callstack: who calls your function

If you can not reproduce your error this way this could be caused by the first debugger break. Try something like this (sorry - c#):

private bool _alreadyCanceled = false;

private void button1_Click(object sender, EventArgs e)

{

    var ofd = new OpenFileDialog();

    ofd.Filter = @"Comma delimited|*.csv";

    ofd.Title = @"Open a Test";

    if(ofd.ShowDialog() == DialogResult.OK)

    {

        var test = ofd.FileName;

        Debug.WriteLine(string.Format("Filename: {0}",test));

    }

    else

    {

        if(_alreadyCanceled)

        {

            int i = 1; //place your breakpoint here - who is calling ?

        }

        _alreadyCanceled = true;

    }

}

0 Kudos
LiYao
by
Deactivated User

Hi Frank,

Thanks a lot for the detailed answer. I will convert the code to vb.net.

0 Kudos
LiYao
by
Deactivated User

Hi Frank,


By using debugger, I got the following error in call stack window:

A first chance exception of type 'System.ArgumentNullException' occurred in Microsoft.VisualBasic.dll

I have modified my code and fixed the issue.

Thank you very much for the kind help.

0 Kudos