如何在Windows窗体下拉列表中添加字符串元素

本文关键字:添加 字符串 元素 下拉列表 窗体 Windows | 更新日期: 2023-09-27 18:11:04

我创建了一个外部类名为"Enum",然后在其中创建了一个名为"本科生"的内部类

那么在这个类中我创建了一个像这样的字符串

 class Enum
    {
     public class UnderGraduate
        {
            public string[] EducationUG=new string[]
        {
            ("Bachelor of Arts (B.A)"),
            ("Bachelor of Arts (Bachelor of Education (B.A. B.Ed)"),
            ("Bachelor of Arts (Bachelor of Law (B.A.B.L)"),
            ("Bachelor of Arts (Bachelor of Law (B.A.LLB)"),
            ("Bachelor of Ayurvedic Medicine and Surgery (B.A.M.S)"),
            ("Bachelor of Applied Sciences (B.A.S)")
}
}
}

现在我想在主类中调用这个字符串,并将其元素添加到一个下拉列表中,使用for循环对先前的下拉列表使用编码

private void edulvlcb_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (edulvlcb.SelectedItem.ToString() == "UnderGraduate")
            {         
                educb.Items.Clear();
                foreach (string ug in new Enum.UnderGraduate.EducationUG[])
                {
                    educb.Items.Add(new ListItem(EducationUG[name].ToString()));
                }

但是显示错误

An Object Reference is required for non-static field ,method or property 'Project.Enum.UnderGraduate.EducationUG'

请帮我解决这个问题.......

如何在Windows窗体下拉列表中添加字符串元素

这样修改代码:(不要使用保留字,如Enum!)

class Enum1
{
    public class UnderGraduate
    {
        public static string[] EducationUG=new string[]{
            "Bachelor of Arts (B.A)",
            "Bachelor of Arts (Bachelor of Education (B.A. B.Ed)",
            "Bachelor of Arts (Bachelor of Law (B.A.B.L)",
            "Bachelor of Arts (Bachelor of Law (B.A.LLB)",
            "Bachelor of Ayurvedic Medicine and Surgery (B.A.M.S)",
            "Bachelor of Applied Sciences (B.A.S)"}
    }
}

private void edulvlcb_SelectedIndexChanged(object sender, EventArgs e)
{
    if (edulvlcb.SelectedItem.ToString() == "UnderGraduate")
    {         
        educb.Items.Clear();
        foreach (string ug in Enum1.UnderGraduate.EducationUG)
            educb.Items.Add(ug);
    }
}