By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,239 Members | 1,503 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,239 IT Pros & Developers. It's quick & easy.

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

P: n/a
Hope this is an easy one: How can I bind a text on a form to a an integer
variable? Possible? Thanks!
Apr 3 '06 #1
Share this Question
Share on Google+
12 Replies


P: n/a
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.

"Monty" <mo***@community.nospam> wrote in message
news:ej**************@TK2MSFTNGP14.phx.gbl...
Hope this is an easy one: How can I bind a text on a form to a an integer
variable? Possible? Thanks!

Apr 3 '06 #2

P: n/a

"Monty" <mo***@community.nospam> wrote in message
news:ej**************@TK2MSFTNGP14.phx.gbl...
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.


Apr 3 '06 #3

P: n/a
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.

"Homer J Simpson" <no****@nowhere.com> wrote in message
news:tzdYf.24774$Ph4.21405@edtnps90...

"Monty" <mo***@community.nospam> wrote in message
news:ej**************@TK2MSFTNGP14.phx.gbl...
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.

Apr 3 '06 #4

P: n/a

"Marina Levit [MVP]" <so*****@nospam.com> wrote in message
news:ei**************@TK2MSFTNGP12.phx.gbl...
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.

Apr 3 '06 #5

P: n/a
"Monty" <mo***@community.nospam> schrieb:
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 S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Apr 3 '06 #6

P: n/a
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.

"Homer J Simpson" <no****@nowhere.com> wrote in message
news:GPeYf.24792$Ph4.3479@edtnps90...

"Marina Levit [MVP]" <so*****@nospam.com> wrote in message
news:ei**************@TK2MSFTNGP12.phx.gbl...
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.

Apr 3 '06 #7

P: n/a
> ??? 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.

Apr 3 '06 #8

P: n/a
Bummer. Thanks for the insight!

"Marina Levit [MVP]" <so*****@nospam.com> wrote in message
news:eN****************@TK2MSFTNGP15.phx.gbl...
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.

"Homer J Simpson" <no****@nowhere.com> wrote in message
news:GPeYf.24792$Ph4.3479@edtnps90...

"Marina Levit [MVP]" <so*****@nospam.com> wrote in message
news:ei**************@TK2MSFTNGP12.phx.gbl...
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.


Apr 3 '06 #9

P: n/a

"Monty" <mo***@community.nospam> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
??? Why would you want to? Just update it when the variable changes.
Convenience. Why would I want to write all those event handlers?
How many?
Also, it's the other way around... I want to update the variable when the
user changes the control's value.


Why not a NumericUpDown control?

Apr 3 '06 #10

P: n/a
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.Prope rtyChanged

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("Pa geNo"))
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

"Monty" <mo***@community.nospam> wrote in message
news:ej**************@TK2MSFTNGP14.phx.gbl...
Hope this is an easy one: How can I bind a text on a form to a an integer
variable? Possible? Thanks!

Apr 3 '06 #11

P: n/a
Interesting, thanks Jim.

"Jim Hughes" <NO*********@Hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
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.Prope rtyChanged

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("Pa geNo"))
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

"Monty" <mo***@community.nospam> wrote in message
news:ej**************@TK2MSFTNGP14.phx.gbl...
Hope this is an easy one: How can I bind a text on a form to a an integer
variable? Possible? Thanks!


Apr 4 '06 #12

P: n/a
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

"Monty" <mo***@community.nospam> schreef in bericht
news:ej**************@TK2MSFTNGP14.phx.gbl...
Hope this is an easy one: How can I bind a text on a form to a an integer
variable? Possible? Thanks!

Apr 4 '06 #13

This discussion thread is closed

Replies have been disabled for this discussion.