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!\");}}"
Related
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", "");
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]+");
}
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;
}
This question already has answers here:
C# - Textbox Newline Problems
(6 answers)
Closed 7 years ago.
I am trying to write a string in a C# textBox by pressing a button. When I press the button my string should be added in the textBox. However, the string is added, but the "\n" is completely ignored. How can I allow it?
This is a very simple example that I have written only to understand where the problem is, but I noticed that the same thing happens with white spaces when I am trying to redirect my console output to a textBox.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
String s = "first line";
s += "\n";
s += "second line";
Console.Out.WriteLine(s);
textBox1.AppendText(s);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
how i call it:
class Program
{
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
Use \r\n or Environment.NewLine
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");
}