Focus Stuck on TextBox

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jehugaleahsa@gmail.com

    Focus Stuck on TextBox

    Hello:

    When we have a text box that is bound to an int?, the text box will
    not allow the user to leave the control until it has a valid integer
    in it. This is actually very bad. If the user wants to remove the
    value, they can't anymore. They can't even leave the form! They are
    being forced to enter 0 or some other bogus value.

    The weird thing is that it doesn't take affect until they set a value
    and lose focus on the control.

    Is there some way of shutting this off? I'm assuming it is some sort
    of funky validation.

    Thanks,
    Travis
  • =?Utf-8?B?Q2lhcmFuIE8nJ0Rvbm5lbGw=?=

    #2
    RE: Focus Stuck on TextBox

    Can you handle the Validating event of the text box, validate it yourself and
    it its ok, set e.Cancel = true;

    That should stop it being annoying.

    --
    Ciaran O''Donnell
    try{ Life(); } catch (TooDifficultException) { throw Toys(); }



    "jehugaleahsa@g mail.com" wrote:
    Hello:
    >
    When we have a text box that is bound to an int?, the text box will
    not allow the user to leave the control until it has a valid integer
    in it. This is actually very bad. If the user wants to remove the
    value, they can't anymore. They can't even leave the form! They are
    being forced to enter 0 or some other bogus value.
    >
    The weird thing is that it doesn't take affect until they set a value
    and lose focus on the control.
    >
    Is there some way of shutting this off? I'm assuming it is some sort
    of funky validation.
    >
    Thanks,
    Travis
    >

    Comment

    • jehugaleahsa@gmail.com

      #3
      Re: Focus Stuck on TextBox

      On Jul 16, 9:19 am, Ciaran O''Donnell
      <CiaranODonn... @discussions.mi crosoft.comwrot e:
      Can you handle the Validating event of the text box, validate it yourselfand
      it its ok, set e.Cancel = true;
      >
      That should stop it being annoying.
      Setting e.Cancel = true; actually causes the validation to fail - in
      other words, keeps the control in focus yet again.

      I would much rather see an error provider or error message than
      prevent the user from tabbing away.

      I mean, I could always set e.Cancel = false;, which would force
      validation to succeed. I could pop up a MessageBox to indicate the
      requirement.

      I will try that.

      Comment

      • Peter Duniho

        #4
        Re: Focus Stuck on TextBox

        On Wed, 16 Jul 2008 07:58:56 -0700, jehugaleahsa@gm ail.com
        <jehugaleahsa@g mail.comwrote:
        [...]
        Is there some way of shutting this off? I'm assuming it is some sort
        of funky validation.
        No, just plain old normal validation.

        Do you have to use the built-in binding? It's not that hard to do your
        own validation, parse the int yourself, and accept an empty textbox as a
        null for your nullable int.

        There may be a way to override the binding behavior, but in this case even
        if there was, I think it'd be easier to just handle the binding yourself
        explicitly.

        Pete

        Comment

        • jehugaleahsa@gmail.com

          #5
          Re: Focus Stuck on TextBox

          I actually found the answer to this question on MSDN.

          For one, you can allow focus to change on failed validation if you set
          the Form's AutoValidate property to EnableAllowFocu sChange.

          Secondly, I can optionally set the Binding instances to have an
          DataSourceUpdat eMode of OnPropertyChang ed. I can then turn validation
          on or off.

          I can also handle the Validating event and report issues here. If I
          set AutoValidate to EnableAllowFocu sChange, I can correctly set
          e.Cancel = true; when validation fails. I can also put an
          ErrorProvider on the form and call SetError(myCont rol, "Error
          Message") to display an exclamation with the message, or I could use a
          MessageBox.

          Finally, if you want to suppress validation until the user is ready to
          save, you can set CausesValidatio n = false for each control. Then in
          the save method you can call ValidateChildre n on the form. It will
          return true if all the controls are validated.

          It is a very flexible system. I just wish there was someone who could
          answer questions when someone like myself comes around.

          Comment

          • Peter Duniho

            #6
            Re: Focus Stuck on TextBox

            On Wed, 16 Jul 2008 10:15:05 -0700, jehugaleahsa@gm ail.com
            <jehugaleahsa@g mail.comwrote:
            [...]
            It is a very flexible system. I just wish there was someone who could
            answer questions when someone like myself comes around.
            Me too. Sorry we weren't more helpful. Please keep in mind though that
            a) this is a C# newsgroup, not a .NET newsgroup, and b) .NET is _huge_ and
            no one person knows everything about .NET. For better or worse, I doubt
            that the sort of binding you're using is something most people do
            (ironically, the more complex database-related binding seems more
            typical), and so it's not too surprising to me that there's a dearth of
            expertise on all the ins-and-outs.

            That said, you found the answer on MSDN, which is the place you should be
            looking to start with for answers to questions like these. If nothing
            else, hopefully it's a good lesson on self-reliance. :) And now you're
            the expert on binding, so when someone _else_ comes asking related
            questions, _you_ can step up and answer them.

            Pete

            Comment

            • Pavel Minaev

              #7
              Re: Focus Stuck on TextBox

              On Jul 16, 6:58 pm, "jehugalea...@g mail.com" <jehugalea...@g mail.com>
              wrote:
              Hello:
              >
              When we have a text box that is bound to an int?, the text box will
              not allow the user to leave the control until it has a valid integer
              in it. This is actually very bad. If the user wants to remove the
              value, they can't anymore. They can't even leave the form! They are
              being forced to enter 0 or some other bogus value.
              >
              The weird thing is that it doesn't take affect until they set a value
              and lose focus on the control.
              >
              Is there some way of shutting this off? I'm assuming it is some sort
              of funky validation.
              If you want to map empty string input by the user to null value in the
              int?, just set the DataSourceNullV alue property of the Binding to
              "null", and NullValue property of the binding to empty string. You
              can't do it from the form designer, so you'll have to write some code:

              textBox1.DataBi ndings["Text"].DataSourceNull Value = null;
              textBox1.DataBi ndings["Text"].NullValue = "";

              Comment

              • jehugaleahsa@gmail.com

                #8
                Re: Focus Stuck on TextBox

                If you want to map empty string input by the user to null value in the
                int?, just set the DataSourceNullV alue property of the Binding to
                "null", and NullValue property of the binding to empty string. You
                can't do it from the form designer, so you'll have to write some code:
                >
                            textBox1.DataBi ndings["Text"].DataSourceNull Value= null;
                            textBox1.DataBi ndings["Text"].NullValue = "";
                Very cool.

                Comment

                • jehugaleahsa@gmail.com

                  #9
                  Re: Focus Stuck on TextBox

                  On Jul 16, 11:26 am, "Peter Duniho" <NpOeStPe...@nn owslpianmk.com>
                  wrote:
                  On Wed, 16 Jul 2008 10:15:05 -0700, jehugalea...@gm ail.com  
                  >
                  <jehugalea...@g mail.comwrote:
                  [...]
                  It is a very flexible system. I just wish there was someone who could
                  answer questions when someone like myself comes around.
                  >
                  Me too.  Sorry we weren't more helpful.  Please keep in mind though that  
                  a) this is a C# newsgroup, not a .NET newsgroup, and b) .NET is _huge_ and  
                  no one person knows everything about .NET.  For better or worse, I doubt  
                  that the sort of binding you're using is something most people do  
                  (ironically, the more complex database-related binding seems more  
                  typical), and so it's not too surprising to me that there's a dearth of  
                  expertise on all the ins-and-outs.
                  >
                  That said, you found the answer on MSDN, which is the place you should be 
                  looking to start with for answers to questions like these.  If nothing  
                  else, hopefully it's a good lesson on self-reliance.  :)  And now you're  
                  the expert on binding, so when someone _else_ comes asking related  
                  questions, _you_ can step up and answer them.
                  >
                  Pete
                  Sorry for the attitude Pete. It wasn't my intention. You help and
                  everyone else's is always appreciated.

                  Believe me, I do always look at MSDN first; I just have a really hard
                  time finding answers to some of my more stupid questions. This time I
                  found the answer after continuing to search long after my post. Some
                  times rewording the question makes Google's engine a little smarter. I
                  just wish Google wouldn't list 3000 commercial sites before listing a
                  single forum.

                  Thank you everyone!

                  Comment

                  Working...