繁体   English   中英

想要使用组合框选择更改标签

[英]Wanting to change a label using a combo box selection

我正在制作一个程序,让用户从组合框中选择分数/等级,并使用按钮单击将为用户计算答案。 但是由于某种原因,当我按下计算按钮时,标签文本根本没有改变。 如果代码凌乱或看起来错误,我也很抱歉,因为我仍在努力学习。

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)
        {

            
               
                

        }
    }
}  ```

你的 switch 语句应该是这样的:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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