Form.Show()没有显示它的子控件

我有一个表单frmPleaseWait ,它有一个MarqueeProgressBar和一个Label ,当UI在我们拥有的结构不良的应用程序中加载数据时,我想使用它。

问题是frmPleaseWait.Show()显示表单,但不显示其中的控件。 它只是一个白色矩形。 现在frmPleaseWait.ShowDialog()显示子控件但不允许UI加载它的数据。

我错过了什么? 以下是我尝试此操作的代码段。

  PleaseWait = new frmPleaseWait(); PleaseWait.Show(this); // Set all available HUD values in HUD Object HUD.LastName = GetCurrentRowVal("LastName").Trim(); HUD.FirstName = GetCurrentRowVal("FirstName").Trim(); HUD.PersonId = Convert.ToInt32(GetCurrentRowVal("PersonID").Trim()); HUD.SSn = GetCurrentRowVal("SSN").Trim(); HUD.MiddleName = GetCurrentRowVal("MiddleName").Trim(); HUD.MasterID = ConnectBLL.BLL.DriInterface.CheckForDriId(HUD.PersonId).ToString(); // This loads numerous UserControls with data shellForm.FormPaint(HUD.PersonId); PleaseWait.Close(); 

编辑

根据答案和我的尝试跟进。

这就是我所拥有的,但是我在pleaseWaitInstance.Location = parent.PointToScreen(Point.Empty);上得到了一个Cross-Thread Exception pleaseWaitInstance.Location = parent.PointToScreen(Point.Empty); 如果我删除该行,它将运行但它在我的屏幕的左上角运行并忽略应用程序的位置。

  public partial class frmPleaseWait : XtraForm { public frmPleaseWait() { InitializeComponent(); } private static frmPleaseWait pleaseWaitInstance; public static void Create(XtraForm parent) { var t = new System.Threading.Thread( () => { pleaseWaitInstance = new frmPleaseWait(); pleaseWaitInstance.FormClosed += (s, e) => pleaseWaitInstance = null; pleaseWaitInstance.StartPosition = FormStartPosition.Manual; pleaseWaitInstance.Location = parent.PointToScreen(Point.Empty); Application.Run(pleaseWaitInstance); }); t.SetApartmentState(System.Threading.ApartmentState.STA); t.IsBackground = true; t.Start(); } public static void Destroy() { if (pleaseWaitInstance != null) pleaseWaitInstance.Invoke(new Action(() => pleaseWaitInstance.Close())); } } 

由于shellForm不起作用,您的表单不起作用。 UI线程忙于加载和绘制控件,它不能同时绘制您的PleaseWait表单。 您需要创建一个单独的线程来抽取消息循环以保持您的PWforms存活。 你可以让它像这样工作:

 public partial class PleaseWait : Form { private static PleaseWait mInstance; public static void Create() { var t = new System.Threading.Thread(() => { mInstance = new PleaseWait(); mInstance.FormClosed += (s, e) => mInstance = null; Application.Run(mInstance); }); t.SetApartmentState(System.Threading.ApartmentState.STA); t.IsBackground = true; t.Start(); } public static void Destroy() { if (mInstance != null) mInstance.Invoke(new Action(() => mInstance.Close())); } private PleaseWait() { InitializeComponent(); } //etc... } 

样品用法:

  PleaseWait.Create(); try { System.Threading.Thread.Sleep(3000); } finally { PleaseWait.Destroy(); } 

我遇到了同样的问题,但这个解决方案并没有帮助我。

我的方法如下:在program.cs中我实例化了’PleaseWait’-Form并将其作为参数提供给主Form:

  pleaseWaitForm pleaseWait = new pleaseWaitForm(); Application.Run(new Form1(pleaseWait)); 

Form1的构造函数就像这样开始:

  public Form1(pleaseWaitForm pleaseWait) { InitializeComponent(); pleaseWait.Show(); } 

这样很容易改变,即PleaseWait表格的进度条,而不会遇到线程的麻烦。

此致,Volker