Anyone following along that has poked around what exists for the Einhugur C# UI Framework should notice that there IS a Timer object in there already.
Since its in the Einhugur.Threading namespace we need to add that using clause to our project
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using AppKit;
using CloudKit;
using Einhugur.Forms;
using Einhugur.Threading; // <<<<<<<<<<<<<<<
using Mono.Data.Sqlite;
using Npgsql;
Using one is as simple as declaring the property for it, creating it, setting its period, or interval, and adding a method for it to call whenever the Timer needs to run some action.
timer = new Timer();
timer.Interval = 0.05;
timer.Action += timer_Action;
One thing you dont need to do, since its not a visual control, is add it to the list of controls in SetUpControls. Since its actually a property on the layout thats enough for it to function exactly as we want.
However a timer by itself isn’t very interesting. Let’s have it drive something visual like a Level Indicator.
TextField textfield1;
TextField textfield2;
ListBox list;
Button pushButton;
Timer timer;
LevelIndicator indicator;
We’ll want to add one more using clause to access the Einhugur Drawing namespace where things like color and gb functions are defined
using Einhugur.Drawing;
And then in SetupControls we need to add code to set the indicator up
indicator = new LevelIndicator((float)(pushButton.Left + pushButton.Width + 12), (float)pushButton.Top, 200, 22);
indicator.Style = LevelIndicator.LevelIndicatorStyle.DiscreteCapacity;
indicator.CriticalFillColor = Color.RGB(255, 0, 0);
indicator.WarningFillColor = Color.RGB(0, 0, 255);
indicator.FillColor = Color.RGB(0, 0, 0);
indicator.MinValue = 0;
indicator.MaxValue = 100;
indicator.WarningValue = indicator.MinValue + (0.50 * (indicator.MaxValue - indicator.MinValue));
indicator.CriticalValue = indicator.MinValue + (0.90 * (indicator.MaxValue - indicator.MinValue));
indicator.Value = 0;
return new Control[] { textfield1, textfield2,
list, pushButton, indicator };
Indicators come in several styles. They are explained on this Apple page. But they are easy to play with. One gives you a segmented capacity, like an audio meter, one a continuous bar that has not marked gradation, one shows as a star rating. Try them all 🙂
Certain settings, like critical fill color warning fill color, warning value and critical value may not make sense with some styles.
If you use the discrete and continue capacity styles they do. And when the value of the level indicator is above the warning, and less than critical that portion of the indicator in the warning color. Same for critical. We’ll set all these up and hook it to a timer that will reveal what I mean.
Using the settings above now all we need is the timer action method to do something to the level indicator. Lets make it count up and down to animate the level indicator.
private int increment = 1;
private void timer_Action(object sender, EventArgs a)
{
if (indicator.Value <= indicator.MinValue)
{
// reverse and go up
increment = 1;
}
else if (indicator.Value >= indicator.MaxValue)
{
// reverse & go down
increment = -1;
}
// change the level indicator
indicator.Value = indicator.Value + increment;
}
OK we have most everything set up. Instead of hooking this timer up to start when we push the button on our layout, which would be simple, lets start it when the window opens.
We dont want to put the call to the timers start method in the Setup of the controls. So how do we get this into what would be the equivalent of the Open event ?
If you right click on “Window” in our definition of the class
public class MainWindow : Window
and select “Go To Definition”
you get the assembly browser. In there you can see that a Window has an Opening method we can make use of. Perfect !
To override the default implementation we need to tell the compiler this IS an override
protected override void Opening()
{
timer.Start();
}
Often the easiest way to get the right definition for the method you want to override is to copy the declaration from the assembly browser, paste it into your code and change virtual to override.
And now as the timer runs its action event it affects the value of the level indicator. On the way up as the level indicator gets to the warning value the color changes to the warning fill color. And when it gets to the critical value it again changes. And as it goes back down in value the colors change again.
Again – our project as it is now