Question Passing an object (Text Box) to sub procedure

cadwalli

Active member
Joined
May 27, 2010
Messages
41
Location
United Kingdom
Programming Experience
10+
Hi

I need to write a procedure which will allow me to set readonly attributes for certain objects, for example:

My form contains two text boxes box1, box2.

My sub procecdure will contain code which sets box1.readonly() = true, but i dont want to name the "box1" in the sub procedure but pass it as a parameter....

I thought this could be achieved using :

private sub disableobject(byval inobject as system.object)
inobject.readonly() = true
end sub

Im pretty sure im missing something simple like casting but not sure what to do.

Can anyone help?

Ian
 
Actually as slight mod to this, I've got the sub procedure now working for individual text boxes:

Private Sub EnableText(ByVal inObject As TextBox)
inObject.ReadOnly = False
End Sub


Which is good, but how could i:

a) pass a list of objects into this form (where i the number of boxes passed in could be diffferent every time), i.e. EnableText(box1,box2,box3) and another time EnableText(box1,box2)

b) is it possible to make this procedure work for other types such as drop downs etc, so it doesnt have to specifically be a textbox being passed?
 
a) either a plain array or collection or define the parameter as ParamArray. Parameter Arrays (Visual Basic)
b) use a common base type, for example Control.
 
John, could you explain what you mean by common base type.

I'd ideally like text boxes and radio sets etc to all be "disabled" by the same function - is this possible
 
If two classes inherit a same class, then this class is a common base type for the two.

All controls inherits somewhere up the hierarchy the Control class, and this class has the Enabled property.
 
You can iterate all the controls on a form and check its type like:

VB.NET:
        For Each c As Control In Me.Controls
            If TypeOf (c) Is TextBox Then
                DirectCast(c, TextBox).Enabled = False
            End If
            If TypeOf (c) Is RadioButton Then
                DirectCast(c, RadioButton).Enabled = False
            End If
        Next
If you were to create a method such as Public Sub Disable(frm As Form), you would use frm.Controls instead of Me.Controls

hth
 
Back
Top