Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C#
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();
  }
}
Posted
Updated 21-Jun-15 23:57pm
v3
Comments
Andy Lanng 22-Jun-15 5:47am    
post your code to show the form, and also the Page.OnLoad of the loading form
Member 11740651 22-Jun-15 5:54am    
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();


}
}


this is the code
Andy Lanng 22-Jun-15 6:01am    
You can add code to the question using "Improve Question" above these comments. I have done it for you.

Hmm. I assume that SetBatteryStatus isn't very intensive?

what is you have this.show() at the top and this.Refresh() where it is? That may give the form enough time to display first, run the setup and then show the results? You may see a better response
Member 11740651 22-Jun-15 6:08am    
When I use this.show() alone without this.Refresh() it is just displaying the forms without any colors and operations done on that.So when i add this.Refresh() below this.Show(), when the code execution point comes to this.Show() it is just displaying the forms without any colors and operations done on that for 1 second and when the code execution point comes to this.Refresh() it is working fine. it is showing the form with colors and operations done on that.
Ralf Meier 22-Jun-15 6:59am    
There is a Problem with the initialisation of your Form.
I think, "SetBatteryStatus" is a method of your Form. What happens in it ?

1 solution

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:
C#
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.

C#
//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();
}


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[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900