468,321 Members | 1,971 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,321 developers. It's quick & easy.

Simple Databinding with Windows Forms

I am new to vb.net programming and am just exploring the way databinding
works with Windows forms and am having trouble with some fairly basic
customization of data entry. The form uses the SqlDataAdapter and
SqlDataset and loads a very simple table, with both text and integer fields.
If a textbox is bound to an integer source where the current value is a
valid integer, say 9, I can type in the textbox 'rubbish' and move to
another textbox. No error is generated, the textbox simply reverts to its
previous value of 9.

What I would like to happen, is that an error message is shown to the user
saying a number is expected. Clicking OK returns the cursor to the
textbox - still with the value of 'rubbish' in it so he can see the rubbish
he has typed. I would hope this is not a wildly unusual customization to
make, however I cannot make it happen.

I have tried code in the Binding.Parse event and Textbox.Validating event,
and both places allow me to catch the error, but not return focus to the
textbox with the offending value showing. I have had a good search through
the archives and seen similar posts, but no solution. Does anyone here know
one?

Thank you for any suggestions
Nov 21 '05 #1
7 2556
Justin,

This is in my opinion typical for the errorprovider

http://msdn.microsoft.com/library/de...classtopic.asp

A very complete sample is on that page.

I hope this helps,

Cor
Nov 21 '05 #2
Hi,

Here is a link to some custom validators written by Billy Hollis.
Which will popup a message when the user inputs invalid data.

http://dotnetmasters.com/SampleCode/...rsVersion3.zip

Ken
-------------------
"Justin Hoffman" <j@b.com> wrote in message
news:dd**********@nwrdmz03.dmz.ncs.ea.ibs-infra.bt.com...
I am new to vb.net programming and am just exploring the way databinding
works with Windows forms and am having trouble with some fairly basic
customization of data entry. The form uses the SqlDataAdapter and
SqlDataset and loads a very simple table, with both text and integer
fields. If a textbox is bound to an integer source where the current value
is a valid integer, say 9, I can type in the textbox 'rubbish' and move to
another textbox. No error is generated, the textbox simply reverts to its
previous value of 9.

What I would like to happen, is that an error message is shown to the user
saying a number is expected. Clicking OK returns the cursor to the
textbox - still with the value of 'rubbish' in it so he can see the
rubbish he has typed. I would hope this is not a wildly unusual
customization to make, however I cannot make it happen.

I have tried code in the Binding.Parse event and Textbox.Validating event,
and both places allow me to catch the error, but not return focus to the
textbox with the offending value showing. I have had a good search
through the archives and seen similar posts, but no solution. Does anyone
here know one?

Thank you for any suggestions

Nov 21 '05 #3
"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:OV**************@tk2msftngp13.phx.gbl...
Justin,

This is in my opinion typical for the errorprovider

http://msdn.microsoft.com/library/de...classtopic.asp

A very complete sample is on that page.

I hope this helps,

Cor


Thanks for the quick response. I loaded the sample and saw what the error
provider does. Are you implying I should abandon hope of getting my form to
work as described and use the errorprovider class instead? It doesn't
really do what I wanted - is leaving the text value in the box too
difficult.
Nov 21 '05 #4
Hi Ken
Thanks for the quick response. While I do appreciate any response, for
someone just starting out on creating a first Windows bound form sample,
there seems to be a lot of code in a lot of files in that zip. Perhaps once
I get going with vb.net I will look at something like that, but at the
moment I am just testing the water with bound forms.
All I need is to show an error message, returning focus to the textbox with
the offending text remaining. Have I asked too much of vb.net? Should I
accept the default behaviour and say the minor refinement is not worth the
work involved? Or perhaps abandon datasets, and populate the textboxes
manually?

"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:O1***************@TK2MSFTNGP14.phx.gbl...
Hi,

Here is a link to some custom validators written by Billy Hollis.
Which will popup a message when the user inputs invalid data.

http://dotnetmasters.com/SampleCode/...rsVersion3.zip

Ken
-------------------
"Justin Hoffman" <j@b.com> wrote in message
news:dd**********@nwrdmz03.dmz.ncs.ea.ibs-infra.bt.com...
I am new to vb.net programming and am just exploring the way databinding
works with Windows forms and am having trouble with some fairly basic
customization of data entry. The form uses the SqlDataAdapter and
SqlDataset and loads a very simple table, with both text and integer
fields. If a textbox is bound to an integer source where the current value
is a valid integer, say 9, I can type in the textbox 'rubbish' and move to
another textbox. No error is generated, the textbox simply reverts to its
previous value of 9.

What I would like to happen, is that an error message is shown to the
user saying a number is expected. Clicking OK returns the cursor to the
textbox - still with the value of 'rubbish' in it so he can see the
rubbish he has typed. I would hope this is not a wildly unusual
customization to make, however I cannot make it happen.

I have tried code in the Binding.Parse event and Textbox.Validating
event, and both places allow me to catch the error, but not return focus
to the textbox with the offending value showing. I have had a good
search through the archives and seen similar posts, but no solution.
Does anyone here know one?

Thank you for any suggestions


Nov 21 '05 #5
Justin,

Can you show me with this sample what you mean (I have seen the situation
that you are describing however first this simple one because you are
talking about simple databinding)

\\\This sample needs a textbox and two buttons on a form
Dim dt As New DataTable
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
dt.Columns.Add("Field")
TextBox1.DataBindings.Add("Text", dt, "Field")
dt.LoadDataRow(New Object() {"1"}, True)
dt.LoadDataRow(New Object() {"2"}, True)
End Sub
Private Sub TextBox1_Validating(ByVal sender _
As Object, ByVal e As System.ComponentModel.CancelEventArgs) _
Handles TextBox1.Validating
If TextBox1.Text.Length > 0 Then
If Not IsNumeric(TextBox1.Text) Then
MessageBox.Show("There was an error ")
TextBox1.Focus()
e.Cancel = True
End If
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
If BindingContext(dt).Position _
< dt.Rows.Count Then
BindingContext(dt).Position += 1
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
If BindingContext(dt).Position > 0 Then
BindingContext(dt).Position -= 1
End If
End Sub
///
Nov 21 '05 #6

"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:uo**************@TK2MSFTNGP10.phx.gbl...
Justin,

Can you show me with this sample what you mean (I have seen the situation
that you are describing however first this simple one because you are
talking about simple databinding)

\\\This sample needs a textbox and two buttons on a form
Dim dt As New DataTable
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
dt.Columns.Add("Field")
TextBox1.DataBindings.Add("Text", dt, "Field")
dt.LoadDataRow(New Object() {"1"}, True)
dt.LoadDataRow(New Object() {"2"}, True)
End Sub
Private Sub TextBox1_Validating(ByVal sender _
As Object, ByVal e As System.ComponentModel.CancelEventArgs) _
Handles TextBox1.Validating
If TextBox1.Text.Length > 0 Then
If Not IsNumeric(TextBox1.Text) Then
MessageBox.Show("There was an error ")
TextBox1.Focus()
e.Cancel = True
End If
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
If BindingContext(dt).Position _
< dt.Rows.Count Then
BindingContext(dt).Position += 1
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
If BindingContext(dt).Position > 0 Then
BindingContext(dt).Position -= 1
End If
End Sub
///

Hi Cor
Thanks for continuing with this. Your example works exactly as I would
like, however as soon as you specify that the datatable's column should be a
number, the behaviour changes.

Add another textbox TextBox2 and change the form's load behaviour as shown
below (my actual code uses SqlClient objects but this illustrates the
point). You now find, without changing any other coding, that your invalid
text entry in the integer column disappears and magically returns to the old
value. That is, the behaviour changes when you have a typed dataset. I
understand and accept that this is default behaviour, but do you need to be
a black-belt dotnet code guru to modify it slightly?

Thanks for any continued interest.
Dim r As DataRow

Dim colString As DataColumn = New DataColumn("StringCol")
colString.DataType = System.Type.GetType("System.String")
dt.Columns.Add(colString)

Dim colInt32 As DataColumn = New DataColumn("Int32Col")
colInt32.DataType = System.Type.GetType("System.Int32")
dt.Columns.Add(colInt32)

r = dt.NewRow()
r("StringCol") = "Number Six"
r("Int32Col") = 6
dt.Rows.Add(r)

r = dt.NewRow()
r("StringCol") = "Number Eight"
r("Int32Col") = 8
dt.Rows.Add(r)

TextBox1.DataBindings.Add("Text", dt, "Int32Col")
TextBox2.DataBindings.Add("Text", dt, "StringCol")


Nov 21 '05 #7
Justin,

I am sorry I find it weird myself as well.

It seems that as soon as the databinding comes active it checks the
posibility of the format and with an error it restores the original value.

I tested it in VB2005 B2, it is the same.

Maybe somebody else knows the easy answer.

Cor
Nov 21 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Tal Sharfi | last post: by
reply views Thread by Robert Ludig | last post: by
6 posts views Thread by eye5600 | last post: by
3 posts views Thread by Tom McLaughlin | last post: by
13 posts views Thread by Michael Maes | last post: by
11 posts views Thread by =?Utf-8?B?R29rdWw=?= | last post: by
3 posts views Thread by David Smith | last post: by
1 post views Thread by howard w | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.