|
-
Apr 15th, 2011, 01:08 PM
#1
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.
-
Apr 15th, 2011, 07:32 PM
#2
Re: Check TextBox Bindingsource Field Data Type
-
Apr 16th, 2011, 11:39 AM
#3
Re: Check TextBox Bindingsource Field Data Type
Thought I'd ask one last time.
-
Apr 17th, 2011, 11:11 PM
#4
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:
Dim b As Binding = Me.TextBox1.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 = prop.PropertyType MessageBox.Show(dataType.ToString()) End If
-
Apr 17th, 2011, 11:33 PM
#5
Re: Check TextBox Bindingsource Field Data Type
Actually, that's not going to work in all cases. I'll investigate a bit further.
-
Apr 18th, 2011, 12:27 PM
#6
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.
-
Apr 18th, 2011, 07:31 PM
#7
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.
-
Apr 18th, 2011, 08:16 PM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|