[RESOLVED] user defined property
Dear All,
I have derived a textbox. This user control works fine everywhere. However, i have added a property in this as follow:
Code:
Public _propertyValue As Boolean
Public Overridable Property AllowNull() As Boolean
Get
Return _propertyValue
End Get
Set(ByVal value As Boolean)
_propertyValue = value
End Set
End Property
The reason behind this property is very simple. I want myself to be able to set this property of the textbox to TRUE if the value that is to be saved in the database can be null or not. If it is FALSE, then i will restrict the user to enter something in the textbox first. This also works fine. At design time, i can select True/False from the property.
Now, here is the problem. I am working in a MDI environment, therefore, i want to check all the textboxes with FALSE option selected for AllowNull property before saving the data.
I have written this piece of code:
Code:
Public Function checkAllTextBoxes() As Boolean
Dim ctrl As Control
For Each ctrl In Me.ActiveMdiChild.Controls
If TypeOf ctrl Is MyTextBox.mytextbox Then
'NOW I WANT TO ACCESS THE PROPERTY ALLOWNULL HERE
'LIKE THIS.
If ctrl.allowNull = false Then
'However, it only shows me the default textbox properties like allowdrop etc.
End If
End If
Next
End Function
how do i make user defined property global and accessible through this code, what am i doing wrong here?
Re: user defined property
Quote:
'However, it only shows me the default textbox properties like allowdrop etc.
It's not showing you the deafult TextBox properties. It's showing you the members of the Control class, because that's what type the 'ctrl' variable is.
Either:
vb.net Code:
If TypeOf ctrl Is MyTextBox Then
Dim mtb As MyTextBox = DirectCast(ctrl, MyTextBox)
or:
vb.net Code:
Dim mtb As MyTextBox = TryCast(ctrl, MyTextBox)
If mtb IsNot Nothing Then
The first option checks the type first and only casts if the control is the correct type. You know that cast will work so DirectCast is used.
The second option attempts a cast and only uses the result if it's not Nothing. If the result is Nothing then that means that the cast failed because the control was not that type.
By casting you are telling the compiler that the object is that type so it's safe to make the assignment. Once you have a variable of type MyTextBox you can access the members of the MyTextBox type.
Re: user defined property
thanks a lot...
the first method worked like a charm :)