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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Monty

    #1

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

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


  • Marina Levit [MVP]

    #2
    Re: Easy One: Bind a Text Box to a an Integer Variable

    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" <monty@communit y.nospam> wrote in message
    news:ej5IZxzVGH A.4792@TK2MSFTN GP14.phx.gbl...[color=blue]
    > Hope this is an easy one: How can I bind a text on a form to a an integer
    > variable? Possible? Thanks!
    >[/color]


    Comment

    • Homer J Simpson

      #3
      Re: Easy One: Bind a Text Box to a an Integer Variable


      "Monty" <monty@communit y.nospam> wrote in message
      news:ej5IZxzVGH A.4792@TK2MSFTN GP14.phx.gbl...
      [color=blue]
      > Hope this is an easy one: How can I bind a text on a form to a an integer
      > variable? Possible? Thanks![/color]

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




      Comment

      • Marina Levit [MVP]

        #4
        Re: Easy One: Bind a Text Box to a an Integer Variable

        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" <nobody@nowhere .com> wrote in message
        news:tzdYf.2477 4$Ph4.21405@edt nps90...[color=blue]
        >
        > "Monty" <monty@communit y.nospam> wrote in message
        > news:ej5IZxzVGH A.4792@TK2MSFTN GP14.phx.gbl...
        >[color=green]
        >> Hope this is an easy one: How can I bind a text on a form to a an integer
        >> variable? Possible? Thanks![/color]
        >
        > ??? Why would you want to? Just update it when the variable changes.
        >
        >
        >
        >[/color]


        Comment

        • Homer J Simpson

          #5
          Re: Easy One: Bind a Text Box to a an Integer Variable


          "Marina Levit [MVP]" <someone@nospam .com> wrote in message
          news:ei3HJ20VGH A.4308@TK2MSFTN GP12.phx.gbl...
          [color=blue]
          > 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.[/color]

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



          Comment

          • Herfried K. Wagner [MVP]

            #6
            Re: Easy One: Bind a Text Box to a an Integer Variable

            "Monty" <monty@communit y.nospam> schrieb:[color=blue]
            > Hope this is an easy one: How can I bind a text on a form to a an integer
            > variable? Possible? Thanks![/color]

            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/>

            Comment

            • Marina Levit [MVP]

              #7
              Re: Easy One: Bind a Text Box to a an Integer Variable

              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" <nobody@nowhere .com> wrote in message
              news:GPeYf.2479 2$Ph4.3479@edtn ps90...[color=blue]
              >
              > "Marina Levit [MVP]" <someone@nospam .com> wrote in message
              > news:ei3HJ20VGH A.4308@TK2MSFTN GP12.phx.gbl...
              >[color=green]
              >> 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.[/color]
              >
              > Binding to a data source is logical. Binding to a program variable is odd.
              > If you update the variable, update the display.
              >
              >
              >[/color]


              Comment

              • Monty

                #8
                Re: Easy One: Bind a Text Box to a an Integer Variable

                > ??? Why would you want to? Just update it when the variable changes.[color=blue]
                >[/color]

                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.



                Comment

                • Monty

                  #9
                  Re: Easy One: Bind a Text Box to a an Integer Variable

                  Bummer. Thanks for the insight!

                  "Marina Levit [MVP]" <someone@nospam .com> wrote in message
                  news:eNsm%23b1V GHA.4720@TK2MSF TNGP15.phx.gbl. ..[color=blue]
                  > 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" <nobody@nowhere .com> wrote in message
                  > news:GPeYf.2479 2$Ph4.3479@edtn ps90...[color=green]
                  >>
                  >> "Marina Levit [MVP]" <someone@nospam .com> wrote in message
                  >> news:ei3HJ20VGH A.4308@TK2MSFTN GP12.phx.gbl...
                  >>[color=darkred]
                  >>> 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.[/color]
                  >>
                  >> Binding to a data source is logical. Binding to a program variable is
                  >> odd. If you update the variable, update the display.
                  >>
                  >>
                  >>[/color]
                  >
                  >[/color]


                  Comment

                  • Homer J Simpson

                    #10
                    Re: Easy One: Bind a Text Box to a an Integer Variable


                    "Monty" <monty@communit y.nospam> wrote in message
                    news:%23JNmi21V GHA.4596@TK2MSF TNGP15.phx.gbl. ..
                    [color=blue][color=green]
                    >> ??? Why would you want to? Just update it when the variable changes.[/color][/color]
                    [color=blue]
                    > Convenience. Why would I want to write all those event handlers?[/color]

                    How many?
                    [color=blue]
                    > Also, it's the other way around... I want to update the variable when the
                    > user changes the control's value.[/color]

                    Why not a NumericUpDown control?



                    Comment

                    • Jim Hughes

                      #11
                      Re: Easy One: Bind a Text Box to a an Integer Variable

                      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.Componen tModel.INotifyP ropertyChanged
                      Public Event PropertyChanged (ByVal sender As Object, _
                      ByVal e As System.Componen tModel.Property ChangedEventArg s) _
                      Implements System.Componen tModel.INotifyP ropertyChanged. PropertyChanged

                      Private _pageno As Integer = 1
                      <System.Compone ntModel.Bindabl e(True)> _
                      Public Property PageNo() As Integer
                      Get
                      Return _pageno
                      End Get
                      Set(ByVal value As Integer)
                      _pageno = value
                      RaiseEvent PropertyChanged (Me, New
                      System.Componen tModel.Property ChangedEventArg s("PageNo"))
                      End Set
                      End Property

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

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

                      End Class ' Form1

                      "Monty" <monty@communit y.nospam> wrote in message
                      news:ej5IZxzVGH A.4792@TK2MSFTN GP14.phx.gbl...[color=blue]
                      > Hope this is an easy one: How can I bind a text on a form to a an integer
                      > variable? Possible? Thanks!
                      >[/color]


                      Comment

                      • Monty

                        #12
                        Re: Easy One: Bind a Text Box to a an Integer Variable

                        Interesting, thanks Jim.

                        "Jim Hughes" <NOSPAMJ3033@Ho tmail.com> wrote in message
                        news:%23eDQQr2V GHA.5468@TK2MSF TNGP14.phx.gbl. ..[color=blue]
                        > 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.Componen tModel.INotifyP ropertyChanged
                        > Public Event PropertyChanged (ByVal sender As Object, _
                        > ByVal e As System.Componen tModel.Property ChangedEventArg s) _
                        > Implements System.Componen tModel.INotifyP ropertyChanged. PropertyChanged
                        >
                        > Private _pageno As Integer = 1
                        > <System.Compone ntModel.Bindabl e(True)> _
                        > Public Property PageNo() As Integer
                        > Get
                        > Return _pageno
                        > End Get
                        > Set(ByVal value As Integer)
                        > _pageno = value
                        > RaiseEvent PropertyChanged (Me, New
                        > System.Componen tModel.Property ChangedEventArg s("PageNo"))
                        > End Set
                        > End Property
                        >
                        > Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
                        > System.EventArg s) _
                        > Handles MyBase.Load
                        > ' updates form title whenever PageNo is changed
                        > Me.DataBindings .Add(New Binding("Text", Me, "PageNo"))
                        > End Sub
                        >
                        > Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
                        > System.EventArg s) _
                        > Handles Button2.Click
                        > ' Increment PageNo
                        > Me.PageNo += 1
                        > End Sub
                        >
                        > End Class ' Form1
                        >
                        > "Monty" <monty@communit y.nospam> wrote in message
                        > news:ej5IZxzVGH A.4792@TK2MSFTN GP14.phx.gbl...[color=green]
                        >> Hope this is an easy one: How can I bind a text on a form to a an integer
                        >> variable? Possible? Thanks!
                        >>[/color]
                        >
                        >[/color]


                        Comment

                        • Cor Ligthert [MVP]

                          #13
                          Re: Easy One: Bind a Text Box to a an Integer Variable

                          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" <monty@communit y.nospam> schreef in bericht
                          news:ej5IZxzVGH A.4792@TK2MSFTN GP14.phx.gbl...[color=blue]
                          > Hope this is an easy one: How can I bind a text on a form to a an integer
                          > variable? Possible? Thanks!
                          >[/color]


                          Comment

                          Working...