I have a NotifyIcon method, although I would like the timeout to happen, before disposing of the BaloonTip.
private void button1_Click(object sender, EventArgs e)
{
notifyIcon1.Visible = true;
notifyIcon1.ShowBalloonTip(30000);
<wait until timeout occurs>
notifyIcon1.Dispose();
}
notifyIcon1.BalloonTipClosed += delegate {notifyIcon1.Dispose ();};
I would rather hide the NotifyIcon instead of recreating/disposing a new instance of it.
Try using a timer.
Should be something like...:
private Timer taskTimer;
private NotifyIcon notifyIcon1;
private void button1_Click(object sender, EventArgs e)
{
notifyIcon1.Visible = true;
notifyIcon1.ShowBalloonTip(30000);
taskTimer = new Timer(TimerCallback, notifyIcon1, 30000, System.Threading.Timeout.Infinite);
}
and...
void TimerCallback(object notifyIcon1Obj)
{
lock (notifyIcon1Obj)
{
NotifyIcon notifyIcon1 = (NotifyIcon)notifyIcon1Obj;
notifyIcon1.dispose();
notifyIcon1 = null;
}
}
HTH
Related
My C# application starts minimised and shows the video stream of a webcam for 5 seconds when a certain event occurs. After that it is minimised again. This works well, but only from the 2nd event. On the first call, only an empty form is displayed.
Is there an event in which I can add a videoSourcePlayer1.Show() so that something is also displayed in the 1 event? It does nothing in the Form1_Shown event.
My code:
...
private readonly UdpClient udp = new UdpClient(port);
public IntPtr myHandle;
public void wait(int milliseconds)
{
...
}
private void StartListening()
{
this.udp.BeginReceive(Receive, new object());
}
private void Receive(IAsyncResult ar)
{
IPEndPoint ip = new IPEndPoint(IPAddress.Parse(ip), port);
byte[] bytes = udp.EndReceive(ar, ref ip);
string message = Encoding.ASCII.GetString(bytes);
if (message == "ON")
{
ShowWindow(myHandle, SW_RESTORE);
Thread.Sleep(50);
SetForegroundWindow(myHandle);
wait(5000);
ShowWindow(myHandle, SW_SHOWMINIMIZED);
}
StartListening();
}
private void Form1_Load(object sender, EventArgs e)
{
myHandle = this.Handle;
videoSourcePlayer1.VideoSource = new MJPEGStream("http://ip/video.mjpg");
videoSourcePlayer1.Start();
this.WindowState = FormWindowState.Minimized;
StartListening();
}
private void Form1_Resize(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
Hide();
notifyIcon1.Visible = true;
videoSourcePlayer1.Hide();
}
if (this.WindowState == FormWindowState.Normal)
{
Show();
this.WindowState = FormWindowState.Normal;
notifyIcon1.Visible = false;
videoSourcePlayer1.Show();
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
videoSourcePlayer1.Stop();
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
Show();
this.WindowState = FormWindowState.Normal;
notifyIcon1.Visible = false;
}
}
What can I do?
Got it. I changed the execution order in Form1_Resize and added a delay.
if (this.WindowState == FormWindowState.Normal)
{
videoSourcePlayer1.Show();
Thread.Sleep(250);
this.WindowState = FormWindowState.Normal;
notifyIcon1.Visible = false;
Show();
}
Add this method to continue the stream
private void bntContinue_Click(object sender, EventArgs e)
{
videoSourcePlayer1.Continue();
}
I have a textbox in my WinForm and when I type the password in, its hidden because:
private void textBoxPWMain2_TextChanged(object sender, EventArgs e)
{
textBoxPWMain2.UseSystemPasswordChar = true;
}
is it possible to add here a button, and while the button is pressed, the password show normal and when I stop pressing the button, the password will hide again?
Maybe this? (Don't forget to subscribe to these events)
private void button2_MouseDown(object sender, EventArgs e)
{
textBoxPWMain2.UseSystemPasswordChar = false;
}
private void button2_MouseUp(object sender, EventArgs e)
{
textBoxPWMain2.UseSystemPasswordChar = true;
}
I have a solution now, I wanted something like a eye button, when you press it down the password shows, when you stop pressing, the password hides again.
Solution
First I added a pictureBox with Eye Icon and added this pictureBox to my password textbox and set Passwort textbox to .UseSystemPasswordChar
public Form1
{
textBoxPW.Controls.Add(pictureBoxEye);
pictureBoxEye.Location = new Point(95,0);
pictureBoxEye.BackColor = Color.Transparent;
textBoxPW.UseSystemPasswordChar = true;
//Subscribe to Event
pictureBoxPW.MouseDown += new MouseEventHandler(pictureBoxPW_MouseDown);
pictureBoxPW.MouseUp += new MouseEventHandler(pictureBoxPW_MouseUp);
}
Added the Mouse_Down/Up Event
private void pictureBoxEye_MouseDown(object sender, MouseEventArgs e)
{
textBoxPW.UseSystemPasswordChar = false;
}
private void pictureBoxEye_MouseUp(object sender, MouseEventArgs e)
{
textBoxPW.UseSystemPasswordChar = true;
}
This works fine for me! Thank you guys !!
Adding a bit change details to ispiro's answer
public void button1_MouseDown(object sender, EventArgs e)
{
textBox1.PasswordChar = '\0';
textBox1.UseSystemPasswordChar = false;
}
public void button1_MouseUp(object sender, EventArgs e)
{
textBox1.PasswordChar = '*';
textBox1.UseSystemPasswordChar = true;
}
Before:-
After :-
Is there a reason you set the UseSystemPasswordChar in the TextChanged event?
If you can set the property in the Initialize() method or in the constructor you can implement the following events for your button:
private void button1_MouseDown(object sender, MouseEventArgs e)
{
textBoxPWMain2.UseSystemPasswordChar = false;
}
private void button1_MouseUp(object sender, MouseEventArgs e)
{
textBoxPWMain2.UseSystemPasswordChar = true;
}
Unfortunately I was not able to find relevant answer to my problem. I have a object encoder that has an event "VideoEncoding". It passes custom EncodingEventArgs that include various Properties like Progress, Size etc. I can output this info to Console or write to text file. But when I try to utilize it in WinForms I'm not able to pass that information to UI like label or progress bar. I tried different approaches. Background Worker seems like a good idea, The problem is that Background Worker cannot subscribe to VideoEncoding event, neither it will take my custom EventArgs. This is what i was able to put together. Maybe there is a different way to do it using delegates that would communicate with UI. Any suggestions are welcome. Thank you.
public partial class Form1 : Form
{
private BackgroundWorker bw;
int _progress;
public Form1()
{
InitializeComponent();
this.bw = new BackgroundWorker();
this.bw.DoWork += new DoWorkEventHandler(bw_DoWork);
this.bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
this.bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
this.bw.WorkerReportsProgress = true;
this.button1.Click += new EventHandler(button1_Click);
}
private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
this.label1.Text = "The job is: " + e.Result.ToString();
this.button1.Enabled = true;
}
private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.label2.Text = e.ProgressPercentage.ToString() + "% complete";
}
private void bw_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = (BackgroundWorker)sender;
this.Encode
worker.ReportProgress(_progress);
e.Result = "Completed";
}
private void button1_Click(object sender, EventArgs e)
{
if (!this.bw.IsBusy)
{
this.bw.RunWorkerAsync();
this.button1.Enabled = false;
}
}
public void Encode()
{
var job = new EncodingJob();
//setup encoding job
//subscribe to an event
ffmpeg.VideoEncoding += GetProgress;
ffmpeg.DoWork(job);
}
public void GetProgress(object sender, EncodingEventArgs e)
{
_progress = (int)e.Progress;
}
}
Try to call the background workers ReportProgress in the GetProgress Method. How should the form know your progress if you don't signalize it?
i create timer in my form, but after i close form, timer still working.
I tried different ways to stop this timer, but did not have any success.
what is problem?
private Timer timer100;
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.timer100 = new System.Windows.Forms.Timer(this.components);
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.MainForm_FormClosing);
this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.MainForm_FormClosed);
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
timer100.Stop();
}
private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
{
timer100.Dispose();
timer100 = null;
}
I did not put InitializeComponent() in MainForm's constructor as jhmt mentioned in comments.
This case is using C# WPF. I want to instantly disable a button after clicking it to prevent clicking it twice in short succession. I disabled the button in OnClick_Event but still clickable.
Part of source is as below.
private void Button_Click_UpdateBurndownChart(object sender, RoutedEventArgs e)
{
if(threadNotWorking)
{
updateButton.IsEnabled = false;
startWorkThread();
}
}
private void startWorkThread()
{
... ...
//after finish required process
updateButton.IsEnabled = true;
}
Is there any way to accomplish this?
you may want to use a dispatcher, there is probably a threading problem (callback function running on seperate thread and trying to access ui which runs on another thread). try this . .
updateButton.Dispatcher.BeginInvoke(
new ThreadStart(() => updateButton.IsEnabled = false),
System.Windows.Threading.DispatcherPriority.Input, null);
instead of
updateButton.IsEnabled = false;
What happens if you were instead to change the order of your events from:
updateButton.IsEnabled = false;
startWorkThread();
To
startWorkThread();
updateButton.IsEnabled = false;
Let me know how this goes.
What it looks like is that you are starting your thread then immediatly enabling your button before your thread has finished. You would be better off using a BackgroundWorker and enable your Button in the RunWorkerCompleted Event. Though you can do something similar by enabling your button using a BeginInvoke at the end of your Process.
public void doWork()
{
System.Threading.Thread.Sleep(10000); //Simulating your Process
Dispatcher.BeginInvoke(new System.Threading.ThreadStart(delegate() { updateButton.IsEnabled = true; }), System.Windows.Threading.DispatcherPriority.Background);
}
Example with BackgroundWorker
using System.ComponentModel;
public partial class MainWindow : Window
{
BackgroundWorker bgw;
public MainWindow()
{
InitializeComponent();
bgw = new BackgroundWorker();
bgw.DoWork += new DoWorkEventHandler(bgw_DoWork);
bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgw_RunWorkerCompleted);
}
void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
updateButton.IsEnabled = true;
}
void bgw_DoWork(object sender, DoWorkEventArgs e)
{
System.Threading.Thread.Sleep(10000); //Simulating your work
}
private void startWorkThread()
{
bgw.RunWorkerAsync();
}
private void updateButton_Click(object sender, RoutedEventArgs e)
{
if (bgw.IsBusy != true)
{
updateButton.IsEnabled = false;
startWorkThread();
}
}
}