如何在表单上循环选择控件类型? [英] How do I loop through chosen types of controls on a form?

查看:51
本文介绍了如何在表单上循环选择控件类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置一个类模块,它允许我传递表单名称和我想要测试的控件类型(文本框,组合框等),并测试它们是否已填充。我的想法是,我会有一个类似于TxtCboLstChkOpt的参数,如果我只想检查Textboxes和Checkboxes,我会将它作为标志传递10010,以便知道要检查哪些类型。我的问题是我不知道如何只运行一次循环并选择那些在其部分中有1的类型。通常我会将此硬编码到Select Case语句中,如下所示:

I''m trying to setup a class module that will allow me to pass the form name and the types of controls that I want tested (textboxes, comboboxes, etc.) and have it test if they are populated. My thinking is that I would have a parameter that would be something like TxtCboLstChkOpt and if I wanted to only check Textboxes and Checkboxes I would pass it 10010 as flags for the class to know which types to check. My problem is that I don''t know how to only run the loop once and select those types that have a 1 in their section. Normally I would have this hard coded into a Select Case statement like the following:

展开 | 选择 | Wrap | 行号

推荐答案

很抱歉,acTextBox,acComboBox,acListBox等的值不是2的幂,所以不能以你怀疑的方式使用。如果要将多个值传递给过程或类方法,则需要使用数组或其中嵌入了值列表的字符串。


PS。您要检查的控件属性是ControlType而不是Type。
Unfortunately, the values of acTextBox, acComboBox, acListBox, etc are not powers of two, so cannot be used in the way you suspected. If you want to pass multiple values to a procedure or class method then you will need to use either an array or a string with value lists embedded within it.

PS. The control property you would want to check would be ControlType rather than Type.


您是使用标志还是复选框来确定要检查的控件?因为第二段代码听起来好像你想要使用复选框。


无论哪种方式,你都不必使用select,我只需要使用5 if语句你要检查的5种控件类型。
Are you using flags or checkboxes to determine what controls to check? Because the second block of code makes it sound like you want to use checkboxes.

Either way, you don''t have to use select, I would just use 5 if statements for the 5 control types you want to check.
展开 | 选择 | Wrap | 行号


@Rabbit我的第二个代码块的计划是设置布尔变量( txt,cbo)基于标志。那么你的建议会循环遍历所有控件五次(每种控件类型一次)?


@NeoPa我没想到acTextBox,acComboBox等的值是2的幂,但我可以使用我的标志然后选择要添加到列表中的控件类型。你建议使用一个数组。是否可以根据我的标志系统将控件类型添加到数组中然后让它循环遍历类型列表以查看它是否应该检查它(而不是select case语句)?使用OP中的示例,我的标志设置为10010.我测试第一个字符(文本框)并将其设置为true,因此我将其添加到数组中。我测试第二个字符(组合框)并将其设置为false,因此我不会将其添加到数组中。我继续浏览每种类型,最终得到了我的类型数组中的Textboxes和Checkboxes。那么然后在每个循环中通过控件,然后我将循环遍历数组,看看我是否应该检查它。这样的东西会起作用吗?


我会将.Type修复为.ControlType。
@Rabbit My plan for the second code block was to set boolean variable (txt, cbo) based on the flags. So your suggestion would then loop through all of the controls five times (once for each control type)?

@NeoPa I wasn''t thinking that the values of acTextBox, acComboBox, etc were powers of two, but that I could use my flags to then choose which type of control to add to the list. You suggest using an array. Would it be something that I could add control types to the array based on my flag system and then have it loop through the list of types to see if it is supposed to check it (instead of the select case statement)? Using the example in the OP, my flag is set to 10010. I test the first character (textboxes) and it is set to true, so I add it to the array. I test the second character (comboboxes) and it is set to false so I don''t add it to the array. I continue on through each type and I end up with Textboxes and Checkboxes in my array of types. So then inside each loop through the controls, I would then loop through the array to see if I''m supposed to check it. Would something like that work?

I''ll fix the .Type to .ControlType.


这篇关于如何在表单上循环选择控件类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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