I am developing a C# desktop application. I want my all open windows to pop up (something which happens with Alt + Tab) every 5th minute. I looked at a few questions here. They suggest doing it by using timers, but how do I pop up the minimised windows?
Here is a really basic example for you to work on.
First create the timer.
Create a function that will run when the timer ticks.
Then add an event to run every time it ticks. And link it your function
Inside that function check if it has been 5 minutes. If so, maximize
the window
public partial class TimerForm : Form
{
Timer timer = new Timer();
Label label = new Label();
public TimerForm ()
{
InitializeComponent();
timer.Tick += new EventHandler(timer_Tick); // Everytime timer ticks, timer_Tick will be called
timer.Interval = (1000) * (1); // Timer will tick evert second
timer.Enabled = true; // Enable the timer
timer.Start(); // Start the timer
}
void timer_Tick(object sender, EventArgs e)
{
// HERE you check if five minutes have passed or whatever you like!
// Then you do this on your window.
this.WindowState = FormWindowState.Maximized;
}
}
Here is the complete solution
public partial class Form1 : Form
{
int formCount = 0;
int X = 10;
System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
public Form1()
{
InitializeComponent();
timer.Tick += new EventHandler(timer_Tick); // Everytime timer ticks, timer_Tick will be called
timer.Interval = (1000) * X; // Timer will tick evert second
timer.Enabled = true; // Enable the timer
timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
FormCollection fc = new FormCollection();
fc = Application.OpenForms;
foreach (Form Z in fc)
{
X = X + 5;
formCount++;
if (formCount == fc.Count)
X = 5;
Z.TopMost = true;
Z.WindowState = FormWindowState.Normal;
System.Threading.Thread.Sleep(5000);
}
}
}
Related
This is my implementation of a Win Form app that has a countdown timer:
readonly DateTime myThreshold;
public Form1()
{
InitializeComponent();
myThreshold = Utils.GetDate();
Timer timer = new Timer();
timer.Interval = 1000; //1 second
timer.Tick += new EventHandler(t_Tick);
timer.Start();
//Threshold check - this only fires once insted of each second
if (DateTime.Now.CompareTo(myThreshold) > 0)
{
// STOP THE TIMER
timer.Stop();
}
else
{
//do other stuff
}
}
void t_Tick(object sender, EventArgs e)
{
TimeSpan timeSpan = myThreshold.Subtract(DateTime.Now);
this.labelTimer.Text = timeSpan.ToString("d' Countdown - 'hh':'mm':'ss''");
}
The wanted behavior is to stop the timer and the tick function when the threshold is reached.
This now does not happens because the check is only executed once since it is placed in the Form1 initialization.
Does exist a way to add this check in a way to immediately stop the Timer once a condition has been meet?
If we define timer as a class field (so it can be accessed from all methods in the class), then we can just add the check to the Tick event itself, and stop the timer from there:
private Timer timer = new Timer();
void t_Tick(object sender, EventArgs e)
{
// Stop the timer if we've reached the threshold
if (DateTime.Now > myThreshold) timer.Stop();
TimeSpan timeSpan = myThreshold.Subtract(DateTime.Now);
this.labelTimer.Text = timeSpan.ToString("d' Countdown - 'hh':'mm':'ss''");
}
I have the following timer implementation. But the timer is not running every 5 seconds as needed. How can make this run every 5 seconds. At present its running about once in 30 seconds.
private void button1_Click(object sender, RoutedEventArgs e)
{
msgsent = 0;
timer.Tick += timer_Tick;
timer.Interval = new TimeSpan(0, 0, 5);
bool isenable = timer.IsEnabled;
timer.Start();
}
private async void timer_Tick(object sender, object e)
{
if (geo == null)
{
geo = new Geolocator();
}
Geoposition posi = await geo.GetGeopositionAsync();
if (posi.Coordinate.Point.Position.Latitude <= 12.9227 && posi.Coordinate.Point.Position.Longitude >= 080.1320)
{
if (msgsent <=1)
{
msgsent = msgsent + 1;
ShowDialog(new MessageDialog("Your Bus has crossed xyz"));
}
}
}
I'll give you a hint. If you understand where each goes, then it should be clear. If not, you will once you get better.
// in the class definition
int msgsent;
Timer timer;
and
// in the constructor
timer = new Timer();
timer.Tick += timer_Tick;
timer.Interval = new TimeSpan(0, 0, 5);
and
// in the Button.Click event handler
timer.Start();
and
// in the Timer.Tick event handler
timer.Stop();
/* do your work here */
timer.Start();
There will be further issues when the user is clicking the button while you're doing your work, but that's beyond the scope of this question.
I have buttons which validate if the user is administrator or not. If the user currently login is not an administrator then label will show as warning message and then hide after a few seconds. I tried using lblWarning.Hide(); and lblWarning.Dispose(); after the warning message, but the problem is, it hides the message before even showing the warning message. This is my code.
private void button6_Click(object sender, EventArgs e)
{
if (txtLog.Text=="administrator")
{
Dialog();
}
else
{
lblWarning.Text = "This action is for administrator only.";
lblWarning.Hide();
}
}
You're going to want to "hide" it with a Timer. You might implement something like this:
var t = new Timer();
t.Interval = 3000; // it will Tick in 3 seconds
t.Tick += (s, e) =>
{
lblWarning.Hide();
t.Stop();
};
t.Start();
instead of this:
lblWarning.Hide();
so if you wanted it visible for more than 3 seconds then just take the time you want and multiply it by 1000 because Interval is in milliseconds.
If you are using UWP XAML in 2020 and your msgSaved label is a TextBlock, you could use the code below:
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(2);
msgSaved.Visibility = Visibility.Visible;
timer.Tick += (s, en) => {
msgSaved.Visibility = Visibility.Collapsed;
timer.Stop(); // Stop the timer
};
timer.Start(); // Starts the timer.
Surely you could just use Thread.Sleep
lblWarning.Text = "This action is for administrator only.";
System.Threading.Thread.Sleep(5000);
lblWarning.Hide();
Where 5000 = the number of miliseconds you want to pause/wait/sleep
The following solution works for wpf applications. When you start timer a separate thread is started. To update UI from that thread you have to use dispatch method. Please the read the comments in code and use code accordingly. Required header
using System.Timers;
private void DisplayWarning(String message, int Interval = 3000)
{
Timer timer = new Timer();
timer.Interval = Interval;
lblWarning.Dispatcher.Invoke(new Action(() => lblWarning.Content = message));
lblWarning.Dispatcher.Invoke(new Action(() => lblWarning.Visibility = Visibility.Visible));
// above two line sets the visibility and shows the message and interval elapses hide the visibility of the label. Elapsed will we called after Start() method.
timer.Elapsed += (s, en) => {
lblWarning.Dispatcher.Invoke(new Action(() => lblWarning.Visibility = Visibility.Hidden));
timer.Stop(); // Stop the timer(otherwise keeps on calling)
};
timer.Start(); // Starts the timer.
}
Usage :
DisplayWarning("Warning message"); // from your code
this function display specific msg on an label for specific time duration including text style
public void show_MSG(string msg, Color color, int d)
{
this.Label.Visible = true;
this.Label.Text = msg;
this.Label.ForeColor = color;
Timer timer = new Timer();
timer.Interval = d;
timer.Tick += (object sender, EventArgs e) =>
{
this.Label.Visible = false;
}; timer.Start();
}
I've no real idea how to do this and I have tried messing with a timer but to no avail so far.
So what am I trying to do?
I have a label that is blank. When a certain event is triggered I want the label to say "Competition successfully setup" for a period of 5 seconds after which I want it to return to being blank.
Surely this can be done?? Can it? I have played around with a timer but I seem to be well off the mark.
Any help would be most welcome. My feeble attempt is below.
private void UpdateLabel(object sender, EventArgs e)
{
var timer = new Timer()
{
Interval = 5000,
};
timer.Tick += (s, evt) =>
lblCompetitionSetupSuccess.Text = "Competition successfully setup";
timer.Start();
lblCompetitionSetupSuccess.Text = string.Empty;
}
Try the other way around:
private void button1_Click(object sender, EventArgs e)
{
label1.Text = "I will vanish in 5 sec";
var timer = new Timer();
timer.Interval = 5000;
timer.Tick += (o, args) => label1.Text = "";
timer.Start();
}
First set the label to whatever text you want it to display for 5 sec
label1.Text = "I will vanish in 5 sec";
Then setup your timer so that on timer elapsed it will remove the text
var timer = new Timer();
timer.Interval = 5000;
timer.Tick += (o, args) => label1.Text = "";
timer.Start();
If you want the timer to stop after the first timer elapse:
timer.Tick += (o, args) =>
{
label1.Text = "";
timer.Enabled = false;
};
Make sure you're using the System.Windows.Forms.Timer class, which calls the tick event on the UI thread.
I would like to set a textbox text to "blink" by changing text colors when a button is clicked.
I can get the text to blink how I want it to, but I want it to stop after a few blinks. I cannot figure out how to make it stop after the timer fires a few times.
Here is my code:
public Form1()
{
InitializeComponent();
Timer timer = new Timer();
timer.Interval = 500;
timer.Enabled = false;
timer.Start();
timer.Tick += new EventHandler(timer_Tick);
if (timerint == 5)
timer.Stop();
}
private void timer_Tick(object sender, EventArgs e)
{
timerint += 1;
if (textBoxInvFooter.ForeColor == SystemColors.GrayText)
textBoxInvFooter.ForeColor = SystemColors.Highlight;
else
textBoxInvFooter.ForeColor = SystemColors.GrayText;
}
I know my problem lies with how I'm using the "timerint", but I'm not sure where to put it, or what solution I should use...
Thank you for all your help!
You just have to put the timer check inside the Tick handler. You can access the Timer object by using the sender argument of the handler.
private void timer_Tick(object sender, EventArgs e)
{
// ...
timerint += 1;
if (timerint == 5)
{
((Timer)sender).Stop();
}
}
Here's the complete code that I would use to solve your issue. It correctly stops the timer, detaches the event handler, and disposes the timer. It disables the button during the flashing, and also restores the colour of the textbox after the five flashes are complete.
The best part is that it is purely defined within the one lambda, so no class-level variables required.
Here it is:
button1.Click += (s, e) =>
{
button1.Enabled = false;
var counter = 0;
var timer = new Timer()
{
Interval = 500,
Enabled = false
};
EventHandler handler = null;
handler = (s2, e2) =>
{
if (++counter >= 5)
{
timer.Stop();
timer.Tick -= handler;
timer.Dispose();
textBoxInvFooter.ForeColor = SystemColors.WindowText;
button1.Enabled = true;
}
else
{
textBoxInvFooter.ForeColor =
textBoxInvFooter.ForeColor == SystemColors.GrayText
? SystemColors.Highlight
: SystemColors.GrayText;
}
};
timer.Tick += handler;
timer.Start();
};