使用名称变量访问多个表单控件 [英] accessing multiple form controls using a variable for the name

查看:30
本文介绍了使用名称变量访问多个表单控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试遍历一组 ComboBox 并使用连接的字符串 & 设置一个属性.变量来表示控件的名称.但是,我无法让表单实例将 (String & Integer_Variable) 识别为其控件之一——因此它无法将任何适当的属性或子例程识别为 System.Windows 的成员.表单.控件.

I'm trying to iterate through a group of ComboBoxes and set a property using a concatenated string & variable to represent the name of the control. However, I can't get the instance of the form to recognize the (String & Integer_Variable) as one of its controls -- and so it doesn't recognize any of the appropriate properties or subroutines as members of the System.Windows.Forms.Control.

我在 SO 上找到了 DirectCast 解决方案,它似乎有效(虽然我很怀疑),但感觉就像一个非常笨拙的解决方案.有没有更干净的方法来做到这一点?

I found the DirectCast solution on SO and it appears to work (although I'm dubious), but that feels like a very clumsy solution. Is there a cleaner way to do this?

For myTempCount = 1 To 6
    If tempValue < Me.("ComboBox" & myTempCount).Items.Count Then
        ComboBox.SelectedIndex = tempValue 'appears to work -- how?
        Me.ComboBox.SelectedIndex = tempValue 'appears to work

        Me.("ComboBox" & myTempCount).SelectedIndex = tempValue 'doesn't work
        Me.Controls.("ComboBox" & myTempCount).SelectedIndex = tempValue 'doesn't work

        DirectCast(Me.Controls.Find(("ComboBox" & myTempCount), True)(0), ComboBox).SelectedIndex = tempValue 'appears to work
        DirectCast(Me.Controls("ComboBox" & myTempCount), ComboBox).SelectedIndex = tempValue  'appears to work
Next

此代码最初是 VBA/VB6,是我通过 ArtinSoft 的 Visual Basic Upgrade Companion (VBUC) 提交的.FWIW,我使用的是 Microsoft Visual Basic 2010 Express.

This code was originally VBA / VB6, which I put through ArtinSoft's Visual Basic Upgrade Companion (VBUC). FWIW, I'm using Microsoft Visual Basic 2010 Express.

推荐答案

回答您的问题:

  1. ComboBox1.SelectedIndex 有效,因为 ComboBox1 是存在于 Form 的 ControlCollection 中的控件
  2. Me.ComboBoxPrinter1.SelectedIndex 之所以有效,是因为 Me 是对您的 Form 类的引用,并且它引用了 Control.
  3. Me.("ComboBoxPrinter" & myTempCount).SelectedIndex 不起作用,因为字符串 ComboBoxPrinter &myTempCount 是一个字符串而不是一个控件.
  4. Me.Controls.("ComboBoxPrinter" & myTempCount).SelectedIndex 出于同样的原因不起作用.
  5. 其他两个实例之所以有效,是因为您使用字符串作为键来查找和返回您转换为正确类型的控件并设置您的属性.
  1. ComboBox1.SelectedIndex works because ComboBox1 is the control that is present in the Form's ControlCollection
  2. Me.ComboBoxPrinter1.SelectedIndex works because Me is a reference to your Form class an it is referencing the Control.
  3. Me.("ComboBoxPrinter" & myTempCount).SelectedIndex doesn't work because the string ComboBoxPrinter & myTempCount is a string not a Control.
  4. Me.Controls.("ComboBoxPrinter" & myTempCount).SelectedIndex doesn't work for the same reasons.
  5. The other two instances work because you are using the string as a key to lookup and return the control which you the cast to the proper type and set your property.

我个人通常使用 DirectCast 以外的 CType.根据此 link 的主要区别CType 和 DirectCast 之间是 DirectCast 必须是确切的类型,因为 CType 可用于缩小或扩大转换.DirectCast 更有效,但更挑剔.

I personally usually use CType other than DirectCast. The main difference according to this link between CType and DirectCast is that DirectCast has to be the exact Type where as CType can be used in narrowing or widening Conversions. DirectCast is more effiecent albeit more finicky.

话虽如此,您可以这样做:

That being said you could do something like this:

For myTempCount = 1 To 6
    If Controls.ContainsKey("ComboBox" & myTempCount) Then
        CType(Controls("ComboBox" & myTempCount), ComboBox).SelectedIndex = tempValue
    End If
Next

我没有在 Controls 前面使用 Me,因为它指的是同一个集合,如果您的控件在另一个集合中,您将需要使用该 Container.即如果您使用的是 Panel Panel1.Controls.ContainsKey

I am not using Me in front of Controls since it refers to the same collection, if your controls are in another collection you will need to use that Container instead. i.e. if you were using a Panel Panel1.Controls.ContainsKey

这篇关于使用名称变量访问多个表单控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆