Results 1 to 3 of 3

Thread: [RESOLVED] user defined property

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2008
    Posts
    14

    Resolved [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?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: user defined property

    '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:
    1. If TypeOf ctrl Is MyTextBox Then
    2.     Dim mtb As MyTextBox = DirectCast(ctrl, MyTextBox)
    or:
    vb.net Code:
    1. Dim mtb As MyTextBox = TryCast(ctrl, MyTextBox)
    2.  
    3. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2008
    Posts
    14

    Re: user defined property

    thanks a lot...
    the first method worked like a charm

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width