none
how to set cursor position in vb.net textbox

    Question

  •  

    Hi..
    In my application which is written on vb.net,i search for a word by typing the same in textbox... here i have doubt to focus the cursor in the text box.. if i press first letter, say 'a', in textbox that will automatically search what are the words start with 'a'...but after searching, cursor is not focussed in the textbox and to type another letter, say 'b', i need to bring my mouse pointer over the textbox and click so that i can type another letter...how can i keep my cursor focussed in the textbox unless i click somewhere else...your any suggestions in solving this problem will help me alot..

    i have already used textbox.focus() but it didn't solve my problem..

    Thursday, January 31, 2008 10:57 AM

Answers

  • It is probably another piece of code that is causing to shift the cursor from the TextBox. TextBox control has a Select() method that can you could use for the said purpose. TextBox1.Select(0, TextBox1.Text.Length) will put the cursot at the end of the TextBox content. Add this line after any KeyPress or at the end of your search routine.

    Friday, February 01, 2008 7:03 PM

All replies

  • Could you explain what other control is stealing the focus from your textbox?  I'm guessing it is some kind of list control that becomes filled with words, but are you selecting an item in that list after populating?  It seems odd that trying textbox.focus  after doing...  whatever it is your doing with words...  does not refocus.  Is the textbox disabled at that point -- or the textbox's parent control?

    Thursday, January 31, 2008 12:41 PM
  • sorry for not making myself clear before...

    I am not selecteing any item in the list and it is not list control which becomes filled with words..but...actually.

    I can view a big sorted list of products in my application and each row in this list has details about a different product. To look for a product, there is a search box and when you type a string in it,  it select the row in the sorted list which product's number matches the entered string...

     

    Once you click in the search box, ideally the cursor should remain focussed in the search box unless you click somewhere else and it is the case on most of the mahines i tested my code on. but, as reported by some users, the cursor get lost after typing every single letter and they have to bring their moust again to the search box to type another letter...

    ..any idea what could be the reason behind this...???

     

     

     

    Friday, February 01, 2008 5:29 PM
  • It is probably another piece of code that is causing to shift the cursor from the TextBox. TextBox control has a Select() method that can you could use for the said purpose. TextBox1.Select(0, TextBox1.Text.Length) will put the cursot at the end of the TextBox content. Add this line after any KeyPress or at the end of your search routine.

    Friday, February 01, 2008 7:03 PM
  • Hi,

     

    You might want to consider using the AutoComplete functionality for the TextBox control.  I love it.

     

    All you need to do is create and load an AutoCompleteStringCollection object.  If you refer to MSDN, you'll see it's very easy to do.

     

    Then you just set a few properties of the TextBox as in the following code taken from one of my applications:

     

       txtCity.AutoCompleteMode = AutoCompleteMode.SuggestAppend

       txtCity.AutoCompleteSource = AutoCompleteSource.CustomSource

       txtCity.AutoCompleteCustomSource = New CityTable().GetAutoCompleteSource

     

    In the above example, I have a class called CityTable that loads all the cities from a database table into a DataTable, but also has an AutoCompleteSource method that creates and returns an AutoStringCollection object to be used for the source.  Loading this collection can be as simple as:

     

       Private m_AutoCompleteSource As New AutoCompleteStringCollection()

     

       For Each row As DataRow In CityTable.Rows

          m_AutoCompleteSource.Add(row("Name").ToString())

       Next

     

    Just an idea to throw at you,

    Greg

    Sunday, February 03, 2008 4:10 AM
  • You have to use imports.system.windows.forms then Textbox1.Focus()

    -ivax
    Friday, May 15, 2009 6:54 AM