4 Replies - 420 Views - Last Post: 28 November 2013 - 08:15 AM Rate Topic: -----

#1 dionsyie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 27-November 13

Help in putting error messages

Posted 27 November 2013 - 06:28 PM

Hi I am starting to learn about C#, and i create calculator but i have problem to make catch error messages, what i would like to is to make catch for every possibly scenario, like if user leaves one field empty (there is two textbxox) to make error message which textbox is emtpy, if is letter inside textbox and not number, if operation is not selected or if number is divided by zero and etc.
my code is here. please help.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Calculator
{
    /// <summary>
    /// Interaction logic for Mainwindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e) 
        {
            try
            {
                double firstOperand = double.Parse(textBox1.Text); 
                double secondOperand = double.Parse(textBox2.Text); 
                double total;

                total = 0;

                switch (comboBox1.Text)
                {
                    case "+":
                        total = firstOperand  + secondOperand;
                        break;

                    case "-":
                        total = firstOperand  - secondOperand;
                        break;

                    case "*":
                        total = firstOperand  * secondOperand;
                        break;

                    case "/":
                        total = firstOperand  / secondOperand;
                        break;
                }
                result.Content = total.ToString();
            }
            catch 
        }
    }
}

This post has been edited by jimblumberg: 27 November 2013 - 06:29 PM
Reason for edit:: Added missing code tags, please learn to use them properly.


Is This A Good Question/Topic? 0
  • +

Replies To: Help in putting error messages

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9497
  • View blog
  • Posts: 35,844
  • Joined: 12-June 08

Re: Help in putting error messages

Posted 27 November 2013 - 06:47 PM

Make a function that does the checking.. if there is something amiss, throw up a warning box, use an 'error provider' object, or what ever.. have the calling event/method stop processing if there are errors (aka make the 'validate' a bool function).
Was This Post Helpful? 0
  • +
  • -

#3 astonecipher  Icon User is offline

  • D.I.C.
  • member icon

Reputation: 768
  • View blog
  • Posts: 3,356
  • Joined: 03-December 12

Re: Help in putting error messages

Posted 27 November 2013 - 06:59 PM

Catching errors is different than validating input. It sounds like you want to validate what comes in.


Example:
string text = textBox1.Text;
int num;
bool isNumeric = Int32.TryParse(text, out num);

   if (!isNumeric){
         MessageBox.Show ("Enter a valid number", "Error", 
                           MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
   }

Was This Post Helpful? 1
  • +
  • -

#4 dionsyie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 27-November 13

Re: Help in putting error messages

Posted 28 November 2013 - 08:09 AM

View Postastonecipher, on 27 November 2013 - 06:59 PM, said:

Catching errors is different than validating input. It sounds like you want to validate what comes in.


Example:
string text = textBox1.Text;
int num;
bool isNumeric = Int32.TryParse(text, out num);

   if (!isNumeric){
         MessageBox.Show ("Enter a valid number", "Error", 
                           MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
   }

Thank you, astonecipher. This help out, with this code it is giving me giving me error message if user doesn't put number or letter in on of the text boxes. Can I also ask you how would you put code in in case if user is trying to divide by zero or if operations (+,-,*,/) is not selected from combobox?
Was This Post Helpful? 0
  • +
  • -

#5 astonecipher  Icon User is offline

  • D.I.C.
  • member icon

Reputation: 768
  • View blog
  • Posts: 3,356
  • Joined: 03-December 12

Re: Help in putting error messages

Posted 28 November 2013 - 08:15 AM

On the first question. Test if it is division by 0. If it is show a message if not it passes through.

The second part is easier because it is a combo box. With those you can just specify a default index.

This post has been edited by astonecipher: 28 November 2013 - 12:55 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1