关于使用我的问题.在vb.net中 [英] question about using me. in vb.net

查看:57
本文介绍了关于使用我的问题.在vb.net中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例代码:

sample code:

Me.GroupBox1.Controls("checkbox" & number).Enabled = True  '(works fine)
Me.GroupBox1("checkbox" + number).checked = True  '(fails)



我意识到问题是.enabled是me.windows.forms.control的一部分,而.checked不是,但是如何使用
更改复选框的状态 (复选框" +数字)?
BTW checkbox1.checked = true可以正常工作.

我敢肯定有一个简单的答案,我们将不胜感激.



I realise the problem is .enabled is part of me.windows.forms.control whereas .checked is not, but how can I change the state of the checkbox using
("checkbox" + number) ?
BTW checkbox1.checked = true works fine.

I''m sure there''s a simple answer and any help is much appreciated.

推荐答案

将".Controls"位添加到第二行并将其强制转换到复选框:
Add the ".Controls" bit to the second line, and cast it to a checkbox:
Me.GroupBox1.Controls("checkbox" & number).Enabled = True
Dim cb As CheckBox = TryCast(Me.GroupBox1.Controls("checkbox" + number), CheckBox)
cb.checked = True



[edit]意识到他需要将其转换为复选框... [/edit]



[edit]Realized he needs to cast it to a checkbox...[/edit]


您需要了解背景:

任何非静态方法(在VB.NET中为共享"),也称为实例方法与静态方法完全相同,只是有一个区别:一个附加参数以相同的方式隐式传递给它.与其他任何参数一样,通过将其压入堆栈.该参数具有方法调用中使用的类实例的运行时类型,称为"Me".此参数可用于使用点表示法通过给定实例访问类的任何成员(公共,内部或私有),例如"Me.member","Me.MyMethod(''...)"等.

通常,您可以跳过我".因为编译器可以弄清楚要使用哪个成员.在某些情况下,我".需要前缀.例如,当有一个显式参数也称为"member"时,您需要编写"Me.member".

—SA
You need to know the background:

Any non-static method ("Shared" in VB.NET), also called instance method is exactly same thing as a static method, with one difference: one additional parameter is implicitly passed to it in the same way as any other parameter, by pushing it on stack. This parameter has the run-time type of the class instance used in the call of the method and is called "Me". This parameter can be used to access any members (public, internal or private) of the class through a give instance using dot notation, like "Me.member", "Me.MyMethod(''...)", etc.

Usually you can skip "Me." as a compiler can figure out which member to use. There are some cases where "Me." prefix is needed. For example, you need to write "Me.member" when there is an explicit parameter also named "member".

—SA


这篇关于关于使用我的问题.在vb.net中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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