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 it
MyForm
:
void MethodToShowMyForm()
{
MyForm myForm = new MyForm();
myForm.Show();
}
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
.
this.Shown += new System.EventHandler(MyForm_Shown);
private void MyForm_Shown(Object sender, EventArgs e)
{
DisplayBatteryStatus();
}
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[
^]