簡體   English   中英

從字典C#中添加值

[英]Adding value from a dictionary c#

我有一種添加標簽頁的方法,並為標簽頁創建了字典,因為它們應該顯示在其每個Mode(Key)中

我想在前端遍歷我的SwitchMode方法,並添加標簽頁。

我的代碼:

    [Flags]
    public enum Mode
    {
        Start = 1,
        Test = 1 << 1,
        Config = 1 << 2,
    }

遍歷字典->在這里我需要幫助-如何添加所有標簽頁,不僅是第一個,而且這樣的遍歷是正確的嗎?

public void SwitchMode (Mode mode)
        {
            foreach (var m in _tabDict)
            {
                if (m.Value == Mode.Start)
                {
                    AddTabPage (_tabDict.First ().Key);
                }
                if (m.Value == Mode.Test)
                {
                    AddTabPage (_tabDict.First ().Key);
                    // AddTabPage (_tabDict.All ()); // doesn't work
                }
                if (m.Value == Mode.Config)
                {
                    AddTabPage (_tabDict.First ().Key);
                }

            }
        }

您可以遍歷Keys集合

foreach (Type key in _tabDict.Keys) {
    AddTabPage(key);
}

我不了解您的方案中模式標志的功能是什么。 AddTabPage中未使用它。 因此,一個選項卡和另一個選項卡之間沒有區別。

m.Key ,在循環中使用m.Key

public void SwitchMode(Mode mode)
{
    foreach (var m in _tabDict)
    {
        if (m.Value == Mode.Start)
        {
            AddTabPage(m.Key);
        }
        if (m.Value == Mode.Test)
        {
            AddTabPage(m.Key);
        }
        if (m.Value == Mode.Config)
        {
            AddTabPage(m.Key);
        }

    }
}

暫無
暫無

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

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