473,408 Members | 2,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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 2712
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: CGuy | last post by:
Hi, I have an ASPX page which has a datagrid and this datagrid is bound to a Custom Collection. sample code this.DataGrid1.DataSource = UserManager.Users; this.DataGrid1.DataBind();
0
by: Tal Sharfi | last post by:
Hi everyone I recently had the need for StringGrid object same as the one that Delphi has. An object that helps show lists of other objects in a simple grid. I searched the news groups and...
0
by: Robert Ludig | last post by:
How do I bind a textbox to a simple string varaible with databinding? I managed to do the binding but unfortnatedly the textvox does not get updated when I change the string wich the textbox is...
6
by: eye5600 | last post by:
Is there a cure for the problems using databinding with a DateTimePicker? I find that a) sometimes it works, sometimes it doesn't, and b) it fails silently causing all databinding on the DataSet...
3
by: Tom McLaughlin | last post by:
I am new to vb.net and I am running into problems understanding DataBinding and its concept. The only examples I find are always talk about Web design, I only want to use this data on my...
13
by: Michael Maes | last post by:
Hi, I have a UserControl containing some controls of which one is a ComboBox. All the InternalControls are Private and some are allowed to be accessed through Public Methods. One of the things I...
3
by: funkyMonkey | last post by:
I'm binding date fields from entity objects and formatting the output in the text box in short date format. Code: BindDateField(Me.txtCheckIn, "Text", reservation.BookingDetail, "CheckIn")...
11
by: =?Utf-8?B?R29rdWw=?= | last post by:
I am struck up with a problem and want anyone here to help me out. I am a beginner in .NET trying to learng DataBinding concepts. I have binded 4 text boxes with a dataset but when I say...
3
by: David Smith | last post by:
I have an app that shows the time it takes to execute a search, much like SQL Management Studio, but the app behaves as though the System.Windows.Forms.Timer gets blocked while binding the results to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.