Delete a string using replace keyword [duplicate] - c#

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", "");

Related

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]+");
}

Detect time when button was clicked [duplicate]

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;
}

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

String takes any value in c# Visual Studio [duplicate]

This question already has answers here:
Can I escape a double quote in a verbatim string literal?
(6 answers)
Closed 7 years ago.
private void button1_Click(object sender, EventArgs e)
{
string answer = (#"class SecLevel \n { \n static void Main()\n{Console.WriteLine("Hello World!");}}");
if (textBox1.Text == answer)
{
MessageBox.Show( "Good!");
}
}
It is because of the "Hello World!" you need to escape the ",
do this instead:
"class SecLevel \\n { \\n static void Main()\\n{Console.WriteLine(\"Hello World!\");}}"

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