如何从哈希表条目中获取值

发布于 2024-12-12 05:34:52 字数 211 浏览 0 评论 0原文

我已将所有表单控件放入哈希表中:-

 foreach (Control c in this.Controls)
        {
            myhash.Add(c.Name, c);

        }

其中有两个单选按钮。我想获取按钮的值,即选中或未选中的值,并将它们分配给一个变量。请问我该怎么做呢。感谢大家的帮助。

I have put all of my form controls in a hashtable thus :-

 foreach (Control c in this.Controls)
        {
            myhash.Add(c.Name, c);

        }

amongst which are two radio buttons. I would like to get the value of the buttons, ie checked or unchecked, and assign them to a variable. How can I do that please. Thanks for all and any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

掐死时间 2024-12-19 05:34:52
foreach (Control c in hashtable.Values)
{
    if(c is RadioButton)
    {
        string name = x.Name;
        bool isChecked = (c as RadioButton).Checked;
    }
}

或者如果你知道名字

(hashtable["name"] as RadioButton).Checked;
foreach (Control c in hashtable.Values)
{
    if(c is RadioButton)
    {
        string name = x.Name;
        bool isChecked = (c as RadioButton).Checked;
    }
}

or if you know the name

(hashtable["name"] as RadioButton).Checked;
枕梦 2024-12-19 05:34:52

您可以通过与其关联的键检索值,基本上控件名称是您创建的哈希表中的键。因此,如果您知道需要访问的控件的名称:

var control = hash[radioButtonControlName] as RadioButton;

否则使用 LINQ OfType() List.ForEach()

// OfType() does check whether each item in hash.Values is of RadioButton type
// and return only matchings
hash.Values.OfType<RadioButton>()
           .ToList()
           .ForEach(rb => { bool isChecked = rb.Checked } );

或者使用 foreach 循环:
有一个关于误解的很好的概述List.ForEach() 用法)

var radioButtons = hash.Values.OfType<RadioButton>();

foreach(var button in radioButons)
{
    bool isChecked = rb.Checked;
}

You can retrieve a value by a key associated with it, basically control Name is a key in hashtable you've created. So if you know a name of controls you need to access:

var control = hash[radioButtonControlName] as RadioButton;

Otherwise using LINQ OfType() and List.ForEach():

// OfType() does check whether each item in hash.Values is of RadioButton type
// and return only matchings
hash.Values.OfType<RadioButton>()
           .ToList()
           .ForEach(rb => { bool isChecked = rb.Checked } );

OR using foreach loop:
(there is a nice overview of misconception of the List.ForEach() usage)

var radioButtons = hash.Values.OfType<RadioButton>();

foreach(var button in radioButons)
{
    bool isChecked = rb.Checked;
}
口干舌燥 2024-12-19 05:34:52

将单选按钮控件投射到 RadioButton 类< /a> 实例,然后查看 检查属性。至少我在 WebForms 中使用类似的类多次这样做过。

Cast the control that is the radio button to a RadioButton Class instance and then look at the checked property. At least that would be how I've done this many times over in WebForms using similar classes.

铁憨憨 2024-12-19 05:34:52

假设代码中的哈希表是 Hashtable 的实例:

Hashtable myhash= new Hashtable();
foreach (Control c in this.Controls)
{
    myhash.Add(c.Name, c);
}

您可以执行以下操作:

foreach (DictionaryEntry entry in myhash)
{
    RadioButton rb = entry.Value as RadioButton;
    if (rb != null)
        bool checked = rb.Checked;
}

您还可以通过以下方式查看哈希映射条目的键:

foreach (DictionaryEntry entry in myhash)
{
    var componentName = entry.Key;
}

这将与您放入哈希映射中的组件的名称 (c.Name) 相对应。

希望这对您有帮助。

Assuming the hashtable in your code is an instance of Hashtable:

Hashtable myhash= new Hashtable();
foreach (Control c in this.Controls)
{
    myhash.Add(c.Name, c);
}

You can do this:

foreach (DictionaryEntry entry in myhash)
{
    RadioButton rb = entry.Value as RadioButton;
    if (rb != null)
        bool checked = rb.Checked;
}

Also you can see the key of the hashmap entry with:

foreach (DictionaryEntry entry in myhash)
{
    var componentName = entry.Key;
}

That will correspond with the name of the component that you put in the hashmap (c.Name).

Hope this help you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文