Simple Databinding with Windows Forms

  • Thread starter Thread starter Justin Hoffman
  • Start date Start date
J

Justin Hoffman

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
 
Cor Ligthert said:
Justin,

This is in my opinion typical for the errorprovider

http://msdn.microsoft.com/library/d...systemwindowsformserrorproviderclasstopic.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.
 
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?
 
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
///
 
Cor Ligthert said:
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")
 
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top