WinForm Realizes Page Button Timing Hiding Function

  • 2021-12-13 09:04:35
  • OfStack

This paper describes the implementation of page button timing hiding function by WinForm. Share it for your reference, as follows:

Sometimes in the process of doing a page need to show a 1 item, and then disappear after a period of time, this can be achieved through timer timing


private void Form1_Load(object sender, EventArgs e)
{
  System.Timers.Timer t = new System.Timers.Timer(3000);
  t.Elapsed += new System.Timers.ElapsedEventHandler(OnTimedEvent);
  t.Enabled = true;
  t.AutoReset = false;
}

And then in OnTimedEvent Event, such as: label1.Visible = false; At this point, we will find that these two threads are not the same thread, so we need to use the delegate delegate To implement cross-threading

Define a delegate


private delegate void SetVisibleCallback();
// In giving label1.visible You can call the following method where you assign a value 
private void SetVisible()
{
  // InvokeRequired Need to compare calling threads ID And creating threads ID
  //  Returns if they are not the same true
  if (this.label1.InvokeRequired)
  {
    SetVisibleCallback d = new SetVisibleCallback(SetPan);
    this.Invoke(d);
   }
   else
   {
    this.label1.Visible = false;
   }
}

Called in the event generated by timer SetVisible() You can


private void OnTimedEvent(object sender, System.Timers.ElapsedEventArgs e)
{
  SetPan();
}

Of course, if you don't delegate the implementation, you can use the OnTimedEvent Write in the event


CheckForIllegalCrossThreadCalls = false;//// Avoid cross-threading problems 
label1.Visible = false;

For more readers interested in C # related content, please check the topics on this site: "WinForm Control Usage Summary", "C # Form Operation Skills Summary", "C # Data Structure and Algorithm Tutorial", "C # Common Control Usage Tutorial", "C # Object-Oriented Programming Introduction Tutorial" and "C # Programming Thread Use Skills Summary"

I hope this article is helpful to everyone's C # programming.


Related articles: