Easy One: Bind a Text Box to a an Integer Variable

M

Monty

Hope this is an easy one: How can I bind a text on a form to a an integer
variable? Possible? Thanks!
 
M

Marina Levit [MVP]

No, you cannot bind to a variable.

Binding works for collections that implement an interface such that the
objects doing the binding can property get data in and out of the source
object. It can't be done with just a given variable.
 
H

Homer J Simpson

Hope this is an easy one: How can I bind a text on a form to a an integer
variable? Possible? Thanks!

??? Why would you want to? Just update it when the variable changes.
 
M

Marina Levit [MVP]

Well, you would want to, because you have 20 fields, and you don't want code
manually updating the 20 variables all the time. It is for the same reasons
you would want to bind to an actual datasource.
 
H

Homer J Simpson

Well, you would want to, because you have 20 fields, and you don't want
code manually updating the 20 variables all the time. It is for the same
reasons you would want to bind to an actual datasource.

Binding to a data source is logical. Binding to a program variable is odd.
If you update the variable, update the display.
 
H

Herfried K. Wagner [MVP]

Monty said:
Hope this is an easy one: How can I bind a text on a form to a an integer
variable? Possible? Thanks!

Not directly, but what's the problem to parse the string using
'Integer.{Parse, TryParse}' and assign it to a private backup field in the
control's 'Validating' event?
 
M

Marina Levit [MVP]

You can argue that if you update the data source, update the display too.
It's not any harder to update a row in a datatable then it is to update a
variable as a developer. Of course multi-row datasources provide other
benefits too - but those are not always necessary.

Obviously, binding to a variable can't be done for technical reasons. But I
don't agree that it wouldn't be convenient in some cases.
 
M

Monty

??? Why would you want to? Just update it when the variable changes.
Convenience. Why would I want to write all those event handlers?

Also, it's the other way around... I want to update the variable when the
user changes the control's value.
 
M

Monty

Bummer. Thanks for the insight!

Marina Levit said:
You can argue that if you update the data source, update the display too.
It's not any harder to update a row in a datatable then it is to update a
variable as a developer. Of course multi-row datasources provide other
benefits too - but those are not always necessary.

Obviously, binding to a variable can't be done for technical reasons. But
I don't agree that it wouldn't be convenient in some cases.
 
J

Jim Hughes

To a varialbe directly, don't think so...

But to a form property, yes
' Add a button to a form, paste code behind below

'VB.Net 2005 code
Public Class Form1
Implements System.ComponentModel.INotifyPropertyChanged
Public Event PropertyChanged(ByVal sender As Object, _
ByVal e As System.ComponentModel.PropertyChangedEventArgs) _
Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged

Private _pageno As Integer = 1
<System.ComponentModel.Bindable(True)> _
Public Property PageNo() As Integer
Get
Return _pageno
End Get
Set(ByVal value As Integer)
_pageno = value
RaiseEvent PropertyChanged(Me, New
System.ComponentModel.PropertyChangedEventArgs("PageNo"))
End Set
End Property

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) _
Handles MyBase.Load
' updates form title whenever PageNo is changed
Me.DataBindings.Add(New Binding("Text", Me, "PageNo"))
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) _
Handles Button2.Click
' Increment PageNo
Me.PageNo += 1
End Sub

End Class ' Form1
 
M

Monty

Interesting, thanks Jim.

Jim Hughes said:
To a varialbe directly, don't think so...

But to a form property, yes
' Add a button to a form, paste code behind below

'VB.Net 2005 code
Public Class Form1
Implements System.ComponentModel.INotifyPropertyChanged
Public Event PropertyChanged(ByVal sender As Object, _
ByVal e As System.ComponentModel.PropertyChangedEventArgs) _
Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged

Private _pageno As Integer = 1
<System.ComponentModel.Bindable(True)> _
Public Property PageNo() As Integer
Get
Return _pageno
End Get
Set(ByVal value As Integer)
_pageno = value
RaiseEvent PropertyChanged(Me, New
System.ComponentModel.PropertyChangedEventArgs("PageNo"))
End Set
End Property

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) _
Handles MyBase.Load
' updates form title whenever PageNo is changed
Me.DataBindings.Add(New Binding("Text", Me, "PageNo"))
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) _
Handles Button2.Click
' Increment PageNo
Me.PageNo += 1
End Sub

End Class ' Form1
 
C

Cor Ligthert [MVP]

Monty,

I think that there are a lot of easier solution to keep track on a changed
textbox, however.

You can create a bindable collection with only one integer value in that.

Protect it that the index of the collection can only be zero.

Than you can bind that collection which holds your to your control.

I would not do that.

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

Top