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
}
}
Related
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
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.");
}
I am new on C# so sorry if it look easy for some of you :)
I have this button click:
private void buttonSwitchCamera_Click(object sender, RoutedEventArgs e)
{
_blManager.SendTrackerCmd(TrackerCmdType.PrimaryAVT_ActiveSensor, (float)IR_Id.IR_1); // Switch to IR1
_blManager.SendTrackerCmd(TrackerCmdType.PrimaryAVT_ActiveSensor, (float)IR_Id.IR_2); // Switch to IR2
}
How can I switch between the two methods when clicking the button?
one click will be for method #1
second click will be for method #2
third click will be for method #1
etc..
You can do something like that:
bool executeMethodOne;
private void buttonSwitchCamera_Click(object sender, RoutedEventArgs e)
{
executeMethodOne = !executeMethodOne;
var IrId = executeMethodOne ? IR_Id.IR_1 : IR_Id.IR_2;
_blManager.SendTrackerCmd(TrackerCmdType.PrimaryAVT_ActiveSensor, (float)IrId);
}
Have a bool called executeMethodOne which you will invert everytime you click on the button. Depending on if it is true or false you can execute the first or the second method
First click is old:
private void OnButtonClickOdd(object sender, RoutedEventArgs e)
{
// Unsubscribe OnButtonClickOdd
button.Click -= OnButtonClickOdd;
// Subcribe to OnButtonClickEven
button.Click += OnButtonClickEven;
// Do your job here
}
private void OnButtonClickEven(object sender, RoutedEventArgs e)
{
// Unsubscribe OnButtonClickEven
button.Click -= OnButtonClickEven;
// Subcribe to OnButtonClickOdd
button.Click += OnButtonClickOdd;
// Do your job here
}
Other way: just use bool flag to know it odd or even click:
private bool odd = true;
private void buttonSwitchCamera_Click(object sender, RoutedEventArgs e)
{
if(old)
{
// Odd click job
}
else
{
// Even click job
}
old = !old;
}
In my project I would like to show message or call methods after 5 minutes for example, If the users didn't click on specific button, I wrote this code
Boolean flage = false;
private void button1_Click(object sender, EventArgs e)
{
Timer Clock;
Clock = new System.Windows.Forms.Timer();
Clock.Interval = 1000;
Clock.Start();
Clock.Tick += new EventHandler(Timer_Tick);
}
public void Timer_Tick(object sender, EventArgs eArgs)
{
if (flage == false)
{
MessageBox.Show("after period of time ");
}
}
private void button2_Click(object sender, EventArgs e)
{
flage = true;
}
Its keeping show the messageBox can any body help me.
Your Timer Clock variable is on the stack and ceases to exist when the function exits.
Try making it a member of the class.
I want to change the radiobutton location and make it move up while i am clicking button
tried this
private void up_MouseDown(object sender, MouseEventArgs e)
{
while(P.Location.Y>0)
P.Location = new System.Drawing.Point(P.Location.X, P.Location.Y - 1);
}
P is a radiobutton
I want it to keep moving up while I'm pressing, but it's just jumping up to the up of the form.
it's working good in debugging but it's really moving fast
I want to slow the movement of the radiobutton and make it visible
Actually you are starting a while loop that will not exit until your RadioButton is at the top of your Form wether you are still pressing the Button or not. You can slow it down by putting a Thread.Sleep in your loop that way it is slowed down visible.
private void up_MouseDown(object sender, MouseEventArgs e)
{
while (P.Location.Y > 0)
{
P.Location = new System.Drawing.Point(P.Location.X, P.Location.Y - 1);
System.Threading.Thread.Sleep(10);
}
}
If you want to have better control I would use a Timer. In this example the Interval is set to 10.
private void up_MouseDown(object sender, MouseEventArgs e)
{
timer1.Start();
}
private void up_MouseUp(object sender, MouseEventArgs e)
{
timer1.Stop();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (P.Location.Y > 0)
{
P.Location = new System.Drawing.Point(P.Location.X, P.Location.Y - 1);
}
}
You can use timer. Add a timer from the toolbox, say its name was timer1, then add following method:
private void P_MouseUp(object sender, MouseEventArgs e) {
timer1.Enabled=false;
}
private void P_MouseDown(object sender, MouseEventArgs e) {
timer1.Enabled=true;
}
private void timer1_Tick(object sender, EventArgs e) {
if(P.Location.Y>0)
P.Location=new System.Drawing.Point(P.Location.X, P.Location.Y-1);
}
You can change the interval of timer1 in properties window. I guess you write this for fun; so, have fun!