[C#]Program obliczający pole figur (MS Visual Studio)

0

Witam,
swoją przygodę z C# dopiero co zaczynam. Na samym początku napisałem prosty program obliczający pole kwadratu.
Mój problem pojawia się w momencie kiedy użytkownik w textBox'ie wpisze zamiast cyfr litery, w takim wypadku program zostaje zamknięty. Chciałbym uzyskać efekt wyświetlenia w polu z wynikiem informacji o zaistniałym błędzie. Domyślam się, że będzie tutaj potrzebny if, jednak nie mam pomysłu jak to zrobić.
Z góry dziękuję za pomoc i pozdrawiam.

0

Lub poczytaj o mechanizmach wyjątków.

0
if (txbX1.TextLength > 0 && txbX2.TextLength > 0)
{
    double x, y;
    try
    {
        x = Double.Parse(txbX1.Text);
        y = Double.Parse(txbX2.Text);
    }
    catch (FormatException)
    {
        return; // tutaj możesz wpisać ten komunikat, nie musi być return
    }
    finally
    {
        txbX1.Clear();
        txbX2.Clear();
    }
}
0

Mój obecny kod wygląda następująco:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Program_obliczający_pole_kwadratu
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Double a = Double.Parse(textBox1.Text);
            if (a > 0)
            {
                Double Pole = a * a;
                Double obw = a * 4;
                label2.Text = "Pole kwadratu o boku " + textBox1.Text + " wynosi " + Pole.ToString() + ", a obwód jest równy " + obw.ToString();
            }
            else if (a == 0)
            {
                label2.Text = "Nie ma kwadratu o takim boku!";
            }
            else if (a < 0)
            {
                label2.Text = "Jako długość boku kwadratu podaj liczbę naturalną!";
            }

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }
    }
}
0
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Program_obliczający_pole_kwadratu
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
               Double a = Double.Parse(textBox1.Text);
               if (a > 0)
               {
                   Double Pole = a * a;
                   Double obw = a * 4;
                   label2.Text = "Pole kwadratu o boku " + textBox1.Text + " wynosi " + Pole.ToString() + ", a obwód jest równy " + obw.ToString();
               }
               else if (a == 0)
               {
                   label2.Text = "Nie ma kwadratu o takim boku!";
               }
               else if (a < 0)
               {
                   label2.Text = "Jako długość boku kwadratu podaj liczbę naturalną!";
               }
            }
            catch(FormatException e)
            {
                 label2.Text="Podano złą liczbę!. Wyjątek " + e.Message;
            }
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }
    }
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1