c# : Condition for button [closed] - c#

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.

Related

Calculate time difference with TextBox? [closed]

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

How do i get my check boxes to display and add numbers into a label on C# [closed]

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 years ago.
Improve this question
Im trying to develop a form for an imaginary pet clinic in Visual Studio using C# and it consists of check boxes and a label. The checkboxes represent a service the clinic performs and when checked the price of the service is supposed to be displayed in the label, whenever any other boxes are checked theyre supposed to add to the price and when unchecked take away from the price. I am stuck on how i get the check boxes to have a value to display to the label
for example: the vaccinations checkbox (chkVaccinations) is supposed to cost 35 dollars
How do i get the check box to have a value of 35 and when checked to add to the label value which is zero?
private void chkVaccinations_CheckedChanged(object sender, EventArgs e) {
if (chkVaccinations.Checked)
{
}
else
{
}
private void lblTotalprice_Click(object sender, EventArgs e)
{
}
Tbh your question lacks alot of information to be able to help you, but here is an idea of a solution following your example assuming this is codebehind. If you want a full MVVM solution I can update the answer for that.
EDIT: Made some changed to adapt the answer to your latest request in the comments
private double TotalPrice { get; set; }
private void ComputeTotalPrice(double increment)
{
TotalPrice += increment;
}
private void chkVaccinations_CheckedChanged(object sender, EventArgs e)
{
if (chkVaccinations.Checked)
{
ComputeTotalPrice(priceAssociatedWithThisCheckBox);
}
else
{
ComputeTotalPrice(-priceAssociatedWithThisCheckBox);
}
UpdateLabel();
}
private void UpdateLabel()
{
If you want to show $ with 2 decimals
string formattedPrice = string.Format("{0:0.00}", TotalPrice).Replace(".00","");
lblTotalprice.Text= $"{formattedPrice}$";
}

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

Validate Phone number textbox [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
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");
}
}

How do i make a number appear in a text box after a button is pressed? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am new to this website and fairly new to programming.
I am making a calculator in C# and I would like to know how I would make a number appear in a text box after the button for it is pressed.
Thanks!
You need to add code to the button event handler:
public void Button1_Click(Object sender, EventArgs e)
{
TextBox1.Text = "1";
}
Which would display the string "1" into the Textbox.
You could also have multiple textboxes on the webpage and use:
public void Button1_Click(Object sender, EventArgs e)
{
string input1 = txtInput.Text;
string input2 = txtInput2.Text;
int userInput;
int userInput2;
int result;
Int32.TryParse(input1, out userInput);
Int32.TryParse(input2, out userInput2);
result = userInput + userInput2;
txtAnswer.Text = "The answer is: " result.ToString();
}
I have included a TryParse example which demonstrates if the conversion from the strings to integer was successful.

Categories