简体   繁体   English

从字典C#中添加值

[英]Adding value from a dictionary c#

i have a method where i'm adding Tabpages and i created a dictionary for the tabpages because they should be shown in their each Mode(Key) 我有一种添加标签页的方法,并为标签页创建了字典,因为它们应该显示在其每个Mode(Key)中

I want to iterate im my SwitchMode method over the forech and and add the tabpages. 我想在前端遍历我的SwitchMode方法,并添加标签页。

My Code: 我的代码:

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

Iterating over Dictionary - > Here I need help - how can I add all the tabpages and not only the first and is this kind of iterating right? 遍历字典->在这里我需要帮助-如何添加所有标签页,不仅是第一个,而且这样的遍历是正确的吗?

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

            }
        }

you can iterate through the Keys collection 您可以遍历Keys集合

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

What I don't understand is what is the function of Mode flag in your scenario. 我不了解您的方案中模式标志的功能是什么。 It's not used in the AddTabPage. AddTabPage中未使用它。 So there is no diferences between one tab and another. 因此,一个选项卡和另一个选项卡之间没有区别。

You're almost there, use m.Key in your loop: 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