簡體   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