简体   繁体   中英

Wanting to change a label using a combo box selection

I'm making a program that lets the user select a score/grade from a combobox and using a button click will calculate the answer for the user. However for some reason when I press calculate button the label text doesn't change at all. Also sorry if the codes messy or looks wrong as I'm still trying to learn.

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace btec_to_ucas
{

    public partial class Form1 : Form
    {
        //These are the values i want displayed on the label
        int PPP = 48;
        
        int MPP = 64;
        int MMP = 80;
        int MMM = 96;
        int MMD = 112;
        int DDM = 128;
        int DDD = 144;
        
        public Form1()
        {
            InitializeComponent();
            
        }
        // below i want whenever the button is pressed it will take the selected answer and display the int onto the label
        private void button1_Click(object sender, EventArgs e)
        {
            {
                switch (comboBox1.SelectedIndex)
                {
                    case 0:
                        if (comboBox1.SelectedIndex == PPP)
                        {
                            label1.Text = "48";
                        }
                        break;
                    case 1:
                        if (comboBox1.SelectedIndex == MPP)
                        {
                            label1.Text = "64";
                        }
                        break;
                    case 2:
                        if (comboBox1.SelectedIndex == MMP)
                        {
                            label1.Text = "96";
                        }



                        break;
                }
            }


        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

            
               
                

        }
    }
}  ```

Your switch statement should be like this:

switch (comboBox1.SelectedIndex)
{
    case 0:
        label1.Text = PPP.ToString();
        break;
    case 1:
        label1.Text = MPP.ToString();
        break;
        ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM