The timer instantiation and enabling is evaluated when you close your form, because ShowDialog
is synchronous. You should put your timer before your ShowDialog
Timer starts only after the Form is closed
-
01-04-2022 - |
Question
I am currently working on a C# application that runs on a Windows CE 5 device with MS Compact Framework 2.0. In this application I call a singleton dialog from a keyboard-hook asynchronously via BeginInvoke:
this.BeginInvoke((ThreadStart)delegate()
{
DlgX.getInstance().display(TaskController.getInstance().getActiveTask().getValues(), true);
});
In the display method of the dialog I want to set the focus to a certain control. As the Win CE device is very slow, I have to use a timer to delay the Focus() execution:
System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
timer.Interval = 600;
timer.Enabled = true;
timer.Tick += (EventHandler)delegate(object obj, EventArgs args)
{
button1.Focus();
timer.Dispose();
};
Unfortunately this does not work. The timer gets executed as soon as I close the dialog. What am I doing wrong?
Thank you in advance for any help!
edit: This is the whole display() method of the dialog:
public void display(List<InputRow> fvList, bool validate)
{
this.fvList = fvList;
ctlCount = (fvList.Count > 5 ? 5 : fvList.Count);
for (int i = 0; i < ctlCount; i++)
{
//some 100% irrelevant stuff
}
button1.KeyDown += new KeyEventHandler(btnOK_KeyDown);
button1.Click += new EventHandler(btnOK_Click);
if (!this.Visible)
{
ShowDialog();
}
if (validate)
{
System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
timer.Interval = 600;
timer.Enabled = true;
timer.Tick += (EventHandler)delegate(object obj, EventArgs args)
{
button1.Focus();
timer.Dispose();
};
}
}
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow