简体   繁体   中英

Set Parent for the dialog box when the same dialog box can be opened from both WPF and WinForm application

I have an application which have 2 modules (one made from WinForms and other made of WPF). I have an Export Button on both of the modules.

Export button is used to Export the grid to Excel File and save on my local.

I have a common method (ExportToExcel()) for exporting grid to excel when Export button is clicked.

Now as i have said my application has both WPF and WinForms used, now when the user clicks on Export Button on 1st module(WinForms) the Export dialog box opens, Now if the user clicks on Export Button of 2nd Module(WPF) the issue was thrown Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it. This exception is only raised if a debugger is attached to the process. which i have corrected applying threading as shown in below code using thread.SetApartmentState(ApartmentState.STA).

Now the issue after using threading and setting ApartmentState to STA is that the parent has been removed from the Export dialog box, which should be the particular module(1st or 2nd) from where the Export button was clicked and dialog box was opened, right now the parent for the dialog box is windows not the application modules(1st or 2nd).

Another issue is that as the parent is not set for the Export Dialog box , if the user clicks on Export button multiple times multiple Export dialog box get opened, which is not correct, once the export button is clicked and the dialog box gets opened, user should not be able to click on export box again until and unless user completes operation on dialog box or closes it.

My requirement is, the parent for the dialog box should be the one module from where the dialog box is opened and user should not be able to perform any operation on the application until the dialog box operation is completed.

Code for Export button click

public void ExportToExcel(UltraGrid gridToExport, UltraGridExcelExporter ultraGridExcelExporter, string name)
{
    Thread thread = new Thread((ThreadStart)(() =>
    {
     // code to open dialog box and perform operation on it.
    }));
    thread.SetApartmentState(ApartmentState.STA);
    thread.Start();
}

I'm not sure what you said but try this:


   //You need to enable (IsMdiContainer)
   //Example, in your WindowsForms set:
   this.IsMdiContainer = true;

   WpfMenu formMenu = new WpfMenu();
   formMenu.MdiParent = this;
   formMenu.Show();

This makes "forms Menu", be a child of your Forms that is being executed, I believe it works for WPF too

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM