When I write something wrong on a textbox and click a button, a messagebox pops up, and keeps popping up since I have a timer.
So I want to make an if statement that if the messagebox is displayed, then stop the timer, until the button is clicked once again.
I tried using this:
private void button1_Click(object sender, EventArgs e)
{
timer1.Start();
if (errormsg)
{
timer1.Stop();
}
data();
}
private void data()
{
//code
Now here's what's in my timer1 code:
private void timer1_Tick(object sender, EventArgs e)
{
int value;
if (int.TryParse(textBox1.Text, out value))
{
if (value > 0)
{
timer1.Interval = value;
}
}
button1.PerformClick();
}
here's the error message:
private void errormsg()
{
MessageBox.Show("Sorry, there was an error. Please, try again.");
}
I will also note that I'm using errormsg in an else statement on my //code
//code
else
{
errormsg();
}
So my question is:
How can I make the timer stop, if a wrong value is displayed on my textbox (//code) causing a messagebox to appear. Then, when a correct value is displayed on a textbox, and I click the button, the timer would start again?
Stop the timer in your errormsg() function. When you clicked the button1 it starts again.
private void button1_Click(object sender, EventArgs e)
{
timer1.Start();
data();
}
private void errormsg()
{
timer1.stop();
MessageBox.Show("Sorry, there was an error. Please, try again.");
}
Related
I have this below form and I want to detect key-strokes and any mouse clicks on the button.
For example, if I press S on my keyboard, message will show START and keypress will display keyboard press. The same thing happen when mouse-click on START button, except that keypress will display START BTN.
Here is my button code. If I only do for button or only IsKeyDown it works fine, but when I combine both in one form, they go haywire.
private void btnStart_Click(object sender, EventArgs e)
{
lblKeypress.Text = "START BTN";
lblmessage.Text = "START";
}
Here is my Keyboard.IsKeyDown code:
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if (Keyboard.IsKeyDown(Key.S))
{
lblKeypress.Text = "Keyboard Press";
lblmessage.Text = "START";
}
}
Please help, thanks.
try this code
private void Form1_KeyDown(object sender,
EventArgs e){
if (e.KeyData == Keys.S){
lblKeypress.Text = "Keyboard Press";
lblmessage.Text = "START";
}
}
Make sure that in your Form you set KeyPreviewProperty to TRUE
In Form1.Designer.cs
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.form_keydown);
private void form_keydown(object sender, KeyEventArgs e)
{
int keyVal = (int)e.KeyValue;
keyValue = -1;
if ((keyVal >= (int)Keys.S))
{
keyValue = (int)e.KeyValue - (int)Keys.S;
lblKeypress.Text = "Keyboard Press";
lblmessage.Text = "START";
}
}
Note:
Ensure you do not have any unrelated WPF namespaces`
(Keyboard.IsKeyDown(Key.S)) is a WPF approach, in WinForms you are better off using combination of e.KeyValue & Keys
I'm a beginner and trying to understand what timer is and how can I use it. I'll be really grateful to you if you replied to my question.
I have a timer called "Elapsed_Time" and I set the interval to 1000 millisecond.
What I wanted to achieve is to show my message: "Hi just once" just once instead of showing it for every 1 second.
private void Elapsed_Time_Tick(object sender, EventArgs e)
{
Messagebox.show("Hi just once");
}
if you still want the timer's Tick event to fire then try this...
private bool _hasTicked = false;
private void Elapsed_Time_Tick(object sender, EventArgs e)
{
if(!_hasTicked)
{
Messagebox.show("Hi just once");
_hasTicked = true;
}
}
private void Elapsed_Time_Tick(object sender, EventArgs e)
{
Messagebox.show("Hi just once");
Elapsed_Time_Tick.Enabled = false;
}
You can do like this
I am programming a chat in C# and I need a function to check if text in textbox is changed. It can be done with TextChanged event but I need to check it only at start of writing.
private void messageText_TextChanged(object sender, TextChangedEventArgs e)
{
chatApp.WriteChatLine(userName + "is typping...");
}
This code writes a chat message every time the text is changed. How to restrict it only for the first time when the user started to write a text into a textbox? I am sorry if this is a stupid question but I can't get it working. Thank you for help!
You can unsubscribe from event handler:
private void messageText_TextChanged(object sender, TextChangedEventArgs e)
{
chatApp.WriteChatLine(userName + "is typping...");
messageText.TextChanged -= messageText_TextChanged;
}
This event handler will be executed once, and then removed from handlers list. Thus further changes of text will not fire event (if there is no other subscribers) or this particular handler will not be executed.
UPDATE: As #karim noted, you will probably need to automatically reset this message after message is sent, or user stopped typing, or user deleted everything. It can be done with timer component (set it's Interval to value you want typing message be displayed after user stopped typing)
private void messageText_TextChanged(object sender, TextChangedEventArgs e)
{
// if timer already running, then don't update status
if (!timer.Enabled)
chatApp.WriteChatLine(userName + "is typping...");
timer.Stop(); // restart timer
timer.Start();
}
private void timer_Tick(object sender, EventArgs e)
{
ClearStatus();
}
private void buttonSend_Click(object sender, EventArgs e)
{
// send message
ClearStatus();
}
private void ClearStatus()
{
chatApp.WriteChatLine(""); // some code which clears status message
timer.Stop(); // stop timer
}
You can simplify this code, by creating property which will handle status update and timer starting/stopping:
private void messageText_TextChanged(object sender, TextChangedEventArgs e)
{
IsUserTyping = true;
}
private void timer_Tick(object sender, EventArgs e)
{
IsUserTyping = false;
}
private void buttonSend_Click(object sender, EventArgs e)
{
// send message
IsUserTyping = false;
}
private bool IsUserTyping
{
get { return timer.Enabled; }
set {
if (value)
{
if (!IsUserTyping)
chatApp.WriteChatLine(userName + "is typping...");
timer.Stop();
timer.Start();
}
else
{
timer.Stop();
chatApp.WriteChatLine("");
}
}
}
You could have a sentinel variable to detect if the change has already fired once or not, something like so:
bool textboxChanged = false;
private void messageText_TextChanged(object sender, TextChangedEventArgs e)
{
if(!textboxChanged) //if not changed
{
chatApp.WriteChatLine(userName + "is typping...");
}
}
So the event still gets fired but it won't do anything until you change the sentinel variable. This has the extra value of allowing you to reset the sentinel variable later on if need be without having to worry about resubscribing to the event.
The simplest version would be
bool wasUserTyping = false;
private void messageText_KeyUp(object sender, KeyEventArgs e){
wasUserTyping = true;
}
private void messageText_TextChanged(object sender, TextChangedEventArgs e){
if(wasUserTyping){
//Yaaay!! User did type
}
}
I am new in C#. I found some code which work on progressbar. What is does, when someone click on button start btnStartAsyncOperation_Click the progress bar starts increasing and when btnCancel_Click is pressed it cancel the operation. Here is the code
namespace BackgroundWorkerSample
{
public partial class Form1 : Form
{
BackgroundWorker m_oWorker;
public Form1()
{
InitializeComponent();
m_oWorker = new BackgroundWorker();
m_oWorker.DoWork += new DoWorkEventHandler(m_oWorker_DoWork);
m_oWorker.ProgressChanged += new ProgressChangedEventHandler(m_oWorker_ProgressChanged);
m_oWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(abcd);
m_oWorker.WorkerReportsProgress = true;
m_oWorker.WorkerSupportsCancellation = true;
}
void abcd(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Cancelled)
{
lblStatus.Text = "Task Cancelled.";
}
else if (e.Error != null)
{
lblStatus.Text = "Error while performing background operation.";
}
else
{
lblStatus.Text = "Task Completed...";
}
btnStartAsyncOperation.Enabled = true;
btnCancel.Enabled = false;
}
void m_oWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
lblStatus.Text = "Processing......" + progressBar1.Value.ToString() + "%";
}
void m_oWorker_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 0; i < 100; i++)
{
Thread.Sleep(100);
m_oWorker.ReportProgress(i);
if (m_oWorker.CancellationPending)
{
e.Cancel = true;
m_oWorker.ReportProgress(0);
return;
}
}
//Report 100% completion on operation completed
m_oWorker.ReportProgress(100);
}
private void btnStartAsyncOperation_Click(object sender, EventArgs e)
{
btnStartAsyncOperation.Enabled = false;
btnCancel.Enabled = true;
//Start the async operation here
m_oWorker.RunWorkerAsync();
}
private void btnCancel_Click(object sender, EventArgs e)
{
if (m_oWorker.IsBusy)
{
//Stop/Cancel the async operation here
m_oWorker.CancelAsync();
}
}
private void progressBar1_Click(object sender, EventArgs e)
{
}
private void lblStatus_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
}
}
}
Now I added 2 more button, button1 to pause and button2 to resume. Since I could not find any method to resume, I had to use CancelAsync() function when I press pause and I keep the value of progress bar in a global variable. Then when I press resume I start the progress bar again using RunWorkerAsync. But the problem is, I can not send the value of global variable in this function so it start from 0 progress.
I tried to use thread.sleep(infinite time here) when someone press pause and then stop the thread when someone press resume. Still the problem is, I can not press any button in this situation. Still if I enable button they don't work.
Please give me some solution.
You could try having your own variable, i.e
bool isPaused = false;
When someone clicks your pause button...
isPaused = true;
And set it to false when someone clicks resume. Finally, in your for loop in your doWork method, make it wait until that variable is false.
while (isPaused)
{
Thread.Sleep(100);
}
Let me know how this works out for you.
I want to display a ballon tip when an error occures instead of showing MessageBox.
[NOTE] i did not want it to be shown on mouse Hover.
I tried both but they actually show the tip on mouse hover
toolTip1.SetToolTip();
toolTip1.Show();
You can use the ToolTip Popup event to check if there is a Tooltip present and cancel it if there isn't. You can then set the tooltip during your validation then show it. In this example I set a timer to reset the tooltip text after a 2 second timeout.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
toolTip1.IsBalloon = true;
toolTip1.Popup += new PopupEventHandler(toolTip1_Popup);
toolTip1.SetToolTip(textBox1, "");
}
void toolTip1_Popup(object sender, PopupEventArgs e)
{
if (toolTip1.GetToolTip(e.AssociatedControl) == "")
e.Cancel = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop();
toolTip1.RemoveAll();
}
private void textBox1_Validating(object sender, CancelEventArgs e)
{
int temp;
if (!int.TryParse(textBox1.Text, out temp))
showTip("Validation Error", (Control)sender);
}
private void showTip(string message, Control destination)
{
toolTip1.Show(message, destination);
timer1.Start();
}
}
Much to my surprise it appears that toolTip1.IsOpen = true will show a tooltip and allow it to stay open. Note that you will need to provide code to close it because it wouldn't go away on its own on my machine no matter what I did.