.
In Depth Banner
Skip Navigation Links

Select your preferred language

The control with a timeout.

  /// <summary>

  /// Summary description for AControl.

  /// </summary>

  [Designer(typeof(AControlDesigner))] //associate the designer with the control

  public class AControl : Control

  {

 

    private TimeSpan _usableTime;

 

    /// <summary>

    /// The period of time after which the control will become unusable

    /// </summary>

    public TimeSpan UsableTime

    {

      get

      {

        return _usableTime;

      }

      set

      {

        _usableTime = value;

        EnableTimeout=EnableTimeout;

      }

    }

 

    Timer TimeoutTimer;

 

    bool _enableTimeout;

 

    /// <summary>

    /// The control will time out and become unusable after a period of time

    /// </summary>

    public bool EnableTimeout

    {

      get

      {

        return _enableTimeout;

      }

      set

      {

        _enableTimeout=value;

        if (TimeoutTimer!=null)

          TimeoutTimer.Dispose();

        if(value && this._usableTime>new TimeSpan(0,0,0,0,0))

        {

          TimeoutTimer=new Timer();

          TimeoutTimer.Interval=(int)_usableTime.TotalMilliseconds;

          TimeoutTimer.Tick+=new EventHandler(TimeoutTimer_Tick);

          TimeoutTimer.Enabled=true;

        }

      }

    }

 

    public AControl()

    {

      //Because the BackColor is always PowderBlue the BackColor property must be removed.

      this.BackColor=Color.PowderBlue;

    }

 

    private void TimeoutTimer_Tick(object sender, EventArgs e)

    {

      TimeoutTimer.Enabled=false;

      this.Enabled=false;

      Invalidate();

    }

 

    protected override void OnPaint(PaintEventArgs e)

    {

      if (this.Enabled==false)

      {

        e.Graphics.DrawLine(Pens.Red,0,0,this.Width,this.Height);

        e.Graphics.DrawLine(Pens.Red,this.Width,0,0,this.Height);

      }

      base.OnPaint (e);

    }

 

 

  }

 

Return to the article

Copyright © Bob Powell 2000-.  All rights reserved.