Form.Show()是不是显示它的子控件 [英] Form.Show() is not showing it's child controls
问题描述
我有一个表格, frmPleaseWait
,具有 MarqueeProgressBar
和标签
我要当UI加载在结构不良的应用程序,我们有数据使用。
的问题是, frmPleaseWait.Show()
示出的形式,但不是在它的控制。这仅仅是一个白色的长方形。现在 frmPleaseWait.ShowDialog()
显示了子控件,但不会让UI加载数据。
我在想什么?下面是从那里我想这是一个code段。
PLEASEWAIT =新frmPleaseWait();
PleaseWait.Show(本);
//设置在HUD对象的所有可用HUD值
HUD.LastName = GetCurrentRowVal(名字)修剪()。
HUD.FirstName = GetCurrentRowVal(名字)修剪()。
HUD.PersonId = Convert.ToInt32(GetCurrentRowVal(PERSONID)修剪());
HUD.SSn = GetCurrentRowVal(SSN)修剪()。
HUD.MiddleName = GetCurrentRowVal(MiddleName)修剪()。
HUD.MasterID = ConnectBLL.BLL.DriInterface.CheckForDriId(HUD.PersonId)的ToString();
//这个加载大量用户控件与数据
shellForm.FormPaint(HUD.PersonId);
PleaseWait.Close();
修改
根据答案,我尝试跟进。
这是我,但我得到一个跨线程异常
在 pleaseWaitInstance.Location = parent.PointToScreen(Point.Empty);
如果我删除该行它将运行,但它运行在我的屏幕的左上角,而忽略了应用程序的位置。
公共部分类frmPleaseWait:XtraForm
{
公共frmPleaseWait()
{
的InitializeComponent();
}
私有静态frmPleaseWait pleaseWaitInstance;
公共静态无效创建(XtraForm父)
{
变种T =新System.Threading.Thread(
()=>
{
pleaseWaitInstance =新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();
}
公共静态无效的destroy()
{
如果(pleaseWaitInstance!= NULL)pleaseWaitInstance.Invoke(新动作(()=> pleaseWaitInstance.Close()));
}
}
您形式不出于同样的原因shellForm不起作用工作。在UI线程繁忙装载和绘画的控件,它不能在同一时间绘制你的PLEASEWAIT形式。你需要创建一个泵消息循环,让您的PW形式活一个单独的线程。你可以把它的工作是这样的:
公共部分类PLEASEWAIT:表格{
私有静态PLEASEWAIT mInstance;
公共静态无效创建(){
变种T =新System.Threading.Thread(()=> {
mInstance =新PLEASEWAIT();
mInstance.FormClosed + =(S,E)=> mInstance = NULL;
Application.Run(mInstance);
});
t.SetApartmentState(System.Threading.ApartmentState.STA);
t.IsBackground = TRUE;
t.Start();
}
公共静态无效的destroy(){
如果(mInstance!= NULL)mInstance.Invoke(新动作(()=> mInstance.Close()));
}
私人PLEASEWAIT(){
的InitializeComponent();
}
//等等...
}
使用范例:
PleaseWait.Create();
尝试 {
System.Threading.Thread.Sleep(3000);
}
最后 {
PleaseWait.Destroy();
}
I have a form, frmPleaseWait
, that has a MarqueeProgressBar
and a Label
that I want to use when the UI is loading the data in a poorly structured app we have.
The problem is that frmPleaseWait.Show()
shows the form but not the controls in it. It is just a white rectangle. Now frmPleaseWait.ShowDialog()
shows the child controls but doesn't let the UI load it's data.
What am I missing? Below is a code snippet from where I am trying this.
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();
Edit
:
Follow up based on answers and my attempt.
This is what I have but I get a Cross-Thread Exception
on pleaseWaitInstance.Location = parent.PointToScreen(Point.Empty);
If I remove that line it will run but it runs in the top left corner of MY screen and ignores the position of the app.
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()));
}
}
Your form doesn't work for the same reason shellForm doesn't work. The UI thread is busy loading and painting the controls, it can't paint your PleaseWait form at the same time. You'll need to create a separate thread that pumps a message loop to keep your PW form alive. You could make it work like this:
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...
}
Sample usage:
PleaseWait.Create();
try {
System.Threading.Thread.Sleep(3000);
}
finally {
PleaseWait.Destroy();
}
这篇关于Form.Show()是不是显示它的子控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!