7 Replies - 429 Views - Last Post: 01 May 2019 - 06:39 PM Rate Topic: -----

#1 Savageoncabbage   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 24-April 19

Need help with some things on this application

Posted 30 April 2019 - 09:28 AM

So I need help with a few things. I need help with putting what is selected from the combobox to the respecting listboxes. I also need to figure out how to keep that only numbers are input in the textboxes for Numofpeople and TipsTotal but let them be able to delete it. I also need help with how to output what the totals are to the texboxes as "$0.00" format.

Here is what I have so far:
public partial class ResturauntMenu : Form
    {
        double taxrte = 0.08;
        double tip;
        double taxes;
        double billAmt = 0.00;
        int bev, appet, main, des, numberPeople;

        public ResturauntMenu()
        {
            InitializeComponent();
            this.TipsTotal.KeyPress += new KeyPressEventHandler(TipsTotal_KeyPress);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'restaurantMenuDataSet.Menu' table. You can move, or remove it, as needed.
            this.menuTableAdapter.Fill(this.restaurantMenuDataSet.Menu);
            SubtotalPrice.Text = "$0.00";
            TaxPrice.Text = "$0.00";
            TipsTotal.Text = "$0.00";
            TotalPrice.Text = "$0.00";
        }

        private void NumofPeople_KeyPressed(object sender, KeyPressEventArgs e)
        {
            NumofPeople.Enabled = true;
            e.Handled = !char.IsDigit(e.KeyChar);
        }

        private void AppetizersOrdered_Click(object sender, EventArgs e)
        {

        }

        private void Subtotal_Click(object sender, EventArgs e)
        {

        }

        private void BeverageItems_SelectedIndexChanged(object sender, EventArgs e)
        {
            bev = BeverageItems.SelectedIndex;
            switch (bev)
            {
                case 0:
                    billAmt = billAmt + 1.95;
                    break;

                case 1:
                    billAmt = billAmt + 1.50;
                    break;

                case 2:
                    billAmt = billAmt + 1.25;
                    break;

                case 3:
                    billAmt = billAmt + 2.50;
                    break;

                case 4:
                    billAmt = billAmt + 1.50;
                    break;
            }
            Bevorder.Text = billAmt.ToString();
        }

        private void AppetizerItems_SelectedIndexChanged(object sender, EventArgs e)
        {
            appet = AppetizerItems.SelectedIndex;

            switch (appet)
            {
                case 0:
                    billAmt = billAmt + 5.95;
                    break;

                case 1:
                    billAmt = billAmt + 8.95;
                    break;

                case 2:
                    billAmt = billAmt + 7.95;
                    break;

                case 3:
                    billAmt = billAmt + 3.95;
                    break;

            }
            AppetizerItems.Text = billAmt.ToString();
        }

        private void MainCourseItems_SelectedIndexChanged(object sender, EventArgs e)
        {
            main = MainCourseItems.SelectedIndex;

            switch (main)
            {
                case 0:
                    billAmt = billAmt + 15.95;
                    break;

                case 1:
                    billAmt = billAmt + 11.95;
                    break;

                case 2:
                    billAmt = billAmt + 18.95;
                    break;

                case 3:
                    billAmt = billAmt + 19.95;
                    break;
                case 4:
                    billAmt = billAmt + 17.95;
                    break;

            }
            MainCourseItems.Text = billAmt.ToString();
        }

        private void DessertChoice_SelectedIndexChanged(object sender, EventArgs e)
        {
            des = DessertChoice.SelectedIndex;

            switch (des)
            {
                case 0:
                    billAmt = billAmt + 5.95;
                    break;

                case 1:
                    billAmt = billAmt + 4.95;
                    break;

                case 2:
                    billAmt = billAmt + 2.95;
                    break;

            }
            DessertChoice.Text = billAmt.ToString();
        }

        private void Appsordered_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void MainCourses_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void DessertItems_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void SubtotalPrice_TextChanged(object sender, EventArgs e)
        {

        }

        private void TipsTotal_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (numberPeople <= 6)
            {
                TipsTotal.Enabled = true;
                e.Handled = !char.IsDigit(e.KeyChar);
                tip = billAmt + tip;
            }
            else
            {
                tip = billAmt * .15;
            }
        }

        private void Bevorder_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void TaxPrice_KeyPressed(object sender, KeyPressEventArgs e)
        {
            taxes = billAmt * taxrte;
        }

        private void TotalPrice_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void ClearBill_Click(object sender, EventArgs e)
        {
            NumofPeople.Clear();
            Bevorder.Items.Clear();
            Appsordered.Items.Clear();
            MainCourseItems.Items.Clear();
            DessertItems.Items.Clear();
            SubtotalPrice.Clear();
            TaxPrice.Clear();
            TotalPrice.Clear();
        }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Need help with some things on this application

#2 Sheepings   User is offline

  • D.I.C Lover
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,260
  • Joined: 05-December 13

Re: Need help with some things on this application

Posted 30 April 2019 - 09:39 AM

I would recommend you start with a good class for better structure. I also suggest you read this to check only numbers https://www.dreaminc...urrency-symbol/

You also should be a bit more specific about your use of Listboxes and what you are trying to do in detail (in terms of functionality you are trying to achieve). Time isn't on my side so maybe the others will give you more pointers.

This post has been edited by Sheepings: 30 April 2019 - 09:40 AM

Was This Post Helpful? 0
  • +
  • -

#3 Savageoncabbage   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 24-April 19

Re: Need help with some things on this application

Posted 30 April 2019 - 09:45 AM

So I was able to only accept numbers and control items but now I need to take the number that is input and add it to my integer numberPeople. How would I do that with it being a keypress?
private void NumofPeople_KeyPressed(object sender, KeyPressEventArgs e)
        {
            int numberPeople = Convert.ToInt32(NumofPeople.Text);
            NumofPeople.Enabled = true;
            e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar);
        }
[\code]

[code]
private void NumofPeople_KeyPressed(object sender, KeyPressEventArgs e)
{
int numberPeople = Convert.ToInt32(NumofPeople.Text);
NumofPeople.Enabled = true;
e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar);
}


Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6969
  • View blog
  • Posts: 23,686
  • Joined: 05-May 12

Re: Need help with some things on this application

Posted 30 April 2019 - 10:32 AM

First of all you are using the wrong technology. WinForms is at end-of-life. If you are developing any new code, you should be using WPF or Xamarin, not WinForms.

Second, if you must use WinForms, you are using the wrong control. A TextBox is meant to accept any kind of text input. If you know what the upper and lower bounds are, use the NumericUpDown control. It only allows numbers as the name implies. :) It even lets you control the number of decimal places and if thousands separators should be used.

There is also the MaskedTextBox which may sound exactly like what you want, especially if you've ever used the masked textbox controls from dBase/FoxBase or Borland/Delphi. If you look at the documentation and sample code, it looks like it's a perfect fit. As a programmer, this looks perfect!

Quote

The MaskedTextBox class is an enhanced TextBox control that supports a declarative syntax for accepting or rejecting user input. Using the Mask property, you can specify the following input without writing any custom validation logic in your application:
  • Required input characters.

  • Optional input characters.

  • The type of input expected at a given position in the mask; for example, a digit, or an alphabetic or alphanumeric character.

  • Mask literals, or characters that should appear directly in the MaskedTextBox; for example, the hyphens (-) in a phone number, or the currency symbol in a price.

  • Special processing for input characters; for example, to convert alphabetic characters to uppercase.


Alas, the .NET Framework implementation is a complete disservice to your users. Your users will find your home address and come with torches and pitchforks if you try to force this abomination on them.

Next, for money, you should really be using the decimal type. This is much safer for limiting the scope of rounding errors typically found with standard implementation of floating point numbers (double and float). It also has the extra benefit of having a ToString("C") method that supports the displaying the value as currency with the appropriate current culture's requirements for currency symbol, location of the currency symbol, decimal point symbol, thousands separators, etc.)

View PostSavageoncabbage, on 30 April 2019 - 12:28 PM, said:

I need help with putting what is selected from the combobox to the respecting listboxes.

What comboboxes? What listboxes? With the code you presented, it's unclear which is which.
Was This Post Helpful? 1
  • +
  • -

#5 Savageoncabbage   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 24-April 19

Re: Need help with some things on this application

Posted 30 April 2019 - 10:53 AM

So it would be if the user chooses something from combobox ie BeverageItems. It should add that to the listbox of BevOrder.
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6969
  • View blog
  • Posts: 23,686
  • Joined: 05-May 12

Re: Need help with some things on this application

Posted 30 April 2019 - 03:12 PM

Be aware that the SelectedIndexChanged event is not an indicator of the user making a choice. Since you are using WinForms, WinForms is of the era of the older Windows UI Design Guidelines that goes all the way back to Window 2.x. In that design guide, a choice is not really made unless the user clicks the "OK" button on a dialog.

(Yes, I know that the modern Web 2.0 UI as well as the Android UI is different. A choice is made until you change your mind and make a different choice.)

Anyway since you chose to write code in WinForms, you should follow the Windows UI Design Guidelines and not make up your on UI rules.

May I suggest using a check listbox to hold the items that a user can potentially order?
Was This Post Helpful? 0
  • +
  • -

#7 Savageoncabbage   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 24-April 19

Re: Need help with some things on this application

Posted 01 May 2019 - 05:03 PM

Ok so I have changed pretty much my entire code and it is now working. The issue I am having is adding a tip.
I need to have the user add a tip if the number of people at the table are under 6. Anything more and it would automatically charge 15%.
This is the code I have for the apply button
private void BtnApply_Click(object sender, EventArgs e)
        {
            if (NumberofPeople.SelectedItem.ToString() == "6 or more")
            {
                TipsTotal = total * 0.15;
            }

            else if (NumberofPeople.SelectedItem.ToString() != "6 or more")
                BtnApply.Enabled = true;
                TipsTotal = Convert.ToDouble(TipTotal.Text);

            total += TipsTotal;
        }


Right now it should be enabled up until the point where they choose the 6 or more option from the combobox and it should then be disabled as well as the textbox which for the textbox I am not sure on how to do it.
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6969
  • View blog
  • Posts: 23,686
  • Joined: 05-May 12

Re: Need help with some things on this application

Posted 01 May 2019 - 06:39 PM

This is when you depend on the selected item change event. When the number of people is 6 or more, disable the tip field (and set a flag for later). When the number of people is less than 6, then enable the tip field (and clear the flag). Later when the apply button is clicked, check the flag to see if you need to compute a fixed tip amount, or if you need to take the value from the enabled tip field.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1