Results 1 to 8 of 8

Thread: Check TextBox Bindingsource Field Data Type

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,515

    Check TextBox Bindingsource Field Data Type

    Lets say I have a form with several Textboxes that are all bound to "bindingsource1". I'd like to be able to check the field data type each textbox is bound to. Something like this,
    Code:
            Dim ctl As Control
            For Each ctl In frm.Controls
                If ctl.GetType.IsAssignableFrom(GetType(TextBox)) Then
                    if ctl 'data type of field' = "Text" then
                        'then check if text is entered
                    end if
                    if ctl 'data type of field' = "Number" then
                        'then check if a number is entered
                    end if 
                End If
            Next
    I've been searching but can't find anything on this subject. Maybe I'm not wording my search right.

    Any information or suggestion would be appriciated.

  2. #2

    Thread Starter
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,515

    Re: Check TextBox Bindingsource Field Data Type

    Is this possible?

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,515

    Re: Check TextBox Bindingsource Field Data Type

    Thought I'd ask one last time.

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

    Re: Check TextBox Bindingsource Field Data Type

    When you bind a control property in WinForms you are creating an instance of the Binding class. You'd use it to get that information, e.g.
    vb.net Code:
    1. Dim b As Binding = Me.TextBox1.DataBindings("Text")
    2.  
    3. If b Is Nothing Then
    4.     MessageBox.Show("Property not bound")
    5. Else
    6.     Dim dataSource As Object = b.DataSource
    7.     Dim propertyName As String = b.BindingMemberInfo.BindingField
    8.     Dim prop As PropertyInfo = dataSource.GetType().GetProperty(propertyName)
    9.     Dim dataType As Type = prop.PropertyType
    10.  
    11.     MessageBox.Show(dataType.ToString())
    12. End If
    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

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

    Re: Check TextBox Bindingsource Field Data Type

    Actually, that's not going to work in all cases. I'll investigate a bit further.
    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

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,515

    Re: Check TextBox Bindingsource Field Data Type

    jmc,

    Thanks for the reply.

    I tried your code but had some problems. This line,
    Code:
    Dim prop As PropertyInfo = dataSource.GetType().GetProperty(propertyName)
    Gave me an errror "Type 'PropertyInfo' not defined "

    So I worked around that problem by writing the code like this,
    Code:
            Dim b As Binding = Me.SCRNSTextBox.DataBindings("Text")
            If b Is Nothing Then
                MessageBox.Show("Property not bound")
    
            Else
                Dim dataSource As Object = b.DataSource
                Dim propertyName As String = b.BindingMemberInfo.BindingField
                'Dim prop As PropertyInfo = dataSource.GetType().GetProperty(propertyName)
                Dim dataType As Type = dataSource.GetType().GetProperty(propertyName).PropertyType
            End If
    But this line,
    Code:
    Dim dataType As Type = dataSource.GetType().GetProperty(propertyName).PropertyType
    Gave me an error "Object reference not set to an instance of an object"
    I tried several variation but with no luck.

    BTW - Debug.Print( datasource.gettype.tostring ) prints "system.windows.Forms.BindingSource"
    Last edited by wes4dbt; Apr 18th, 2011 at 12:34 PM.

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

    Re: Check TextBox Bindingsource Field Data Type

    This sort of thing shouldn't really need to be explained to someone with close to a thousand posts but here goes. If you want to use a type name unqualified in code then you must import the namespace that that type is a member of. If you don't know what namespace that is, simply read the documentation for the type.

    As for the NullReferenceException, I did mention that that code wouldn't work. It only works if you bind directly to an object with a property with that name, which you're not in the case of a BindingSource. It would require you to use PropertyDescriptors but, at this stage, I'm not exactly sure how.
    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

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,515

    Re: Check TextBox Bindingsource Field Data Type

    jmc,

    In my defence, 90% of those post where in the Reporting or VB6 forum. Anyway I should have thought it through but now I've fixed the first isssue.

    At sometime in the future you have any new information on the NullReferenceException issue Id be glad to hear about. For now, I'll move on with plan B.

    Thanks for the reply

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