none
Beginner C# Help: Trying to use Arrays to calculate combo box options RRS feed

  • Question

  • I am trying to create a program where you can select 1 of 3 different options in each of the 3 combo boxes, along with how many of the item you want. Then once you hit calculate, it will display the subtotal and taxes. I am trying to figure out how to accomplish this using arrays for the values in my combo boxes, and loop the values to display them depending on what was chosen. If anyone could give me some pointers on how to accomplish this that would be incredibly helpful.


    • Edited by Jubby Ton Monday, December 18, 2017 8:12 PM
    Monday, December 18, 2017 8:08 PM

Answers

  • A simple way

    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication3
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
                comboBox2.DropDownStyle = ComboBoxStyle.DropDownList;
                comboBox3.DropDownStyle = ComboBoxStyle.DropDownList;
    
                comboBox1.Items.AddRange(new string[] {"Diamond","Pearl","Gold" });
                // we could use the above syntax for the next two but wanted to show
                // another method to load.
                comboBox2.DataSource = new List<int>() { 1, 2, 3, 4};
                comboBox3.DataSource = new List<decimal>() {10.5M,20M,30M,40M };
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                if (comboBox1.SelectedIndex > -1)
                {
                    textBox1.Text = $"{comboBox1.Text} qty {comboBox2.Text} price {comboBox3.Text} = {Convert.ToInt32(comboBox2.Text) * Convert.ToDecimal(comboBox3.Text)}";
                }
                else
                {
                    MessageBox.Show("Select a item");
                }
                
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Jubby Ton Tuesday, December 19, 2017 6:26 PM
    Monday, December 18, 2017 8:24 PM
    Moderator