【问题标题】:using a string to reference an object in C#在 C# 中使用字符串引用对象
【发布时间】:2016-09-14 03:18:49
【问题描述】:
    private void check_boxes(ref int which_series_a, string wh_bx)
    {
        // ******************** Checks to see if box is checked **********************
        if (wh_bx.Checked == true)

我尝试了this.this.Controls,但没有任何效果。

【问题讨论】:

  • wh_bx是控件的ID吗?有什么理由不能传递控件实例本身?
  • 在不知道wh_bx 包含哪些类型的值以及这些值与表单上的控件对象之间的关系的情况下,很难回答您的问题。

标签: c# string object reference


【解决方案1】:

假设 wh_bx 是复选框的名称。

 private void check_boxes(ref int which_series_a, string wh_bx)
 {   
  var myCtrl = this.Controls.Find(wh_bx, true) as CheckBox;
  if(myCtrl != null)
  {
     if(myCtrl.Checked){}
  }
}

【讨论】:

  • 在访问Checked 属性之前,您是否需要myCtrl 的类型为CheckBox
猜你喜欢
  • 1970-01-01
  • 2012-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-10
  • 2010-11-12
相关资源
最近更新 更多