PopUp window on a specific time in WPF? - c#

How can I create and show a popup window at a specific time in WPF?
What I mean how to display the window on the side of system tray.

You could use a timer if you're trying to make the thing popup in a certain number of hours/seconds/minutes (or work out how many hours/seconds/minutes are left until your specific time comes around).
private System.Windows.Threading.DispatcherTimer popupTimer;
// Whatever is going to start the timer - I've used a click event
private void OnClick(object sender, RoutedEventArgs e)
{
popupTimer = new System.Windows.Threading.DispatcherTimer();
// Work out interval as time you want to popup - current time
popupTimer.Interval = specificTime - DateTime.Now;
popupTimer.IsEnabled = true;
popupTimer.Tick += new EventHandler(popupTimer_Tick);
}
void popupTimer_Tick(object sender, EventArgs e)
{
popupTimer.IsEnabled = false;
// Show popup
// ......
}
Ok, so you also want to know how to do a notifier popup type thing, which maybe this article in CodeProject might help.

Check out this question for firing an event at a set time.

You might want to check out DispatcherTimer.

Related

C# winforms with lots of buttons with custom backgrounds and mouse hover

I have project with several hundreds of buttons, created dynamically within for-loop. I also have timer to update toolstripstatuslabel (labelClock) with current time every second:
static System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
...
timer.Tick += new System.EventHandler(timer_Tick);
...
private void timer_Tick(object sender, EventArgs e)
{
labelClock.Text = DateTime.Now.ToString();
}
Here's the problem: last clicked button will be focused, of course. So if I scroll page down, everytime timer ticks page scrolls up (or down), so the focused button becomes visible.
How can I prevent that?
Stupid solution (won't work, if you need to save focus of button):
private void timer_Tick(object sender, EventArgs e)
{
justAnotherButton.Focus();
labelClock.Text = DateTime.Now.ToString();
}
You need to pass focus to another focusable control, it may be another Button, TextBox, etc. You may use button with zero Width and Height, so user won't see it.
If you are not concerned with keeping the same button focussed, set the focus to the parent form. Although this sounds like a bit of a nightmare to deal with, and i'd look for a way not not needing hundreds of buttons!
private void timer_Tick(object sender, EventArgs e)
{
formMain.Focus()
labelClock.Text = DateTime.Now.ToString();
}
You could create a container control that extends System.Windows.Forms.ContainerControl and override the ScrollToControl to return the current scroll position. Then add all your buttons to this control.
class MyContainer : ContainerControl
{
protected override System.Drawing.Point ScrollToControl(Control activeControl)
{
return base.AutoScrollPosition;
}
}

Is there a way to set a control as a tooltip?

I'm wondering if it's possible to use ToolTip.SetToolTip or something similar to open a control as a tooltip instead of just a string (i.e. SetToolTip(controlToWhichToAdd, panelToDisplayAsToolTip) instead of passing a string as your second parameter).
If this isn't possible I'm guessing next best thing is displaying a panel on the mouse location on mouse_enter event on the control and removing it (or making it invisible) on mouse_leave.
Or are there other practices that make this possible in an easier way?
This is not possible out of the box. You have two choices. First option is to override the Draw Event, which will let you customize how the tooltip looks. Here is an example of this. Be sure you set the OwnerDraw property to true if you use this method!
Although the first method will work if you just need some simple customization, the second option will work best if you need more flexible options. The second option is to do what you already suggested and create your own sort of tooltip. Simply put, you would first create an event handler for the MouseEnter event. When that event fires, you'd enable a Timer. This timer would be the delay that occurs before the tooltip is show. Then finally, you'd just make your panel appear at the mouse coordinates.
Suppose you have a form with a button and timer on it and you want the button to have a tooltip that is a panel:
public partial class Form1 : Form
{
private Panel _myToolTipPanel;
private void Form1_Load(object sender, EventArgs e)
{
_myToolTipPanel = new Panel {Visible = false};
Controls.Add(_myToolTipPanel);
Label myLabel = new Label();
myLabel.Text = "Testing";
_myToolTipPanel.Controls.Add(myLabel);
}
private void button1_MouseEnter(object sender, EventArgs e)
{
timer1.Enabled = true;
}
private void button1_MouseLeave(object sender, EventArgs e)
{
timer1.Enabled = false;
_myToolTipPanel.Visible = false;
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Enabled = false;
Point position = Cursor.Position;
Point formPoisition = PointToClient(position);
_myToolTipPanel.Visible = true;
_myToolTipPanel.Location = formPoisition;
}
}
Now of course you will have to do some beautifying of the tooltip, but this is the general idea!
One Approach could be inheriting the ToolTip control and then override the SetToolTip and Show methods . Inside the SetToolTip the private method - SetToolTipInternal needs to be re-written , but most of the functionality could be reuse - it uses the Mouse Events ( leave , move) to bind region. but since tooltip uses internal's of windows to show the baloon window. you will have to override quite a bit of code.
but this could be time consuming and needs quite a bit of testing.
You could write a handler for the Tooltip.Popup event, and cancel the popup to display your own panel.
You'd need to clean it up at the appropriate time, though.
For example:
private void ToolTip1_Popup(Object sender, PopupEventArgs e)
{
e.Cancel = true;
//Do work here to display whatever control you'd like
}
If you're just looking for more formatting options in the tooltip display, an alternative is something like this CodeProject entry, which implements an HTML-enabled tooltip:

How to set a Timer based on a user choice?

I am working on a small program and, everything works fine. Instead of having a hard coded timer though, I'd like to change the timers interval from the form from a listbox or a numericupdownbox, combobox or something along those lines.
So instead of it being a hard coded 3000MS like it is I wanted to be able to change it on the form from a small menu with 1000-10000 milliseconds.
The thing is, I am not sure how to tell the timer to use an interval specified in a optional box.
Is it possible?
Thanks.
If you're going to use a control that allows for a non-numeric input such as a TextBox ensure you validate that the input is in fact a number. Better to trap errors in the first place than deal with exceptions later.
private void SetIntervalButton_Click(object sender, EventArgs e)
{
int interval = 0;
bool success = Int32.TryParse(intervalTextBox.Text, out interval);
if(success)
{
operationTimer.Interval = interval;
}
}
You can omit the checking above if you're using a NumericUpDown control as it only allows a numeric value.
private void SetIntervalButton_Click(object sender, EventArgs e)
{
operationTimer.Interval = (int) numericUpDown1.Value;
}
You can set the interval of a timer to a value you like in the change event your your combobox.
private void ComboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
ComboBox comboBox = (ComboBox) sender;
aTimer.Interval = double.Parse(ComboBox1.SelectedValue);
}
You should use double.TryParse if you can not ensure valid data for setting the interval. If the value is being taken from a combobox and its read-only then there is no need for TryParse.
You can try:
myTimer.Interval = (int) myTextbox.Text;
You can put this in textchanged event if you want it fired every time you change the textbox value.

How to trigger the auto-hide icon in c#?

I have an application. If the application is not being used for a certain amount if time, it should hide. When application is hidden and we mouse-over the icon, it should be restored.
How can I do this? Thanks in advance.
You have to define a timer in your application that will count the time when mouse is not over the form/window. Then just hide your application.
Download WPF NotifyIcon
And handle MouseOver event, that will show Form/Window
EDIT:
If you do not need to minimize application to tray and hide window keeping it on desktop -> use the same algorithm, but do not hide the window, just set transparency to 0% or 10%. When mouse is over - set transparency to 100%.
Like JesseJames said, use a timer to measure the inactive time of the application and hide it after an amount of time. Re-activate it when the mouse is hovered over the NotifyIcon. Here's a sample WindowsForms solution that does the job:
private Timer _timer;
private int _ticks;
public Form1()
{
_timer = new Timer { Interval = 1000, Enabled = true };
_timer.Tick += TimerTick;
Activated += Form1_Activated;
MouseMove += Form1_MouseMove;
//notifyIcon1 is an icon set through the designer
notifyIcon1.MouseMove += NotifyIcon1MouseMove;
}
protected void TimerTick(object sender, EventArgs e)
{
//After 5 seconds the app will be hidden
if (_ticks++ == 5)
{
WindowState = FormWindowState.Minimized;
Hide();
_timer.Stop();
_ticks = 0;
}
}
protected void NotifyIcon1MouseMove(object sender, MouseEventArgs e)
{
WindowState = FormWindowState.Normal;
Show();
_ticks = 0;
_timer.Start();
}
protected void Form1_MouseMove(object sender, MouseEventArgs e)
{
_ticks = 0;
}
Perhaps there might exist a cleaner solution, I don't know, but it gets you on the way. Same principle will go for WPF, only the code will be slightly different. Hope this helps!
To see if the user has made any input you can use a similar approach like this one. To get your application visible again you need a way to get the global mouse and maybe keyboard input, to do this you can use hooks, you can find one solution for that here. And if the hook is triggered it really just depends on what kind of UI you are using, but calling specific hide or show methods should be suffice.

c# Shutdown a system after holding yes for 5 seconds

I was wondering if anyone knows how to use a dialog box to create a hold down button event. Here is the scenerio:
a user would like to shutdown their system, but because it is critical that they confirm, that user must hold the button for 5 seconds before the action can be done.
I am trying to do it in a yes no scenario ie.
To confirm shutdown please hold "Yes" for 5 seconds.
Anyone done this before able to offer a little help/insight?
Try using a button's Mouse_Down & Mouse_Up event, and a timer (this assumes you're using WinForms).
private void button1_MouseDown(object sender, MouseEventArgs e)
{
if (this.timer1.Enabled == false)
{
this.timer1.Interval = 5000;
this.timer1.Enabled = true;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
this.timer1.Enabled = false;
MessageBox.Show("Shutdown!");
}
private void button1_MouseUp(object sender, MouseEventArgs e)
{
timer1.Enabled = false;
}
You could capture the button press on 'mousedown', and start a 5-second timer. Once the timer completes, shutdown is initiated. If a 'mouseup' event happens, it could stop and reset the timer.
Sure, handle BOTH the mousedown event and the mouseup event. Start a timer on the mousedown and see how long it has run on the mouseup. Done!
You could do this any number of ways. The first that comes to my mind would be to spin off a thread that waits 5 seconds and is simply aborted if the user's mouse comes back up.
Thread shutdown;
private void button1_MouseDown(object sender, MouseEventArgs e)
{
shutdown = new Thread(()=>ShutDown());
shutdown.Start();
}
private void ShutDown()
{
Thread.Sleep(5000);
Console.Write("5 seconds has elapsed");
// Do something.
}
private void button1_MouseUp(object sender, MouseEventArgs e)
{
if (shutdown != null)
{
shutdown.Abort();
shutdown = null;
}
}
Low overhead and you're not adding additional supporting controls for something this simple.
Why bother when you can just use getAsyncKeyState()? Tell them to hold down 'y' for 5 seconds. You can find a reference here: http://www.pinvoke.net/default.aspx/user32.getasynckeystate
Or you can do it your way and start a timer on MouseDown, then on MouseUp, end the timer and then see if it's more or less than 5 seconds. http://msdn.microsoft.com/en-us/library/system.windows.forms.control.mousedown%28VS.71%29.aspx
You can use the Form.MouseDown events do detect that the user has pressed the mouse button. In the event handler, check to see if cursor is over the button or not (the event is passed in the coordinates of the cursor). You can then enable a timer which will tick in 5 seconds, and perform the shutdown when the timer ticks.
When the user first clicks YES, start a timer that repeatedly checks if the mouse location is inside of the button. After 5 seconds has elapsed, proceed with the shutdown. If the user moves the mouse out of the button, stop the timer.
private DateTime mouseDownTime;
private void Button_MouseDown(object sender, MouseButtonEventArgs e)
{
mouseDownTime = DateTime.Now;
}
private void Button_MouseUp(object sender, MouseButtonEventArgs e)
{
if (mouseDownTime.AddSeconds(5) < DateTime.Now)
MessageBox.Show("You held it for 5 seconds!");
}
You can set up a timer on the MouseDown event, and if the mouse capture changes (check the MouseCaptureChanged event) to false before the timer event fires, cancel the timer.

Categories

Resources