开发者

VB.net - Element Reference

I know that you can refer to a form "indirectly" by simply including "me" instead of "form1," for example.

Is there a way to do the same th开发者_C百科ing for form elements like text boxes or radio buttons?

Example:

If me.checked = true then
    count += 1
Else
    count -=1
End If


The only way to do that is to create a property named checked that wraps a check box control which is a member of your form class. Something like:

Public Property checked() As Boolean
    Get
        return myCheckbox.Checked
    End Get
    Set(ByVal Value As Integer)
        myCheckbox.Checked = value
    End Set
End Property

Doing this doesn't get you much though. It would actually lead to code obfuscation rather than clarity and brevity.


In VB the keyword me is a reference to an instance of the class which contains the scope of code (as a function of that class) which contains the me reference. I don't know if this is clear enough, but basically me can't be used from inside the Form class to refer to a member of the class (such as a CheckBox or RadioButton control) - only to the class itself.

The CheckBox and RadioButton controls that you place on a Form are created as private objects inside of the Form class which contains them. They are called members of the class. From within the class which contains the CheckBox and RadioButton member instances you can refer to the class itself (the Form) as me. So, assuming that you have a checkbox called "checkbox1" as a control on that form, that CheckBox control would be created as a private member inside of the Form like this:

Private checkbox1 as CheckBox

After that, from that form you could refer to that CheckBox like so:

Me.checkbox1


VB.net uses a sender variable that indicates who's action is being sent to the procedure. By adding multiple elements to the procedure's "handles" property you can use the sender to identify the element being active.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜