有没有比“这个*"更好的方法? [英] Is there a better way than *this*?

查看:77
本文介绍了有没有比“这个*"更好的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.

新手级VB程序员在这里.

我有一个包含一个组合框的表单,用户可以从中选择PC主机名.选择主机名后,程序将循环执行并执行三个功能:

1.启用所有复选框
2.选择/清除相应的框
3.禁用选中的复选框(以表明它们是最低要求).

问题是,对我来说,似乎有更多简化方法可以完成这些任务.我主要涉猎PHP之类的东西,所以我的思维方式是使用DOM,在DOM中对表单项进行索引,并可以通过form.elements [i]语法进行引用.

在此处寻找启发性(和相关的)概念.

谢谢!

 ' 创建包含21个关联复选框的主机名和值的数组
 Dim  arrAccounts(,) As  字符串 = {_
    {" 错误真实真实错误错误错误错误错误真实错误错误错误 False 错误错误错误},_
    {" 错误真实真实错误错误错误错误错误真实错误错误错误 True 真实错误错误},_
    {" 错误真实真实错误错误错误错误错误真实错误错误错误 False 错误错误错误},_
    {" 错误真实真实错误错误错误错误错误真实错误错误错误 True 真实错误错误},_
    {" 错误真实真实错误错误错误错误错误真实错误错误错误 True 真实错误错误} _
}

' 遍历帐户以设置值并随后禁用
对于主机名=  0  收件人(arrAccounts.长度/ 21 )- 1 
    ' 通过与组合框中的选择进行比较来确定相关的主机名
    如果 arrAccounts(主机名, 0 )= ComboBox1.Text 然后

        ' 启用复选框
        ckFirefox.Enabled = 真实
        ckChrome.Enabled = 真实
        ckClocX.Enabled = 真实
        ckJRE.Enabled = 真实
        ckKronos.Enabled = 真实
        ckWinlink.Enabled = 真实
        ckTokairo.Enabled = 真实
        ckNovarad.Enabled = 真实
        ckCART.Enabled = 真实
        ckAcrobat.Enabled = 真实
        ckEduTracker.Enabled = 真实
        ckStaffing.Enabled = 真实
        ck610.Enabled = 
        ck1515.Enabled = 真实
        ck161.Enabled = 真实
        ck1060.Enabled = 真实
        ckBasisAlarm.Enabled = 
        ckBasisSys.Enabled = 真实
        ckBasisID.Enabled = 
        ckOpOutlook.Enabled = 真实

        ' 选中相应的复选框
        ckFirefox.Checked = arrAccounts(HostName, 1 )
        ckChrome.Checked = arrAccounts(HostName, 2 )
        ckClocX.Checked = arrAccounts(HostName, 3 )
        ckJRE.Checked = arrAccounts(HostName, 4 )
        ckKronos.Checked = arrAccounts(HostName, 5 )
        ckWinlink.Checked = arrAccounts(HostName, 6 )
        ckTokairo.Checked = arrAccounts(HostName, 7 )
        ckNovarad.Checked = arrAccounts(HostName, 8 )
        ckCART.Checked = arrAccounts(HostName, 9 )
        ckAcrobat.Checked = arrAccounts(HostName, 10 )
        ckEduTracker.Checked = arrAccounts(HostName, 11 )
        ckStaffing.Checked = arrAccounts(HostName, 12 )
        ck610.Checked = arrAccounts(HostName, 13 )
        ck1515.Checked = arrAccounts(HostName, 14 )
        ck161.Checked = arrAccounts(HostName, 15 )
        ck1060.Checked = arrAccounts(HostName, 16 )
        ckBasisAlarm.Checked = arrAccounts(HostName, 17 )
        ckBasisSys.Checked = arrAccounts(HostName, 18 )
        ckBasisID.Checked = arrAccounts(HostName, 19 )
        ckOpOutlook.Checked = arrAccounts(HostName, 20 )

        ' 禁用CHECKED复选框
        ckFirefox.Enabled = 不是 布尔值 .Parse(arrAccounts(HostName, 1 ))
        ckChrome.Enabled = 不是 布尔值 .Parse(arrAccounts(HostName, 2 ))
        ckClocX.Enabled = 不是 布尔值 .Parse(arrAccounts(HostName, 3 ))
        ckJRE.Enabled = 不是 布尔值 .Parse(arrAccounts(HostName, 4 ))
        ckKronos.Enabled = 不是 布尔值 .Parse(arrAccounts(HostName, 5 ))
        ckWinlink.Enabled = 不是 布尔值 .Parse(arrAccounts(HostName, 6 ))
        ckTokairo.Enabled = 不是 布尔值 .Parse(arrAccounts(HostName, 7 ))
        ckNovarad.Enabled = 不是 布尔值 .Parse(arrAccounts(HostName, 8 ))
        ckCART.Enabled = 不是 布尔值 .Parse(arrAccounts(HostName, 9 ))
        ckAcrobat.Enabled = 不是 布尔值 .Parse(arrAccounts(HostName, 10 ))
        ckEduTracker.Enabled = 不是 布尔值 .Parse(arrAccounts(HostName, 11 ))
        ckStaffing.Enabled = 不是 布尔值 .Parse(arrAccounts(HostName, 12 ))
        ck610.Enabled = 不是 布尔值 .Parse(arrAccounts(HostName, 13 ))
        ck1515.Enabled = 不是 布尔值 .Parse(arrAccounts(HostName, 14 ))
        ck161.Enabled = 不是 布尔值 .Parse(arrAccounts(HostName, 15 ))
        ck1060.Enabled = 不是 布尔值 .Parse(arrAccounts(HostName, 16 ))
        ckBasisAlarm.Enabled = 不是 布尔值 .Parse(arrAccounts(HostName, 17 ))
        ckBasisSys.Enabled = 不是 布尔值 .Parse(arrAccounts(HostName, 18 ))
        ckBasisID.Enabled = 不是 布尔值 .Parse(arrAccounts(HostName, 19 ))
        ckOpOutlook.Enabled = 不是 布尔值 .Parse(arrAccounts(HostName, 20 ))
    结束 如果
下一步 

解决方案

我通常会这样做(请注意,这是从内存中进行编码的没有调试器的好处)

1.将复选框的tag属性设置为数组的索引值.

chkChrome.Tag = 1

2.遍历所有控件,并根据主机数组中的值设置其属性.请注意,我假设您已经找到了与组合框设置匹配的正确主机阵列.

For Each control As Control In Form1.Controls

   If control.Tag Is Not Nothing Then ''We have a checkbox here
   
        Dim checked = CBool(HostArray(control.Tag))
        control.Checked = checked
        control.Enabled = Not checked
  
   End If

Next


不是使用数组而是将设置创建为结构或类,然后创建它们的集合并将控件绑定到由组合框选择触发的适当属性

好吧,我一直在研究类和结构,以便了解如何在这里实现它们.之所以艰难,是因为大多数信息源似乎都难以通过经典的(以及一点点俗套的)Class Animal和Class Vehicle示例来展示继承,而完全忽略了现实世界的使用.

通过使用STRUCT(例如),代码似乎要长得多,所以现在我想知道首先使用STRUCT的好处是什么.我的感觉是,它将以某种方式利用语言和框架本身的面向对象特性,但是我不确定这是怎么回事.

这些准确的观察结果(更长;更优化)了吗?


Hello, all.

Novice level VB programmer here.

I''ve got a form that contains a combo box from which the user can select a PC host name. When the host name is selected, the program loops through and performs three functions:

1. enable all check boxes
2. select/clear the appropriate boxes
3. disable selected check boxes (to indicate they are the minimum required)

The problem is that, to me, there seems like there would be more streamlined way to go about the tasks. I dabble mostly with PHP and that sort of thing, so I''m in the mindset of using the DOM where form items are indexed and can be referenced by the form.elements[i] syntax.

Looking for illuminating (and relevant) concepts here.

Thanks!

' Create array containing hostnames and values for the 21 associated check boxes
Dim arrAccounts(,) As String = { _
    {"HOST-01", True, False, True, True, False, False, False, False, False, True, False, False, True, False, False, False, False, False, False, False}, _
    {"HOST-02", True, False, True, True, False, False, False, False, False, True, False, False, True, False, False, False, True, True, False, False}, _
    {"HOST-03", True, False, True, True, False, False, False, False, False, True, False, False, True, False, False, False, False, False, False, False}, _
    {"HOST-04", True, False, True, True, False, False, False, False, False, True, False, False, True, False, False, False, True, True, False, False}, _
    {"HOST-05", True, False, True, True, False, False, False, False, False, True, False, False, True, False, False, False, True, True, False, False} _
}

' Loop through account to set the values and disable afterward
For HostName = 0 To (arrAccounts.Length / 21) - 1
    ' Determine the relevant hostname by comparing to selection in combo box
    If arrAccounts(HostName, 0) = ComboBox1.Text Then

        ' Enable check boxes
        ckFirefox.Enabled = True
        ckChrome.Enabled = True
        ckClocX.Enabled = True
        ckJRE.Enabled = True
        ckKronos.Enabled = True
        ckWinlink.Enabled = True
        ckTokairo.Enabled = True
        ckNovarad.Enabled = True
        ckCART.Enabled = True
        ckAcrobat.Enabled = True
        ckEduTracker.Enabled = True
        ckStaffing.Enabled = True
        ck610.Enabled = True
        ck1515.Enabled = True
        ck161.Enabled = True
        ck1060.Enabled = True
        ckBasisAlarm.Enabled = True
        ckBasisSys.Enabled = True
        ckBasisID.Enabled = True
        ckOpOutlook.Enabled = True

        ' Check the appropriate check boxes
        ckFirefox.Checked = arrAccounts(HostName, 1)
        ckChrome.Checked = arrAccounts(HostName, 2)
        ckClocX.Checked = arrAccounts(HostName, 3)
        ckJRE.Checked = arrAccounts(HostName, 4)
        ckKronos.Checked = arrAccounts(HostName, 5)
        ckWinlink.Checked = arrAccounts(HostName, 6)
        ckTokairo.Checked = arrAccounts(HostName, 7)
        ckNovarad.Checked = arrAccounts(HostName, 8)
        ckCART.Checked = arrAccounts(HostName, 9)
        ckAcrobat.Checked = arrAccounts(HostName, 10)
        ckEduTracker.Checked = arrAccounts(HostName, 11)
        ckStaffing.Checked = arrAccounts(HostName, 12)
        ck610.Checked = arrAccounts(HostName, 13)
        ck1515.Checked = arrAccounts(HostName, 14)
        ck161.Checked = arrAccounts(HostName, 15)
        ck1060.Checked = arrAccounts(HostName, 16)
        ckBasisAlarm.Checked = arrAccounts(HostName, 17)
        ckBasisSys.Checked = arrAccounts(HostName, 18)
        ckBasisID.Checked = arrAccounts(HostName, 19)
        ckOpOutlook.Checked = arrAccounts(HostName, 20)

        ' Disable CHECKED check boxes
        ckFirefox.Enabled = Not Boolean.Parse(arrAccounts(HostName, 1))
        ckChrome.Enabled = Not Boolean.Parse(arrAccounts(HostName, 2))
        ckClocX.Enabled = Not Boolean.Parse(arrAccounts(HostName, 3))
        ckJRE.Enabled = Not Boolean.Parse(arrAccounts(HostName, 4))
        ckKronos.Enabled = Not Boolean.Parse(arrAccounts(HostName, 5))
        ckWinlink.Enabled = Not Boolean.Parse(arrAccounts(HostName, 6))
        ckTokairo.Enabled = Not Boolean.Parse(arrAccounts(HostName, 7))
        ckNovarad.Enabled = Not Boolean.Parse(arrAccounts(HostName, 8))
        ckCART.Enabled = Not Boolean.Parse(arrAccounts(HostName, 9))
        ckAcrobat.Enabled = Not Boolean.Parse(arrAccounts(HostName, 10))
        ckEduTracker.Enabled = Not Boolean.Parse(arrAccounts(HostName, 11))
        ckStaffing.Enabled = Not Boolean.Parse(arrAccounts(HostName, 12))
        ck610.Enabled = Not Boolean.Parse(arrAccounts(HostName, 13))
        ck1515.Enabled = Not Boolean.Parse(arrAccounts(HostName, 14))
        ck161.Enabled = Not Boolean.Parse(arrAccounts(HostName, 15))
        ck1060.Enabled = Not Boolean.Parse(arrAccounts(HostName, 16))
        ckBasisAlarm.Enabled = Not Boolean.Parse(arrAccounts(HostName, 17))
        ckBasisSys.Enabled = Not Boolean.Parse(arrAccounts(HostName, 18))
        ckBasisID.Enabled = Not Boolean.Parse(arrAccounts(HostName, 19))
        ckOpOutlook.Enabled = Not Boolean.Parse(arrAccounts(HostName, 20))
    End If
Next

解决方案

I usually do something like this (please note this is coding from memory without the benefit of the debugger)

1. Set the tag property for your checkboxes to the index value of the array.

chkChrome.Tag = 1

2. Loop through all controls and set their property based on the values in the host array. Note that I assumed you already found the correct host array that matches the combo box setting.

For Each control As Control In Form1.Controls

   If control.Tag Is Not Nothing Then ''We have a checkbox here
   
        Dim checked = CBool(HostArray(control.Tag))
        control.Checked = checked
        control.Enabled = Not checked
  
   End If

Next


Instead of using an array create the settings as a struct or class, then create a collection of them and bind the controls to the appropriate properties triggered by the combobox selection


Okay, I''ve been looking through Classes and Structures in order to see how they might be implemented here. It''s tough going because most sources of information seem hell-bent on demonstrating inheritance through the canonical (and a little threadbare) Class Animal and Class Vehicle examples, utterly neglecting real world uses.

By using a STRUCT (for example), it seems as if the code would be much longer, so now I''m wondering what the advantage is of using the STRUCT in the first place. My feeling is that it would somehow capitalize on the OO nature of the language and framework themselves, but I''m not certain how that might be.

Are these accurate observations (much longer; optimized)?


这篇关于有没有比“这个*"更好的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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