Show()方法未在Winforms C#中正确显示GUI [英] Show() method not showing the GUI Properly in Winforms C#
问题描述
在Windows应用程序中,要显示我使用过此.Show()的表单。但它只是在没有任何颜色和操作的情况下显示表单。我在this.Show()之后使用了this.Refresh(),这个只显示没有任何颜色和操作的表单1秒钟,并显示颜色和操作。如果我们使用ShowDialog()它按预期工作但我需要使用Show()。
任何人都可以帮助我。提前致谢
Hi,
In Windows applications, to display the form i have used this.Show(). But it is just showing the Forms without any colors and operations done on that. I have used this.Refresh() after this.Show() , this one just shows the forms without any colors and operations for 1 second and displays the colors and operations done on that. if we use ShowDialog() it is working as expected but i need to do with Show().
Can anyone please help me. Thanks in advance
public void DisplayBatteryStatus()
{
if (nremaining > 50 && nremaining < 75) // GOOD
{
if (bdischarging)
SetBatteryStatus(nremaining, 1);
//else if ((!bdischarging) && (!bGreen) && (!bYellow) && (!bOrange) && (!bRed))
// SetBatteryStatus(nremaining, 1);
}
else if (nremaining > 25 && nremaining < 50) // MEDIUM
{
if (bdischarging)
SetBatteryStatus(nremaining, 2);
//else if ((!bdischarging) && (bGreen) && (!bYellow) && (!bOrange) && (!bRed))
// SetBatteryStatus(nremaining, 2);
}
else if (nremaining > 10 && nremaining < 25)// LOW
{
if (bdischarging)
SetBatteryStatus(nremaining, 3);
//else if ((!bdischarging) && (bGreen) && (bYellow) && (!bOrange) && (!bRed))
// SetBatteryStatus(nremaining, 3);
}
else if (nremaining <= 10)// CRITICAL
{
if (bdischarging)
SetBatteryStatus(nremaining, 4);
//else if ((!bdischarging) && (bGreen) && (bYellow) && (bOrange) && (!bRed))
// SetBatteryStatus(nremaining, 4);
}
if (!ismsgboxOpened && isSatisfied)
{
this.Show();
this.Refresh();
}
}
推荐答案
根据您的问题描述,我相信您的问题是顺序您正在执行哪些方法。如果没有看到您的表单在何处以及如何实例化,我无法给您一个确切的原因。我能做的就是你如何做到这一点的理想方式。
有些地方,这个表格正在被实例化。在这个例子中,我们称之为MyForm
:
Based on the description of your problem, I believe your issue is the sequence in which you are executing the methods. Without seeing where and how your form is instantiated, I can't give you an exact cause. What I can do you is how you the ideal way to go about this.
Some where, this form is being instantiated. In this example, we'll call itMyForm
:
void MethodToShowMyForm()
{
MyForm myForm = new MyForm();
myForm.Show();
}
请注意上面的示例中我如何调用 Show()
我实例化的方法。这是.NET中的首选模式。您的表单永远不会显示自己。
在您的表单中,您应该执行 DisplayBatteryStatus()
方法事件显示
。
Notice how in the example above, I call the Show()
method where I instantiated. This is the preferred pattern in .NET. Your forms should never show themselves.
In your form, you should execute the DisplayBatteryStatus()
method on the event Shown
.
//Found in the designer file
this.Shown += new System.EventHandler(MyForm_Shown);
//In your form's code behind
private void MyForm_Shown(Object sender, EventArgs e)
{
DisplayBatteryStatus();
}
只要您的表单显示给用户,您的方法就会相应地设置表单并显示用户你打算如何展示它们。在这种情况下,我更喜欢的另一个事件是加载
事件。加载表单时(我相信这是在初始化完成后,不是100%肯定),这段代码将被执行。这样,当您显示表单时,它已经设置了正确的UI布局和颜色,然后才会向用户显示。
您需要从 DisplayBatteryStatus中删除
this.Show()
/ code>方法。根据代码的执行方式,您可能仍需要 Refresh
调用,但我认为您不需要它。如果有的话,你可能需要添加 Application.DoEvents()
让它完成处理所有待处理的事情。
以下是我讨论的事件的一些链接。
Form.Shown事件 [ ^ ]
Form.Load事件 [ ^ ]
As soon as your form is shown to the user, your method will set the form accordingly and show the user what you intended on showing them. Another event, one that I would prefer in this situation, is the Load
event. When the form is loaded (I believe this is after the initialize is complete, not 100% sure) this code would get executed. That way, when you show the form, it was already setup with the correct UI layout and colors before it was ever shown to the user.
You will need to remove the this.Show()
from your DisplayBatteryStatus
method. Depending on how your code executes, you may still need the Refresh
call, but I don't think you will need it. If anything, you might need to add Application.DoEvents()
to have it finish processing everything pending.
Here are some links to the events I discussed.
Form.Shown Event[^]
Form.Load Event[^]
这篇关于Show()方法未在Winforms C#中正确显示GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!