日期:2014-05-17  浏览次数:21073 次

C#在不同线程中如何设置一个窗体是另一个窗体的父窗体
主窗体form1是Run()出来的,另一个窗体form2是在新线程中使用showdialog方法显示出来的,现在需要把form2的父窗体设置为form1,改如何实现?

------解决方案--------------------
你怎么能在非工作线程showdialog呢?
------解决方案--------------------
请回到主线程创建并调用ShowDialog,也就是用Invoke或者BeginInvoke进行所有UI相关操作
------解决方案--------------------
不可以了吧。为什么要这样
------解决方案--------------------

Thread th = new Thread(new ThreadStart(ExportCost));
            th.SetApartmentState(ApartmentState.STA);
            th.Start();


private void ExportCost()
        {
            SetExportBusinessNum();
            if (!string.IsNullOrEmpty(ExportBusinessNum))
            {
                Query_Customs_CustomsDeclaration query_Customs_CustomsDeclaration = GetQuery_Customs_CustomsDeclaration();
                query_Customs_CustomsDeclaration.BusinessNum = ExportBusinessNum;

                DataTable dt = Customs_CustomsDeclarationManager.QDZYExportCostExcel(query_Customs_CustomsDeclaration);
                if (dt != null && dt.Rows.Count > 0)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "excel文件(*.xls)
------解决方案--------------------
*.xls";
                    sfd.FileName = "财务数据";
                    DataTable dtCopy = dt.Copy();
                    if (dt.Columns.Contains("是否审核")) dt.Columns.Remove("是否审核");
                    if (dt.Columns.Contains("是否导出")) dt.Columns.Remove("是否导出");
                    if (dt.Columns.Contains("Id")) dt.Columns.Remove("Id");