Dzień dobry,
Mam problem przy obsłudze błędu w moim kalkulatorze BMI. Mianowicie kiedy wpisuję w textboxa ciąg literek, to chciałbym, żeby mój "program" dał sobie radę z tym i wyświetlił komunikat "błąd danych"
Nie za bardzo wiem jak zapisać w kodzie, że jeżeli w textboxa jest wpisywany string to wyświetl komunikat " błąd danych"
OTO MÓJ KOD:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CS_BMI_Calc
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void label3_Click(object sender, EventArgs e)
{
}
private void label5_Click(object sender, EventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
txtMasa.Clear();
txtWzrost.Clear();
txtBmi.Clear();
txtBmi.BackColor = Color.White;
lblwynik.Text = "";
lblwynik.BackColor = Color.White;
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void button1_Click(object sender, EventArgs e)
{
if (txtMasa.Text != "" && txtWzrost.Text != "" && Convert.ToDouble(txtMasa.Text) > 0 && Convert.ToDouble(txtWzrost.Text) > 0)
{
txtBmi.BackColor = Color.White;
double masa = Convert.ToDouble(txtMasa.Text);
double wzrost = Convert.ToDouble(txtWzrost.Text) / 100;
double BMI = masa / (wzrost * wzrost);
txtBmi.Text = String.Format("{0:f}",BMI);
if (BMI < 18)
{
lblwynik.Text = "Niedowaga !!!";
txtBmi.BackColor = Color.PowderBlue;
}
else if (BMI < 24.99)
{
lblwynik.Text = "Waga Prawidłowa";
txtBmi.BackColor = Color.Green;
}
else
{
lblwynik.Text = "NADWAGA !!!";
txtBmi.BackColor = Color.Red;
}
}
else lblwynik.Text = "Błąd danych";
}
private void lblwynik_Click(object sender, EventArgs e)
{
}
}
}