CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: listbox help

  1. #1
    Join Date
    Feb 2012
    Posts
    3

    listbox help

    Code:
    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 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 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;
            }
        }
    }
    I have two textboxes and one for name and one for price. when i enter on it its going in to a listbox but when i dont enter any letter in to textbox 1 a error massage is going to com up and the same for textbox 2 and the same is nothing is in both. i know i add the information in to the list box like this lstSeats.Items.Add(string.Format("\t\t{0} \t{1}", txtPrice.Text, txtName.Text));
    but if the textbox are blank it get it to the listbox but no enter when i press the butten. What do i do wrong?
    Last edited by DataMiser; February 28th, 2012 at 10:11 AM. Reason: added code tags

  2. #2
    Join Date
    Dec 2008
    Posts
    144

    Re: listbox help

    The code you provided only shows lstSeats once in the initializeGUI method, and you're just clearing the list then. Maybe I missed it, but I didn't see anywhere where you were adding items to the listbox.
    Code:
    if (Issue.Resolved)
    {
         ThreadTools.Click();
         MarkThreadResolved();
    }

  3. #3
    Join Date
    Jun 2011
    Location
    Buenos Aires, Argentina
    Posts
    130

    Re: listbox help

    I'm guessing you have some sort of "GO" button or something... Use it's clicked event handler to check if the TextBox's Text.Length is 0, and if so, show an error message saying that the user has to input something.

    For your numbers only TextBox, you could use a maskedTextBox to avoid having to validate user input. The user will only be allowed to enter numbers (provided you set <mask> atribute ok). Check MSDN on that.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured