Home All Groups Group Topic Archive Search About

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

Author
3 Apr 2006 4:33 PM
Monty
Hope this is an easy one: How can I bind a text on a form to a an integer
variable? Possible? Thanks!

Author
3 Apr 2006 4:56 PM
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.

Show quoteHide quote
"Monty" <monty@community.nospam> wrote in message
news:ej5IZxzVGHA.4792@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!
>
Author
3 Apr 2006 6:11 PM
Homer J Simpson
"Monty" <monty@community.nospam> wrote in message
news:ej5IZxzVGHA.4792@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.
Author
3 Apr 2006 6:36 PM
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.

"Homer J Simpson" <nob***@nowhere.com> wrote in message
news:tzdYf.24774$Ph4.21405@edtnps90...
Show quoteHide quote
>
> "Monty" <monty@community.nospam> wrote in message
> news:ej5IZxzVGHA.4792@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.
>
>
>
>
Author
3 Apr 2006 7:37 PM
Homer J Simpson
"Marina Levit [MVP]" <someone@nospam.com> wrote in message
news:ei3HJ20VGHA.4308@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.
Author
3 Apr 2006 7:44 PM
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.

"Homer J Simpson" <nob***@nowhere.com> wrote in message
news:GPeYf.24792$Ph4.3479@edtnps90...
Show quoteHide quote
>
> "Marina Levit [MVP]" <someone@nospam.com> wrote in message
> news:ei3HJ20VGHA.4308@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.
>
>
>
Author
3 Apr 2006 8:35 PM
Monty
Bummer. Thanks for the insight!

Show quoteHide quote
"Marina Levit [MVP]" <someone@nospam.com> wrote in message
news:eNsm%23b1VGHA.4720@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" <nob***@nowhere.com> wrote in message
> news:GPeYf.24792$Ph4.3479@edtnps90...
>>
>> "Marina Levit [MVP]" <someone@nospam.com> wrote in message
>> news:ei3HJ20VGHA.4308@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.
>>
>>
>>
>
>
Author
3 Apr 2006 8:31 PM
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.
Author
3 Apr 2006 10:04 PM
Homer J Simpson
"Monty" <monty@community.nospam> wrote in message
news:%23JNmi21VGHA.4596@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?
Author
3 Apr 2006 7:37 PM
Herfried K. Wagner [MVP]
"Monty" <monty@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/>
Author
3 Apr 2006 10:06 PM
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

Show quoteHide quote
"Monty" <monty@community.nospam> wrote in message
news:ej5IZxzVGHA.4792@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!
>
Author
4 Apr 2006 1:44 AM
Monty
Interesting, thanks Jim.

Show quoteHide quote
"Jim Hughes" <NOSPAMJ3033@Hotmail.com> wrote in message
news:%23eDQQr2VGHA.5468@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.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
>
> "Monty" <monty@community.nospam> wrote in message
> news:ej5IZxzVGHA.4792@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!
>>
>
>
Author
4 Apr 2006 7:41 AM
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

Show quoteHide quote
"Monty" <monty@community.nospam> schreef in bericht
news:ej5IZxzVGHA.4792@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!
>