簡體   English   中英

如何在Windows窗體C#的下拉列表中添加字符串元素?

[英]How to add string elements in dropdown list in Windows form c#?

我將外部類命名為“ Enum”,然后將內部類命名為“ UnderGraduate”

然后在那堂課我做了這樣的字符串

 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)")
}
}
}

現在我想在主類中調用此字符串,並使用編碼將上一個dropdownlist的SelectedIndexChange上的for循環使用for循環將其元素添加到一個dropdownlist中

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'

請幫我解決這個問題.......

像這樣更改您的代碼:(請勿使用像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);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM