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
I want to know how I can validate a phone number in a textbox using this code:
System.Text.RegularExpressions.Regex rphone = new System.Text.RegularExpressions.Regex(#"^((\+){0,1}91(\s){0,1}(\-){0,1}(\s){0,1}){0,1}9[0-9](\s){0,1}(\-){0,1}(\s){0,1}[1-9]{1}[0-9]{7}$");
try:
\+[0-9]{3}\s+[0-9]{3}\s+[0-9]{5}\s+[0-9]{3}
heres a little explaination of pattern above .
\+ -------------|> +sign
[0-9]{3} -------|> matches 0-9 max of 3digit
\s+ ------------|> a whitespace
matches with your sample:
+020 111 94546 333
if you want to validate if the number was completed lets say you got a button then textbox do something like:
private void button1_Click(object sender, EventArgs e)
{
Regex phoneNumpattern = new Regex(#"\+[0-9]{3}\s+[0-9]{3}\s+[0-9]{5}\s+[0-9]{3}");
if (phoneNumpattern.IsMatch(textBox1.Text))
{
MessageBox.Show("OK");
}
else
{
MessageBox.Show("Invalid phone number");
}
}
private void myTextBox_Leave(object sender, EventArgs e)
{
Regex pattern = new Regex(#"^((\+){0,1}91(\s){0,1}(\-){0,1}(\s){0,1}){0,1}9[0-9](\s){0,1}(\-){0,1}(\s){0,1}[1-9]{1}[0-9]{7}$");
if (pattern.IsMatch(myTextBox.Text))
{
MessageBox.Show("OK");
}
else
{
MessageBox.Show("Invalid phone number");
}
}
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 days ago.
Improve this question
I got this time difference calculator and I don't like the DateTimePicker because when I have written the hour's it doesn't automatically jump over to minutes so I would like to change them to texbox, is that possible?
The problem Im having is that I cant use textBox.Value so what do I do?
private void button1_Click(object sender, EventArgs e)
{
TimeSpan result = this.dateTimePicker2.Value - this.dateTimePicker1.Value;
this.textBox1.Text = result.ToString();
string s = this.textBox1.Text;
string[] temparry = textBox1.Text.Split ('.');
textBox1.Text = temparry[0];
}
I would like it to be something more like this
private void button1_Click(object sender, EventArgs e)
{
TimeSpan result = this.texbox2.Value - this.texbox3.Value;
this.textBox1.Text = result.ToString();
string s = this.textBox1.Text;
string[] temparry = textBox1.Text.Split ('.');
textBox1.Text = temparry[0];
}
But it dosnt work
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 days ago.
Improve this question
In the text box, when I enter a number, the first procedure is executed, and when I enter letters, the second procedure is executed.
private void Button1_Click(object sender, EventArgs e)
{
{
if (textBox1.Text.Contains(letters))
dataGridView1.DataSource = DB.pro1(textBox1.Text, Convert.ToInt32(this.textBox1.Text));
else
dataGridView1.DataSource = DB.pro2(textBox1.Text, Convert.ToInt32(this.textBox1.Text));
}
}
private void Button1_Click(object sender, EventArgs e)
{
int number;
bool isNumeric = int.TryParse(textBox1.Text, out number);
if (isNumeric)
{
dataGridView1.DataSource = DB.pro1(textBox1.Text, number);
}
else
{
dataGridView1.DataSource = DB.pro2(textBox1.Text, number);
}
}
The int.TryParse method is used to attempt to parse the input in textBox1 as an integer. If the parsing is successful, it returns true and the result of the parsing is stored in the number variable. If the parsing fails, it returns false.
Based on the result of the TryParse method, the appropriate method (pro1 or pro2) is called and the data source for dataGridView1 is set accordingly.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to do a filtering algorithm with C#, it will be a textbox and I will enter the forbidden words there. If banned words are found in the text I enter in richtextbox later, the word will be deleted as soon as it is written. How can I do this?
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
string[] badWords = textBox1.Text.Split(',');
string[] myText = richTextBox1.Text.Split(',');
foreach (var badWord in badWords)
{
if (myText.Contains(badWord))
{
richTextBox1.Text.Replace(badWord, "");
}
}
}
C# strings are immutable, you need to assign the Text property of your RichTextBox:
if (myText.Contains(badWord))
{
richTextBox1.Text = richTextBox1.Text.Replace(badWord, "");
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
first of all sorry for my broken language. i would like to create a calculator that shows the different between 2 numbers in C#
i tried to make it but i always end up with adding the textbox 1 to the textbox 2 and displaying the total in text box 3, which i don't want to do
please see the GUI , it's simple
i also would like to make the number in green color if the different is positive and red if the different is negative. see the picture
thanks!
simple gui
Write this code in the click event of calculate button
private void Calculate_Click(object sender, EventArgs e)
{
if (textBox1.Text != "" && textBox2.Text != "")
{
int diff = (Convert.ToInt32(textBox1.Text) - Convert.ToInt32(textBox2.Text));
textBox3.Text = diff.ToString();
if (diff >= 0)
{
textBox3.ForeColor = Color.Green;
}
else
textBox3.ForeColor = Color.Red;
}
else
{
MessageBox.Show("PLZ Enter the balances");
}
}
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;
}