C# 希望使用组合框选择更改标签
我正在制作一个程序,让用户从一个组合框中选择一个分数/等级,并使用一个按钮点击将为用户计算答案。然而,由于某种原因,当我按下“计算”按钮时,标签文本根本没有改变。如果代码凌乱或看起来出错,我也很抱歉,因为我还在努力学习C# 希望使用组合框选择更改标签,c#,C#,我正在制作一个程序,让用户从一个组合框中选择一个分数/等级,并使用一个按钮点击将为用户计算答案。然而,由于某种原因,当我按下“计算”按钮时,标签文本根本没有改变。如果代码凌乱或看起来出错,我也很抱歉,因为我还在努力学习 using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text
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;
...
您的标签不会更改,因为您检查SelectedIndex是否等于整数常量。这不能是因为开关已将SelectedIndex评估为不同的值。请说明如何初始化组合框内容。TBH我会创建一个字段,当索引发生更改时,您可以指定该字段,然后单击按钮,将标签文本属性更新为它应该是什么,或者在
SelectedIndexChanged
事件中全部执行。耶稣基督,谢谢您,我已经坚持了很久没有担心了。如果答案有帮助,请将其标记为已接受答案。