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 Booking
{
    public partial class MainForm : Form
    {
        private double revenue = 0.0;
        private const int totalNumOfSeats = 240;
        private int numOfReservedSeats = 0;
        public MainForm()
        {
            InitializeComponent();
            InitializeGUI();

        }
        private void InitializeGUI()
        {
            rbtnReserve.Checked = true; 
            lstSeats.Items.Clear();
            txtName.Text = string.Empty;
        }
        private bool ReadAndValidateName(out string name)
        {
            name = "";
            if (txtName.Text.Length > 0)
            {
                name = txtName.Text;
                return true;
            }
           else
            {
           MessageBox.Show("Enter Letters Only", "Invalid Character", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtName.Focus();
                return false;
            }   
        }

        private bool ReadAndValidatePrice(out double price)
        {
            price = 0;
            double converted;
            converted = Convert.ToDouble(txtPrice.Text);

            if (converted >= 0.0)
            {
                price = Double.Parse(txtPrice.Text);
                return true;
            }
            else
            {
                MessageBox.Show("Enter Numbers Only", "Invalid Character", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtPrice.Focus();
                return false;
            }
        }
          
        private bool ReadAndValidateInput(out string name ,out double price)
        {
            return ReadAndValidateName(out name) & ReadAndValidatePrice(out price);
            
        }
        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }

        private void txtPrice_TextChanged(object sender, EventArgs e)
        {

        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            string costumerName = string.Empty;
            double seatPrice = 0.0;

            bool inputOk = ReadAndValidateInput(out costumerName, out seatPrice);

            if (inputOk)
            {
                numOfReservedSeats++;
                revenue += seatPrice;

            }
        }

        private void txtName_TextChanged(object sender, EventArgs e)
        {

        }

        private void lstSeats_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void btnOK_Click_1(object sender, EventArgs e)
        {

            lstSeats.Items.Add(string.Format("\t\t{0} \t{1}",txtPrice.Text, txtName.Text));
	       
        }

        private void txtPrice_TextChanged_1(object sender, EventArgs e)
        {

        }

        private void rbtnReserve_CheckedChanged(object sender, EventArgs e)
        {
            txtName.Enabled = true;
            txtPrice.Enabled = true;
            btnOK.Enabled = true;
        }

        private void rbtnCancel_CheckedChanged(object sender, EventArgs e)
        {
            txtName.Enabled = false;
            txtPrice.Enabled = false;
            btnOK.Enabled = false;
        }
 
    }
}

Hello im a really noob when it com to windows form so i need same help to understand this. How do i get private bool ReadAndValidateName(out string name) and price in to the textboxes. do i need to use samething in to the private void btnOK_Click(object sender, EventArgs e) to get it do show the error message and so on?

Don't really get what you mean by

How do i get private bool ReadAndValidateName(out string name) and price in to the textboxes

You also have two Click events for the same button?Line 84 and 109. Generated in the developer while doubleclicking twice on your OKbtn I guess.

when i click th butten the error message dosent com up ot the text from my 2 textboxes dosent go in to my listbox. so i wondering what is wrong

Edited 4 Years Ago by Johan__: n/a

You want to append your validated name to a textbox?
If I am right, txtName.Text already contains a name the user typed in, before he hits the OKbutton.
So you want to do something like : txtName.Text = txtName.Text = Name; ?

yes im going to enter a letter in to 1 textbox and if i dont have anything in it the error message going to com up and know it does nothing and thats way i have two two Click on the same butten so the name and price can com up on thelistbox

Let's follow your ReadAndValidateName method on line 29.
Say I type a "3" in the textbox. On Line 32 you test txtName.Text.Length > 0 this is true, because Length is 1 (I typed a 3 remember) So Name is set to "3" and true is returned and no error messagebox!
I think you better rewrite some of your code and not test on every key input, but test the whole textbox string in your OK button click handler.

Edited 4 Years Ago by ddanbe: typo

ok so its the problem whit my if and else becose it dont com any error massage if i dont put anything in my price 2

Normally use a Validating event, but for simplicity have a look at this:

private bool ValidateName(string name)
        {
            bool formatOK = true;

            if (name.Length > 0)
            {
                for (int i = 0; i < name.Length; i++)
                {
                    formatOK = char.IsLetter(name[i]);
                    if (!formatOK) break;
                }
                return formatOK;
            }
            else
            {
                MessageBox.Show("Enter Letters Only", "Invalid Character", MessageBoxButtons.OK, MessageBoxIcon.Error);                
                return false;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
           if (ValidateName(txtName.Text)) //. . . 
        }

Edited 4 Years Ago by ddanbe: n/a

This article has been dead for over six months. Start a new discussion instead.