Why is there a NullReferenceException error when the ToolStrip - openFileDialog.showDialog () button is pressed twice?

I created a clean WindowsFormsApplication solution, added it ToolStripto the main form, and placed one button on it. I added also OpenFileDialog, so the event Clickfor ToolStripButtonlooks like this:

private void toolStripButton1_Click(object sender, EventArgs e)  
{  
    openFileDialog1.ShowDialog();  
}

I have not changed any other properties or events.

The funny thing is that when I double-click on the button ToolStripButton(the second click should be quite quick before opening the dialog), cancel both dialogs (or select a file, it does not matter) and then click in the client area of ​​the main form, the NullReferenceExceptionapplication will fail (error data is attached at the end of the message). Note that the event is Clickimplemented, but DoubleClicknot.

What is even more strange is that when replaced OpenFileDialogwith any custom form, blocks ToolStripButtoncan be double clicked.

I am using VS2008 with .NET3.5 on Windows 7 Professional (from MSDNAA) with the latest updates. I have not changed many parameters in VS (only fontsize, workspace folder and line numbering).

- , ? 100% , ?

, , OpenFileDialog.ShowDialog(), ( ). ?

:

System.NullReference
     Message = " ​​ ."
      = "System.Windows.Forms"
     StackTrace:
           System.Windows.Forms.NativeWindow.WindowClass.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
           System.Windows.Forms.UnsafeNativeMethods.PeekMessage(MSG & msg, HandleRef hwnd, Int32 msgMin, Int32 msgMax, Int32 remove)
           System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32, Int32 pvLoopData)
           System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner( Int32, ApplicationContext)
           System.Windows.Forms.Application.ThreadContext.RunMessageLoop( Int32, ApplicationContext)
           System.Windows.Forms.Application.Run(Form mainForm)
           WindowsFormsApplication1.Program.Main() w C:\Users\Marchewek\Desktop\Workspaces\VisualStudio\WindowsFormsApplication1\Program.cs: 20
           System.AppDomain._nExecuteAssembly ( , String [] args)
           System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String [] args)
           Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           System.Threading.ThreadHelper.ThreadStart_Context ( )
           System.Threading.ExecutionContext.Run( , ContextCallback, )
           System.Threading.ThreadHelper.ThreadStart()
 InnerException:

+3
3

- Windows 7 - , . , , win 7, .

XP. , toolStripButton . , , .

, - , , - . - .

, - DoubleClickEnabled toolStripButton. - , .

:

    public Form1()
    {
        InitializeComponent();

        // This is a workaround for a framework bug
        // see blah blah
        toolStripButton1.DoubleClickEnabled = true; 

    }

.

Neil

+2

ShowDialog . ShowDialog OpenFileDialog UI , .

, ShowDialog, , OpenFileDialog. .

, , , . Click ; OpenFileDialog Click. , , :

private void toolStripButton1_Click(object sender, EventArgs e)   
{
    using(var OFD = new OpenFileDialog())
    {
        OFD.ShowDialog();  
    } 
} 
0

Screen resolution may be one of the causes of an error in the structure. In my case, I changed my screen resolution and the problem was not produced in this resolution, and when I return to the recommended resolution, I had a problem.

0
source

Source: https://habr.com/ru/post/1783049/


All Articles