VB.Net - Focus

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • krndhi1983
    New Member
    • Jun 2007
    • 22

    #1

    VB.Net - Focus

    Hi to all
    I am facing this kind of problem.

    When i write the following code

    TextBox1.Focus( )

    If the text box already contains any values the cursor will be placed below the first character of the string of textbox.I need to place the cursor at the end of the string in the textbox.
    Any one can response me?

    Sorry for the bad English.
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    I suggest using client side code. HTH.

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Originally posted by krndhi1983
      Hi to all
      I am facing this kind of problem.

      When i write the following code

      TextBox1.Focus( )

      If the text box already contains any values the cursor will be placed below the first character of the string of textbox.I need to place the cursor at the end of the string in the textbox.
      Any one can response me?

      Sorry for the bad English.
      This is going to require a lot of JavaScript.
      Please see this thread on text box cursor location...I think it may help you.

      This may also help you understand how to use JavaScript in .NET


      -Frinny

      Comment

      • TRScheel
        Recognized Expert Contributor
        • Apr 2007
        • 638

        #4
        The others have already started answering this question, but I am wondering, is this a web application? or a desktop application?

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          I found this on an msdn forums when looking for javascript selection
          [code=html]
          <HTML>
          <HEAD>
          <TITLE> Select Test </TITLE>
          </HEAD>
          <SCRIPT LANGUAGE="JavaS cript">
          <!--
          function selectSomeText( element){
          var workRange=docum ent.selection.c reateRange();
          var allRange=elemen t.createTextRan ge();
          workRange.setEn dPoint("StartTo Start",allRange );
          workRange.selec t();
          }
          function getCaretPositio n(element){
          var workRange=docum ent.selection.c reateRange();
          var allRange=elemen t.createTextRan ge();
          workRange.setEn dPoint("StartTo Start",allRange );
          return workRange.text. length;
          }
          //-->
          </SCRIPT>
          <BODY>
          <form method="GET" name="Login">
          <pre>
          <input type="type" value ="This is the user name" onclick="select SomeText(this)" style="width:30 0px"><br>
          <input type="type" value ="abcdefghijklm nopqrstuvw" onclick="alert( getCaretPositio n(this))" style="width:30 0px">
          </pre>
          </form>
          </BODY>
          </HTML>
          [/code]

          You should be able to pull what you need from out of that

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            Originally posted by Plater
            I found this on an msdn forums when looking for javascript selection
            [code=html]
            <HTML>
            <HEAD>
            <TITLE> Select Test </TITLE>
            </HEAD>
            <SCRIPT LANGUAGE="JavaS cript">
            <!--
            function selectSomeText( element){
            var workRange=docum ent.selection.c reateRange();
            var allRange=elemen t.createTextRan ge();
            workRange.setEn dPoint("StartTo Start",allRange );
            workRange.selec t();
            }
            function getCaretPositio n(element){
            var workRange=docum ent.selection.c reateRange();
            var allRange=elemen t.createTextRan ge();
            workRange.setEn dPoint("StartTo Start",allRange );
            return workRange.text. length;
            }
            //-->
            </SCRIPT>
            <BODY>
            <form method="GET" name="Login">
            <pre>
            <input type="type" value ="This is the user name" onclick="select SomeText(this)" style="width:30 0px"><br>
            <input type="type" value ="abcdefghijklm nopqrstuvw" onclick="alert( getCaretPositio n(this))" style="width:30 0px">
            </pre>
            </form>
            </BODY>
            </HTML>
            [/code]

            You should be able to pull what you need from out of that

            That could have been very useful when I was trying to implement this!
            Of course I always have to do things the hard way first.
            I guess that's just how I learn :)

            Thanks Plater!

            -Frinny

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              I just made it better
              [code=javascript]
              function MoveToEnd(eleme nt)
              {
              var ee=element;
              var eelen =(ee.value.leng th);
              setCaretTo(ee,e elen);
              }
              function setCaretTo(obj, pos)
              {
              if(obj.createTe xtRange)
              {
              var range = obj.createTextR ange();
              range.move('cha racter', pos);
              range.select();
              }
              else if(obj.selectio nStart)
              {
              obj.focus();
              obj.setSelectio nRange(pos, pos);
              }
              }
              [/code]
              Use whatever means you want to get the textbox element (I used .getElementById () to pass in to movetoend)

              Comment

              Working...