如何从哈希表条目中获取值
我已将所有表单控件放入哈希表中:-
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
或者如果你知道名字
or if you know the name
您可以通过与其关联的键检索值,基本上控件名称是您创建的哈希表中的键。因此,如果您知道需要访问的控件的名称:
否则使用 LINQ OfType() 和 List.ForEach():
或者使用
foreach
循环:(有一个关于误解的很好的概述List.ForEach() 用法)
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:
Otherwise using LINQ OfType() and List.ForEach():
OR using
foreach
loop:(there is a nice overview of misconception of the List.ForEach() usage)
将单选按钮控件投射到 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.
假设代码中的哈希表是 Hashtable 的实例:
您可以执行以下操作:
您还可以通过以下方式查看哈希映射条目的键:
这将与您放入哈希映射中的组件的名称 (c.Name) 相对应。
希望这对您有帮助。
Assuming the hashtable in your code is an instance of Hashtable:
You can do this:
Also you can see the key of the hashmap entry with:
That will correspond with the name of the component that you put in the hashmap (c.Name).
Hope this help you.