Detect time when button was clicked [duplicate] - c#

This question already has answers here:
How can I get the current time in C#? [closed]
(4 answers)
Closed 6 years ago.
I have a form which I need to show the exact time when my button was clicked. How may I "capture" the exact time (hh:mm:ss) when I pressed it?
(please, if the question is duplicated, inform me so I can delete this one)

Use DateTime.Now:
void myButton_Click(object sender, RoutedEventArgs e)
{
// Captures the current time in a DateTime object.
DateTime currentTime = DateTime.Now;
// If you need a string, you can use the ToString method.
string timeString = currentTime.ToString("hh:mm:ss");
}

Use DateTime.Now in your button clicked handler:
protected void btn_Clicked(object sender, EventArgs e)
{
DateTime whenClicked = DateTime.Now;
}

Related

Delete a string using replace keyword [duplicate]

This question already has answers here:
C# string replace does not actually replace the value in the string [duplicate]
(3 answers)
Closed 5 years ago.
I have a problem with String.Replace method. It is not working for buttonclick events.
i use this code
private void button4_Click(object sender, EventArgs e)
{
textBox3.Text.Replace("apple","");
}
When I press clear button it doesn't remove "apple" words
The replace function does not change the control, it returns a new string.
If textBox3 says "applebananacherry"
String text = textBox3.Text.Replace("apple","");
would leave text with "bananacherry"
What you need is
textBox3.Text = textBox3.Text.Replace("apple", "");

C# WPF How to use only numeric value in a textbox [duplicate]

This question already has answers here:
How do I get a TextBox to only accept numeric input in WPF?
(33 answers)
Closed 5 years ago.
I use a WPF application I would like to allow only numbers in a textbox. In a WindowsForm application I would know how I would have to make it.
Unfortunately, there is a KeyPress not in WPF and the "code" functioned also no longer.
How is it right and what is the event?
Here you can see the old code that has worked in Windows Form:
private void tbKreisdurchmesser_KeyPress(object sender, KeyEventArgs e)
{
if (char.IsNumber(e.Key) || e.KeyChar==".")
{
}
else
{
e.Handled = e.KeyChar != (char)Keys.Back;
}
}
You can add Previewtextinput event for textbox and validate the value inside that event using Regex,
private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
var textBox = sender as TextBox;
e.Handled = Regex.IsMatch(e.Text, "[^0-9]+");
}

Functions for C# dynamic item [duplicate]

This question already has answers here:
How can I create a dynamic button click event on a dynamic button?
(6 answers)
Closed 7 years ago.
I wonder whether it is possible to create functions for dynamic items (textboxes, buttons) like
private void Button_Click(object sender, EventArgs e)
private void TextBox_TextChanged(object sender, EventArgs e)
and other?
If yes, then how?
I know you can use DynamicalButton.Click(); but that doesn't help much.
EDIT
As example, you can make a function like Button_Click or TextBox_TextChanged, PictureBox_MouseOver easily when you make the objects with designer and they work properly. How to do that with dynamic objects?
Found the answer
DynamicalObject.Click += new EventHandler(DynamicalObject_Click);
protected void DynamicalObject_Click (object sender, EventArgs e)
{
what happens on the event
}
as example

How to measure time of a key is pressed in c# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
In the TextEdit a user types a word 'hello', i want to now how much time did he taked to write the word 'hello'.If possible i want to know how much time did he taked to type each character. For example the person taked 3 seconds to type the word hello, and it took him 1s to type 'h' and 2s to press 'e'.
-Thanks
If talking about Windows Forms, you can use Control.Leave and Control.Enter events. Something like this.
And you can use TextChanged to see how long it takes for the text to be changed.
DateTime StartTime;
DateTime EndTime;
private void textBox1_Enter(object sender, EventArgs e)
{
MessageBox.Show("Entered");
StartTime = DateTime.Now;
}
private void textBox1_Leave(object sender, EventArgs e)
{
MessageBox.Show("Left");
EndTime = DateTime.Now;
MessageBox.Show("Time in control: " + EndTime.Subtract(StartTime).ToString());
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
MessageBox.Show("Changed at : " + DateTime.Now.Subtract(StartTime).ToString());
StartTime = DateTime.Now;
}

MonthCalender to textbox [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to use MonthCalender to insert date in text box?
I have textBox1 and MonthCalender1 How I can make the selected date in the calender mapped as a string in textBox1?
try this in selected change event of calender :
protected void MonthCalender1 _SelectionChanged(object sender, EventArgs e)
{
System.DateTime myDate = MonthCalender1 .SelectedDate;
textBox1.Text = myDate.ToString ("dd/MM/yyyy");
}

Categories