Here's the C# code directly from the website (http://jobijoy.blogspot.com/2007/10/time-picker-user-control.html) that everyone refers to when someone asks about TimePicker for WPF, although I moved it around a little bit to be more organized. (Please note, if you're trying to run this code to work with it: you must change the XAML code on this site from KeyDown to PreviewKeyDown on the 3 Grids where Hours, Minutes, and Seconds displays live, and change the TextBlocks with each Grid to TextBoxes)
public partial class TimeControl : UserControl
{
public TimeControl()
{
InitializeComponent();
}
public TimeSpan Value
{
get { return (TimeSpan)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value", typeof(TimeSpan), typeof(TimeControl),
new UIPropertyMetadata(DateTime.Now.TimeOfDay, new PropertyChangedCallback(OnValueChanged)));
public int Hours
{
get { return (int)GetValue(HoursProperty); }
set { SetValue(HoursProperty, value); }
}
public static readonly DependencyProperty HoursProperty =
DependencyProperty.Register("Hours", typeof(int), typeof(TimeControl),
new UIPropertyMetadata(0, new PropertyChangedCallback(OnTimeChanged)));
public int Minutes
{
get { return (int)GetValue(MinutesProperty); }
set { SetValue(MinutesProperty, value); }
}
public static readonly DependencyProperty MinutesProperty =
DependencyProperty.Register("Minutes", typeof(int), typeof(TimeControl),
new UIPropertyMetadata(0, new PropertyChangedCallback(OnTimeChanged)));
public int Seconds
{
get { return (int)GetValue(SecondsProperty); }
set { SetValue(SecondsProperty, value); }
}
public static readonly DependencyProperty SecondsProperty =
DependencyProperty.Register("Seconds", typeof(int), typeof(TimeControl),
new UIPropertyMetadata(0, new PropertyChangedCallback(OnTimeChanged)));
private static void OnValueChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
TimeControl control = obj as TimeControl;
control.Hours = ((TimeSpan)e.NewValue).Hours;
control.Minutes = ((TimeSpan)e.NewValue).Minutes;
control.Seconds = ((TimeSpan)e.NewValue).Seconds;
}
private static void OnTimeChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
TimeControl control = obj as TimeControl;
control.Value = new TimeSpan(control.Hours, control.Minutes, control.Seconds);
}
private void Down(object sender, KeyEventArgs args)
{
switch (((Grid)sender).Name)
{
case "sec":
if (args.Key == Key.Up)
this.Seconds++;
if (args.Key == Key.Down)
this.Seconds--;
break;
case "min":
if (args.Key == Key.Up)
this.Minutes++;
if (args.Key == Key.Down)
this.Minutes--;
break;
case "hour":
if (args.Key == Key.Up)
this.Hours++;
if (args.Key == Key.Down)
this.Hours--;
break;
}
}
}
I'm not very good with Dependency or Binding yet, I'm just learning it, that's why I can't figure it out. But here's the problem: When the Minutes or Seconds are taken beyond 59/-59 there is an infinite loop. I'll explain the flow of it (at least I'm learning that much here!):
Let's say the TimeControl object is at 0:59:00 and we press the up key while focused on the minute TextBox. So, as we follow the logic, it goes to the PreviewKeyDown event, and the switch statement takes us to this.Minutes++ which gets Minutes and sees 59, so sets minutes to 60.
This triggers OnTimeChanged for Minutes, which gets Hours (0) Minutes (60) Seconds (0) and sets Value to that. Since Value is a TimeSpan, it interprets this as 1:00:00, which is great.
So, once that is set, it tiggers OnValueChanged, which sets Hours to 1, and this immediately calls back to OnTimeChanged for Hours. At this point it gets Hours (1) Minutes (60) Seconds (0) and sets Value to that (which is interpreted as 2:00:00).
Now we have an infinite loop until Hours becomes too large and throws an exception. This is a little over my head to understand how to fix it. What would be the 'proper' fix? I know it could be fixed with if statements in the switch statement, or even the OnTimeChanged/OnValueChanged methods, but I'm sure there's a better way to do it with the dependencies.
Simple Fix: change it so it resets the minutes first, then update the hour.
// Disclaimer: Haven't read the code yet so i might be wrong
No need to set properties if they aren't different, try something like this:
private static void OnValueChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
TimeControl control = obj as TimeControl;
var ts = (TimeSpan)e.NewValue;
if(ts.Hours != control.Hours) control.Hours = ts.Hours;
if(ts.Minutes != control.Minutes) control.Minutes = ts.Minutes;
if(ts.Seconds != control.Seconds) control.Seconds = ts.Seconds;
}
Normally I'd put this logic in the setters, something you see common with data access layers...but I think your dependency calls would still happen there, so best to do it in this event handler in your code.
Related
Im using a multi-select ListView in C# .NET 4.5
The issue occurs when selecting multiple items (ie. Shift + End or Shift + Click, etc.) These are just a few examples of many different mouse/keyboard combinations for multi-selecting of course..
This is my event handler for when selecting items in a list:
private void lvTitles_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
MessageBox.Show(e.Item.Text.ToString());
//MessageBox just for testing I am actually running a SQL query here
}
My problem is that if I select 500 items the event is triggered 500 times. The intent is to get that last item the user selected (via keyboard/mouse combinations mentioned above), on and do something with it ... in my case I need to run a SQL query against it.
If I click first on item 0 in the listview it is ok to run the query, then when you shift+end it highlights all the rest, and I want it to run the query only on the last item selected. Instead it is running on every item in between.
EDIT: On another note, the event triggers as it unselects as well, in which case it really shouldn't do anything when deselecting.
Have you considered performing the action on a button press instead? That way they can also use Ctrl-Click to select any individual items they want?
Otherwise what you would have to do is wait a certain amount of time before firing the action, known as debouncing, you can read a more about debouncing here: https://stackoverflow.com/a/4517995/984780
I created a class you can use for debouncing:
public class Debounce {
private Action _action;
private bool _isThreadRunning;
private Thread _thread;
private DateTime _runAt;
private double _waitSeconds;
private Debounce(double waitSeconds, Action action) {
_action = action;
_waitSeconds = waitSeconds;
}
private void Invoke() {
_runAt = DateTime.Now.AddSeconds(_waitSeconds);
lock(this) {
if(!_isThreadRunning) {
_isThreadRunning = true;
_thread = new Thread(() => {
while(true) {
Thread.Sleep(100);
lock(this) {
if(DateTime.Now > _runAt) {
_action();
_isThreadRunning = false;
_thread = null;
break;
}
}
}
});
_thread.Start();
}
}
}
private static Dictionary<Action, Debounce> __debounces;
private static Dictionary<Action, Debounce> _debounces {
get {
if(__debounces == null) {
__debounces = new Dictionary<Action, Debounce>();
}
return __debounces;
}
}
public static void Run(double waitSeconds, Action action) {
Debounce debounce;
if(!_debounces.TryGetValue(action, out debounce)) {
debounce = new Debounce(waitSeconds, action);
_debounces.Add(action, debounce);
}
debounce._waitSeconds = waitSeconds;
debounce.Invoke();
}
}
Then you can change your code to this:
private void lvTitles_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
Debounce.Run(5, () => MessageBox.Show(e.Item.Text.ToString()));
}
This should work no matter how they select items, it will run your code 5 seconds after their last selection action.
I just wrote this class and did a quick test, a more thorough test would be advised. In any case hopefully it's enough to get the idea.
I am attempting to put all my timing logic into an attached behavior. My goals are:
(Using Visual Studio 2010.)
Simplify my XAML code by replacing many MultiValue triggers,
Change the background color of each ListViewItem based on a timer and data within the ListViewItem,
Use a singleton pattern for the timer as I only want one timer for all my ListViewItems.
My problems are:
How to have the display watch for color changes
Get the specific ListViewItem instance when the timer event fires ("this" is not allowed), and
Removing all memory footprints of the static properties and events when the user control holding the ListView and its items is closed.
Problem #3 may be done automatically by WPF and C# (I'm not sure). #1 poses a problem, because if I understand attached properties correctly, the property will only set the background color on initialization so update to the background color will not be made by the simple XAML shown below. Is a Trigger necessary?
How can this be done?
TIA
Here is what I have so far:
XAML
<Style x:Key="listViewItemStyle" TargetType="{x:Type ListViewItem}">
<Setter Property="v:ListViewItemBehavior.MyValue" Value="{Binding}"/>
--IS A TRIGGER NEEDED HERE TO UPDATE THE BACKGROUND COLOR, AND IF SO
HOW IS IT BOUND TO THE CLASS OF THE ATTACHED PROPERTY WHICH IS NOT IN THE
VIEW MODEL?
</Style>
Code
// Using singleton pattern to create one time to be shared among all ListViewItem instances.
public static class ListTimer
{
// Reasons for using a DispatcherTimer opposed to a System.Timers.Timer are that the DispatcherTimer runs on the same thread as the
// Dispatcher and a DispatcherPriority can be set on the DispatcherTimer. Timer runs in its own thread.
private static readonly Timer listTimer;
static ListTimer()
{
listTimer = new Timer { AutoReset = true, Enabled = true, Interval = 10 * 1000 }; // Interval in milliseconds
listTimer.Elapsed += listTimer_Elapsed;
}
static void listTimer_Elapsed(object sender, ElapsedEventArgs e)
{
if (ListTimerEvent != null)
{
ListTimerEvent(sender, e);
}
}
public static event EventHandler ListTimerEvent;
}
// Static classes can not implement an interface. (Hence : INotifyPropertyChanged can not be used).
public static class ListViewItemBehavior
{
public static string GetMyValue(DependencyObject obj)
{
return (string)obj.GetValue(MyValueProperty);
}
public static void SetMyValue(DependencyObject obj, string value)
{
obj.SetValue(MyValueProperty, value);
}
// Using a DependencyProperty as the backing store for MyValue. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MyValueProperty =
DependencyProperty.RegisterAttached("MyValue", typeof(Object), typeof(ListViewItemBehavior), new UIPropertyMetadata(null, OnMyValueChanged));
static void OnMyValueChanged(DependencyObject depObj, DependencyPropertyChangedEventArgs e)
{
var item = depObj as ListViewItem;
if (item == null) return;
--EACH INSTANCE HAS ITS OWN VALUE ON VIEW_ENCOUNTERTIME--
View_encountertime vt = item.DataContext as View_encountertime;
ListTimer.ListTimerEvent +=new EventHandler(ListTimer_ListTimerEvent);
}
static void ListTimer_ListTimerEvent(object sender, EventArgs e)
{
Timer timer = sender as Timer;
var y = this.GetValue(MyValueProperty); <-- WRONG! CAN'T USE "THIS"
-- I would put logic to set the background color here for
-- each instance ...if I knew how!
}
You can use an anonymous method in the OnMyValueChanged to specify the timer event handler. This way, you will have access to the specific ListViewItem.
static void OnMyValueChanged(DependencyObject depObj, DependencyPropertyChangedEventArgs e)
{
var item = depObj as ListViewItem;
if (item == null) return;
View_encountertime vt = item.DataContext as View_encountertime;
ListTimer.ListTimerEvent += (sender, e) =>
{
Timer timer = sender as Timer;
var y = item.GetValue(MyValueProperty);
<-- put logic to set the background color here
}
}
My final solution that seems to work without any explicit triggers with the timer as above is:
static void OnMyValueChanged(DependencyObject depObj, DependencyPropertyChangedEventArgs args)
{
var item = depObj as ListViewItem;
if (item == null) return;
// This is unable to pass the instance data: ListTimer.ListTimerEvent +=new EventHandler(ListTimer_ListTimerEvent);
// Use an anonymous method in the OnMyValueChanged to specify the timer event handler. This will give access to the specific
// ListViewItem.
ListTimer.ListTimerEvent += (sender, e) =>
{
Timer timer = sender as Timer;
// The Timer is running on a different thread then the ListViewItem item
item.Dispatcher.Invoke((Action)(() =>
{
View_encountertime vt = item.DataContext as View_encountertime;
if (vt == null) return;
if (vt.Tcheckout != null || vt.Notseen == true)
{
item.Background = Brushes.White;
return;
}
DateTime z = (DateTime)vt.Tencounter;
// get current time.
DateTime now = DateTime.Now;
TimeSpan ts = now.Subtract(z);
item.Background = Brushes.Red;
if (ts.CompareTo(WaitingTime.ninetymin) < 1) item.Background = Brushes.Orange;
if (ts.CompareTo(WaitingTime.seventyfivemin) < 1) item.Background = Brushes.Yellow;
if (ts.CompareTo(WaitingTime.sixtymin) < 1) item.Background = Brushes.Green;
if (ts.CompareTo(WaitingTime.fortyfivemin) < 1) item.Background = Brushes.Turquoise;
if (ts.CompareTo(WaitingTime.thirtymin) < 1) item.Background = Brushes.Blue;
if (ts.CompareTo(WaitingTime.fifteenmin) < 1) item.Background = Brushes.Violet;
}));
};
}
As it turns out, Background is a dependency property of the ListViewItem, so no explicit trigger is needed to update the display when the Background property is changed. The UI will take care of itself.
Im using a multi-select ListView in C# .NET 4.5
The issue occurs when selecting multiple items (ie. Shift + End or Shift + Click, etc.) These are just a few examples of many different mouse/keyboard combinations for multi-selecting of course..
This is my event handler for when selecting items in a list:
private void lvTitles_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
MessageBox.Show(e.Item.Text.ToString());
//MessageBox just for testing I am actually running a SQL query here
}
My problem is that if I select 500 items the event is triggered 500 times. The intent is to get that last item the user selected (via keyboard/mouse combinations mentioned above), on and do something with it ... in my case I need to run a SQL query against it.
If I click first on item 0 in the listview it is ok to run the query, then when you shift+end it highlights all the rest, and I want it to run the query only on the last item selected. Instead it is running on every item in between.
EDIT: On another note, the event triggers as it unselects as well, in which case it really shouldn't do anything when deselecting.
Have you considered performing the action on a button press instead? That way they can also use Ctrl-Click to select any individual items they want?
Otherwise what you would have to do is wait a certain amount of time before firing the action, known as debouncing, you can read a more about debouncing here: https://stackoverflow.com/a/4517995/984780
I created a class you can use for debouncing:
public class Debounce {
private Action _action;
private bool _isThreadRunning;
private Thread _thread;
private DateTime _runAt;
private double _waitSeconds;
private Debounce(double waitSeconds, Action action) {
_action = action;
_waitSeconds = waitSeconds;
}
private void Invoke() {
_runAt = DateTime.Now.AddSeconds(_waitSeconds);
lock(this) {
if(!_isThreadRunning) {
_isThreadRunning = true;
_thread = new Thread(() => {
while(true) {
Thread.Sleep(100);
lock(this) {
if(DateTime.Now > _runAt) {
_action();
_isThreadRunning = false;
_thread = null;
break;
}
}
}
});
_thread.Start();
}
}
}
private static Dictionary<Action, Debounce> __debounces;
private static Dictionary<Action, Debounce> _debounces {
get {
if(__debounces == null) {
__debounces = new Dictionary<Action, Debounce>();
}
return __debounces;
}
}
public static void Run(double waitSeconds, Action action) {
Debounce debounce;
if(!_debounces.TryGetValue(action, out debounce)) {
debounce = new Debounce(waitSeconds, action);
_debounces.Add(action, debounce);
}
debounce._waitSeconds = waitSeconds;
debounce.Invoke();
}
}
Then you can change your code to this:
private void lvTitles_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
Debounce.Run(5, () => MessageBox.Show(e.Item.Text.ToString()));
}
This should work no matter how they select items, it will run your code 5 seconds after their last selection action.
I just wrote this class and did a quick test, a more thorough test would be advised. In any case hopefully it's enough to get the idea.
Forgive the ignorance of this question, as I'm completely new to C#, but how would one go about setting and handling a temporary or timed variable? For example, whenever an event fires, I want to increment a counter. If the counter exceeds 3 within 60 seconds, I want to trigger another event. Elsewise, if 60 seconds passes and the counter isn't incremented, it resets back to 0.
Should I be using MemoryCache? Or is there some nifty flag I can set for variables to make them unset after a specified duration (assuming they aren't refreshed)? What would be the best way to go about accomplishing this simple task? Note that I'm not necessarily looking for someone to write it for me; just a helpful hand or suggestion to point me in the right direction. This is a rough outline of what I'm trying to accomplish:
private static int totalCount = 0;
private static double maxCount = 3;
private static double timeLimit = 60;
private static void TestEvent(object src, EventArgs mea)
{
totalCount++;
if (totalCount > maxCount)
{
DoSomething();
}
}
Thanks for any assistance you can offer. I make it a point to always reward helpful answers.
You could maintain a Queue<T> (see http://msdn.microsoft.com/en-us/library/7977ey2c.aspx), where each entry is the time that an event fired. When an event fires, you first remove from the queue any entries that are more than 60 seconds old (which is easy, because the queue is ordered by time, and the head of the queue is the oldest entry), then add a new entry for the event that just fired. If the count of the queue exceeds your threshold, then you have satisfied the condition you're looking for.
I recommend using DateTime.UtcNow instead of DateTime.Now for the time you store in the Queue<T>. UtcNow is much faster, and it avoids the problem that transitions from daylight savings time to standard time and vice versa can cause.
Here is some code off the top of my head (may need a little fixing up):
private static Queue<DateTime> eventQueue = new Queue<DateTime>();
private static int timeWindowSeconds = 60;
private static int threshold = 3;
private static void TestEvent(object src, EventArgs mea) {
DateTime now = DateTime.UtcNow;
DateTime tooOld = now.AddSeconds(-timeWindowSeconds);
// remove old entries
while((eventQueue.Count > 0) && (eventQueue.Peek() < tooOld)) {
eventQueue.Dequeue();
}
// add new entry
eventQueue.Enqueue(now);
// test for condition
if (eventQueue.Count >= threshold) {
eventQueue.Clear();
DoSomething();
}
}
You might do it like this:
private static int totalCount = 0;
private static double maxCount = 3;
private static TimeSpan timeLimit = TimeSpan.FromSeconds(60);
private static DateTime lastIncrementTime;
private static void TestEvent(object src, EventArgs mea)
{
// If the time between now and lastIncrementTime is more than the timeLimit...
if(DateTime.Now - lastIncrementTime > timeLimit)
{
totalCount = 0;
}
lastIncrementTime = DateTime.Now;
totalCount++;
if (totalCount > maxCount)
{
DoSomething();
}
}
You can use the StopWatch class.
On the form load event (if you want to count the 60 second from that event), start the timer,
Everytime in the click event,check how many seconds it is invoked your other method or so.
I'd do something like this:
private class SomeEventMonitor
{
public int Threshold { get ; private set ; }
public TimeSpan ThresholdWindow { get ; private set ; }
private DateTime marker ;
private int count ;
/// <summary>
/// public constructor
/// </summary>
/// <param name="threshold"></param>
/// <param name="window"></param>
public SomeEventMonitor( int threshold , TimeSpan window )
{
if ( threshold < 1 ) throw new ArgumentOutOfRangeException("threshold") ;
if ( window <= TimeSpan.Zero ) throw new ArgumentOutofRangeException("window") ;
this.Threshold = threshold ;
this.ThresholdWindow = window ;
Reset() ;
return ;
}
private void Reset()
{
this.marker = DateTime.Now ;
this.count = 0 ;
return ;
}
public event EventHandler ThresholdExceeded ;
private static readonly object latch = new object() ;
public void EventWatcher( object source , EventArgs eventArgs )
{
lock ( latch )
{
DateTime current = DateTime.Now ;
if ( ++count > Threshold )
{
TimeSpan window = current -marker ;
if ( window > ThresholdWindow )
{
ThresholdExceeded( this , new EventArgs() ) ;
Reset() ;
}
}
}
return ;
}
}
You can use another variable to log the time of changing the value of totalcount. check it with the current time and do whatever you want.
Here is the code...
private static int totalCount = 0;
private static double maxCount = 3;
private static double timeLimit = 60;
private static DateTime timeflag= DateTime.Now;
private static void TestEvent(object src, EventArgs mea)
{
if (timeflag.AddSeconds(timeLimit) < DateTime.Now)
{
totalCount = 0;
}
totalCount++;
timeflag = DateTime.Now;
if (totalCount > maxCount)
{
DoSomething();
}
}
There are myriad ways you could approach this. The first thing that comes to mind for me would be to use a Stopwatch object or a Timer object that starts on a background thread, and then write an event handler that can subscribe to the event in which you're interested. As the event occurs, your handler fires, allowing you to suspend the timer and query the time elapsed, and make your increment/reset decision accordingly.
That's merely a very rough sketch of one notion, but should give you some ideas moving forward. Good luck.
Per the comment made by #hatchet above, this almost starts to sound like a queue with "expiring" members or a "sliding window" event horizon you'd have to capture should that comment accurately reflect your problem.
EDIT Being the borderline obsessive-compulsive that I am, I gave your original problem some thought and came up with a concept that may or may not be relevant to your problem, but at least for the academic exercise I'm going to post what I did. What caught my attention in your original post was the notion of an expiring or timed variable which I thought was quite novel. Your problem specified that you want to do something specific when a given interval elapses.
I tried to abstract that idea into a generic form, thinking of a few ways such an idea might be useful. One idea that came to mind was in a game environment, where (for example) a message might only be available to the player for 20 seconds before "self-destructing." I could imagine how having the expiration "plumbing" wired into a type might prove very convenient. Another scenario could be in a CAI environment where a System.Drawing.Image should only be displayed for a fixed time, then disappear - again, a scenario where having the expiration and timing code built-in could be useful.
So, with at least that much notional practicality in mind, I set to work, and what I threw together (and I won't pretend that its comprehensive or complete) is a generic for an Expiring type, expressed as Expiring<T>. The baseline code I've put together is as follows:
// First stab at an "expiring" type that is only valid for a set interval.
public class Expiring<T>
{
public delegate void ExpiredHandler(object sender, EventArgs e);
public event ExpiredHandler OnExpired;
T instance;
int signaledCount = 0;
long milliseconds = 0;
bool isExpired = false;
bool exceptOnExpiredReference = true;
System.Timers.Timer lapseTimer = new System.Timers.Timer();
public Expiring(T value)
{
instance = value;
}
public virtual void TimerElapsed(object sender, System.Timers.ElapsedEventArgs args)
{
if (OnExpired != null)
{
OnExpired(this, null);
}
isExpired = true;
}
public Expiring(T startValue, long expirationInterval, bool throwElapsedReferenceException):this(startValue)
{
milliseconds = expirationInterval;
lapseTimer.AutoReset = true;
lapseTimer.Interval = milliseconds;
exceptOnExpiredReference = throwElapsedReferenceException;
lapseTimer.Elapsed+=new System.Timers.ElapsedEventHandler(TimerElapsed);
this.Set();
}
public void Set()
{
signaledCount++;
lapseTimer.Stop();
lapseTimer.Start();
}
public T Value
{
get
{
if (!isExpired || !exceptOnExpiredReference)
return instance;
else
throw new InvalidOperationException("Reference to an expired value.");
}
set
{
instance = value;
}
}
}
The idea here is that someone could declare an Expiring<int>, specify its initial value, expiration time, and a value to indicate whether an attempt to access the value of an instance after the expiration interval has passed should throw an exception. When the expirationInterval passes, the OnExpired event is raised, allowing the declarer to specify a custom event handler to provide custom actions when the value expires.
If the caller wishes to reset the expiration timer, he need only call the Set() method of the object. That also increments an internal "signaledCount" value that I ultimately did not use, but was thinking of in terms of determining how many times the expiration timer has been reset. If the Value property of the object is accessed after the expiration interval passes, an InvalidOperationException is thrown with a "Value has expired" message.
This idea, unfortunately, has more notional/academic value than practical, I'm afraid. It would have a great deal more utility if it were possible to overload all the arithmetic operators to the implementations of the native value types, but I discovered quickly that C# doesn't like this notion at all (and found that out right here on a rather extensive post on the subject here on SO). Ideally, I'd love to be able to say something like:
Expired<Int32> foo = new Expired<Int32>(5,10000,true);
Expired<Int32> bar = new Expired<Int32>(10,10000,true);
Expired<Int32> baz = foo+bar; // can't make that work
There was some notion that this problem could be overcome with dynamic types, but I opted not to pursue it at this point. The idea, as I hammered it out, is offered for discussion as it applies to a generic view of the OP's "timed variable" notion. Constructive comments/criticism/refinements encouraged and welcome.
When I use a numbericupdown object with thousandsseperator set to true it only updates the text to display the commas correctly when it loses focus. Is there a way to force it to refresh each time the value is changed?
You would need to do an event.
As we know, the thounsandseperator is triggered by focus we can simply call it as we type.
private void numericUpDown1_KeyUp(object sender, KeyEventArgs e)
{
numericUpDown1.Focus();
//Edit:
numericUpDown1.Select(desiredPosition,0)
}
So as the user type, we give the box the it focus back which is a hack to recall the thousandsseperator formatting.
Note: Problems with hacks are wierd situations which calls for more hacks... e.g: Cursor sets back to the front of the text... you would need another hack to fix it.
Experiment with the other events to find the one that fits your case.
Edit: Btw, if you do want to go even further with this ...
Keep Track of the cursor.
Put back the cursor at the right position when keyup is called.
Setting the cursor position in numericUpDown control
To format the text value in your control you'd need a call to ParseEditText() which is protected but can be accessed from a class which inherits NumericUpDown. The problem is after your call the cursor will move before the first character. In order to control the position of the cursor you need access to the SelectionStart property which NumericUpDown don't expose. NumericUpDown still has a field named upDownEdit of type UpDownEdit. The UpDownEdit class although internal inherits from TextBox and behave much like one. So a solution would be to inherit from NumericUpDown and use reflection to get/set the value of upDownEdit.SelectionStart. Here is something you can work on:
public class NumericUpDownExt : NumericUpDown
{
private static FieldInfo upDownEditField;
private static PropertyInfo selectionStartProperty;
private static PropertyInfo selectionLengthProperty;
static NumericUpDownExt()
{
upDownEditField = (typeof(UpDownBase)).GetField("upDownEdit", BindingFlags.Instance | BindingFlags.NonPublic);
Type upDownEditType = upDownEditField.FieldType;
selectionStartProperty = upDownEditType.GetProperty("SelectionStart");
selectionLengthProperty = upDownEditType.GetProperty("SelectionLength");
}
public NumericUpDownExt() : base()
{
}
public int SelectionStart
{
get
{
return Convert.ToInt32(selectionStartProperty.GetValue(upDownEditField.GetValue(this), null));
}
set
{
if (value >= 0)
{
selectionStartProperty.SetValue(upDownEditField.GetValue(this), value, null);
}
}
}
public int SelectionLength
{
get
{
return Convert.ToInt32(selectionLengthProperty.GetValue(upDownEditField.GetValue(this), null));
}
set
{
selectionLengthProperty.SetValue(upDownEditField.GetValue(this), value, null);
}
}
protected override void OnTextChanged(EventArgs e)
{
int pos = SelectionStart;
string textBefore = this.Text;
ParseEditText();
string textAfter = this.Text;
pos += textAfter.Length - textBefore.Length;
SelectionStart = pos;
base.OnTextChanged(e);
}
}