private void CalculateBMIButton_Click(object sender, EventArgs e) { int height = 0; float weight = 0.0f; float bmi = 0.0f; try { height = int.Parse(HeightTextBox.Text); } catch { MessageBox.Show("拜託請輸入身高!"); HeightTextBox.Text = ""; //TextBox清空 HeightTextBox.Focus(); //游標位置 return; } try { weight = float.Parse(WeightTextBox.Text); } catch { MessageBox.Show("拜託請輸入體重!"); WeightTextBox.Text = ""; //TextBox清空 WeightTextBox.Focus(); //游標位置 return; } bmi = weight / ((height / 100.0f) * (height / 100.0f)); MessageBox.Show(string.Format("BMI:{0}", bmi.ToString("#.##"))); }
private void CalculateBMIButton_Click(object sender, EventArgs e) { double bmi = 0.0; double height = 0.0; double weight = 0.0; try { height = double.Parse(HeightTextBox.Text); } catch { MessageBox.Show("好好輸入身高可以嗎?"); HeightTextBox.Text = string.Empty; HeightTextBox.Focus(); return; } try { weight = double.Parse(WeightTextBox.Text); } catch { MessageBox.Show("好好輸入體重可以嗎?"); WeightTextBox.Text = string.Empty; WeightTextBox.Focus(); return; } height = height / 100.0; bmi = weight / (height * height); MessageBox.Show(string.Format("BMI: {0}", bmi.ToString("#.##"))); }
private bool ValidateForm() { if (!Regex.Match(TitleTextBox.Text, @"^\D{1,20}$").Success) { MessageBox.Show("Title must consist of at least 1 character and not exceed 20 characters!"); TitleTextBox.Focus(); return(false); } if (!Regex.Match(DescriptionTextBox.Text, @"^\D{1,200}$").Success) { MessageBox.Show("Description must consist of at least 1 character and not exceed 200 characters!"); DescriptionTextBox.Focus(); return(false); } if (CategoryComboBox.SelectedItem == null) { MessageBox.Show("Please select category"); return(false); } if (ProducerComboBox.SelectedItem == null) { MessageBox.Show("Please select categoty"); return(false); } if (!Regex.Match(WeightTextBox.Text, @"^[0-9]*(?:\,[0-9]*)?$").Success) { MessageBox.Show("Invalid weight! Check the data you've entered!"); WeightTextBox.Focus(); return(false); } if (!Regex.Match(ComponentsTextBox.Text, @"^\D{1,200}$").Success) { MessageBox.Show("Components must consist of at least 1 character and not exceed 200 characters!"); ComponentsTextBox.Focus(); return(false); } if (!Regex.Match(PriceTextBox.Text, @"^[0-9]*(?:\,[0-9]*)?$").Success) { MessageBox.Show("Invalid price! Check the data you've entered!"); PriceTextBox.Focus(); return(false); } return(true); }
private void CalculateBMIButton_Click(object sender, EventArgs e) { int height = 0; float weight = 0.0f; float bmi = 0.0f; try { height = int.Parse(HeightTextBox.Text); } catch { MessageBox.Show("請輸入身高!!"); HeightTextBox.Text = ""; HeightTextBox.Focus(); return; } try { weight = float.Parse(WeightTextBox.Text); } catch { MessageBox.Show("請輸入體重!!"); WeightTextBox.Text = ""; WeightTextBox.Focus(); return; } bmi = weight / ((height / 100.0f) * (height / 100.0f)); //TODO 如果bmi大於24 或 bmi 小於 18 不健康 //TODO 顯示不健康的提示 //TODO 不然顯示健康的提示 if (bmi >= 18.5 && bmi <= 24.9) { MessageBox.Show(string.Format("BMI:{0}", bmi.ToString("#.##"))); BmiLabel.Text = ("BMI:" + bmi.ToString("#.##")); StatusLabel.Text = ("健康狀態:非常健康"); } else { MessageBox.Show(string.Format("BMI:{0}", bmi.ToString("#.##"))); BmiLabel.Text = ("BMI:" + bmi.ToString("#.##")); StatusLabel.Text = ("健康狀態:不健康唷"); } }
private void AddToWorkoutButton_Click(object sender, EventArgs e) { if (ExerciseBoxesFilled()) { var exercise = new Exercise { Name = ExerciseNameTextBox.Text, Reps = RepsTextBox.Text, Weight = WeightTextBox.Text, SetTime = (int)SetTimeNumeric.Value, RestTime = (int)RestTimeNumeric.Value }; ExerciseListBox.Items.Add($"{exercise.Name} - {exercise.Weight} X {exercise.Reps} (ST: {exercise.SetTime}) (RT: {exercise.RestTime})"); _workout.Add(exercise); WeightTextBox.Focus(); } }
private void BmiButton_Click(object sender, EventArgs e) { float height = 0.0f; float weight = 0.0f; float bmi = 0.0f; try { height = float.Parse(HeightTextBox.Text); } catch { BmiStatusLabel.Text = "請輸入身高!"; BmiStatusLabel.ForeColor = Color.Blue; HeightTextBox.Text = ""; //TextBox清空 HeightTextBox.Focus(); //游標位置 return; } try { weight = float.Parse(WeightTextBox.Text); } catch { BmiStatusLabel.Text = "請輸入體重!"; BmiStatusLabel.ForeColor = Color.Blue; WeightTextBox.Text = ""; //TextBox清空 WeightTextBox.Focus(); //游標位置 return; } bmi = weight / ((height / 100.0f) * (height / 100.0f)); if (bmi > 24 || bmi < 18) { BmiStatusLabel.Text = "BMI = " + bmi.ToString("##.##") + ",BMI異常,請注意健康"; BmiStatusLabel.ForeColor = Color.Red; } else { BmiStatusLabel.Text = "BMI = " + bmi.ToString("##.##") + ",BMI正常"; BmiStatusLabel.ForeColor = Color.Green; } }
private void CalculateBMIButton_Click(object sender, EventArgs e) { // Declare local variables to store input and calculated values double height = 0; double weight = 0; double bmi = 0; // Determine height is not a numeric value if (!double.TryParse(HeightTextBox.Text, out height)) { MessageBox.Show("Height must be a numeric value", "Invalid Input!"); HeightTextBox.Focus(); } // Determine weight is not a numeric value else if (!double.TryParse(WeightTextBox.Text, out weight)) { MessageBox.Show("Weight must be a numeric value", "Invalid Input!"); WeightTextBox.Focus(); } else // If both inputs are OK then calculate the BMI { // Determine which measurement units has selected if (ImperialRadioButton.Checked) { // If imperial unit has selected bmi = (weight * 703) / (height * height); } else { // If metric unit has selected bmi = weight / (height * height); } // Display calculated BMI BmiTextBox.Text = bmi.ToString("N2"); // Call DisplayResult() method to display BMI status and update progress bar DisplayResult(bmi); } }
private void ProductComboBox_KeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Return) { try { foodlist.Where(p => p.FoodName.Contains(ProductComboBox.SelectedValue.ToString())).ToList(); WeightTextBox.Focus(); e.Handled = true; return; } catch (NullReferenceException) { MessageBox.Show("Продукт не найден!"); ProductComboBox.SelectedValue = null; e.Handled = true; return; } } else { return; } }
/** * <summary> * This eventhandler calculate the BMI and display on the textboxes * </summary> * * @method CalculateButton_Click * @param {object} sender * @param {EventArgs} e */ private void CalculateButton_Click(object sender, EventArgs e) { // local variables double height = 0, weight = 0, BMI; try { height = Convert.ToDouble(HeightTextBox.Text); weight = Convert.ToDouble(WeightTextBox.Text); } catch (FormatException) { MessageBox.Show("Fill out the forms"); } if (HeightTextBox.Text == "" || WeightTextBox.Text == "") { HeightTextBox.Focus(); HeightTextBox.SelectAll(); WeightTextBox.Focus(); WeightTextBox.SelectAll(); } else if (height < 0 || weight < 0) { MessageBox.Show("Please provide positive values"); } else if (height > 0 && weight > 0) { // in case the input is in imperial if (ImperialButton.Checked == true) { // calculate BMI in imperial BMI = (weight * 703) / (height * height); // condition to set progress bar value if (((BMI / 30) * 100) > 100) { BMIProgressBar.Value = 100; } else { BMIProgressBar.Value = ((int)((BMI / 30) * 100)); } BMIResultBox.Text = string.Format("{0:f1}", BMI); BMIResultBox.BackColor = Color.LightSalmon; BMIScaleBox.BackColor = Color.LightSalmon; // determine the BMI if (BMI < 18.5) { this.BMIScaleBox.Text = ("Underweight"); BMIProgressBar.ForeColor = Color.Yellow; } else if (BMI >= 18.5 && BMI <= 24.9) { this.BMIScaleBox.Text = ("Normal"); BMIProgressBar.ForeColor = Color.Green; } else if (BMI >= 25 && BMI <= 29.9) { this.BMIScaleBox.Text = ("Overweight"); BMIProgressBar.ForeColor = Color.DarkGreen; } else if (BMI >= 30) { this.BMIScaleBox.Text = ("Obese"); BMIProgressBar.ForeColor = Color.Red; } } // in case the input is in metric units else if (MetricUnitsButton.Checked == true) { // calculate BMI in metric units BMI = weight / ((height * height) / 10000); // condition to set progress bar value if (((BMI / 30) * 100) > 100) { BMIProgressBar.Value = 100; } else { BMIProgressBar.Value = ((int)((BMI / 30) * 100)); } BMIResultBox.Text = string.Format("{0:f1}", BMI); BMIResultBox.BackColor = Color.LightSalmon; BMIScaleBox.BackColor = Color.LightSalmon; BMIProgressBar.Maximum = 100; // determine the BMI if (BMI < 18.5) { this.BMIScaleBox.Text = ("Underweight"); BMIProgressBar.ForeColor = Color.Yellow; } else if (BMI >= 18.5 && BMI <= 24.9) { this.BMIScaleBox.Text = ("Normal"); BMIProgressBar.ForeColor = Color.Green; } else if (BMI >= 25 && BMI <= 29.9) { this.BMIScaleBox.Text = ("Overweight"); BMIProgressBar.ForeColor = Color.DarkGreen; } else if (BMI >= 30) { this.BMIScaleBox.Text = ("Obese"); BMIProgressBar.ForeColor = Color.Red; } } } }
private void CalculateBMI_Click(object sender, EventArgs e) { //initialize variables double result; double height; double weight; try { if (HeightTextBox.Text == "" || WeightTextBox.Text == "") { MessageBox.Show("Fields can not be empty, please fill them out"); if ((HeightTextBox.Text == "") || (HeightTextBox.Text == "" && WeightTextBox.Text == "")) { HeightTextBox.Focus(); } else { WeightTextBox.Focus(); } } try { height = Convert.ToDouble(HeightTextBox.Text); weight = Convert.ToDouble(WeightTextBox.Text); } catch (Exception) { MessageBox.Show("Please type numbers"); HeightTextBox.Clear(); WeightTextBox.Clear(); HeightTextBox.Focus(); } height = Convert.ToDouble(HeightTextBox.Text); weight = Convert.ToDouble(WeightTextBox.Text); if (imperialRadBtn.Checked == true) { result = ((weight * 703)) / ((height * height)); ResultTextBox.Text = Convert.ToString(result); if (result < 18.5) { ResultInfoTextBox.Text = Convert.ToString("Underweight"); } if ((result > 18.5) && (result < 24.9)) { ResultInfoTextBox.Text = Convert.ToString("Normal"); } if ((result > 25) && (result < 29.9)) { ResultInfoTextBox.Text = Convert.ToString("Overweight"); } if (result > 30) { ResultInfoTextBox.Text = Convert.ToString("Obese"); } } else if (metricRadBnt.Checked == true) { result = (weight / (height * height)); ResultTextBox.Text = Convert.ToString(result); if (result < 18.5) { ResultInfoTextBox.Text = Convert.ToString("Underweight"); } if ((result > 18.5) && (result < 24.9)) { ResultInfoTextBox.Text = Convert.ToString("Normal"); } if ((result > 25) && (result < 29.9)) { ResultInfoTextBox.Text = Convert.ToString("Overweight"); } if (result > 30) { ResultInfoTextBox.Text = Convert.ToString("Obese"); } } } catch (Exception) { MessageBox.Show("Press OK"); } }