locked
Combox set cursor at end of text RRS feed

  • Question

  • Hi all,

    How can I position the cursor at the end of combobox text after item selected

    I'm trying this but is does not work

    Private Sub ComboBox1_SelectedValueChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedValueChanged	
        ComboBox1.SelectionStart = ComboBox1.Text.Length
    End Sub
    
    ' to prevent user input on ComboBox 
    Private Sub ComboBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ComboBox1.KeyPress
         e.Handled = True
    End Sub
    Thanks for help


    Saturday, May 19, 2018 12:00 PM

Answers

  • Check this solution too:

    Private Sub ComboBox1_SelectedValueChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedValueChanged
       BeginInvoke(Sub() ComboBox1.SelectionStart = ComboBox1.Text.Length)
    End Sub



    • Edited by Viorel_MVP Saturday, May 19, 2018 5:16 PM
    • Proposed as answer by Reed KimbleMVP Sunday, May 20, 2018 12:18 PM
    • Marked as answer by Claudio111 Monday, May 21, 2018 11:02 PM
    Saturday, May 19, 2018 5:15 PM
  • Hi all

    What I wish is the user to select an item that cannot be editable.

    Then have the carot at the end of text. and not text selected

    For now I get no caret and full text selected (with blue blackcolor) 

    Hi Caludio111,

    Do you want to get this?

    If yes, you can do like this:

     Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
            SendKeys.Send("{End}")
        End Sub

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Cor Ligthert Monday, May 21, 2018 2:03 PM
    • Marked as answer by Claudio111 Monday, May 21, 2018 10:56 PM
    Monday, May 21, 2018 5:42 AM

All replies

  • By default, the cursor will be positioned at the end of the text after selecting an item, so no code is necessary.

    Adding that KeyPress event handler makes it appear like you want the user to only select items from the existing combobox items collection.  If that is the desired behavior then you should set the DropDownStyle property to DropDownList.

    If you are trying to achieve some other result, please explain more about what you want to do.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Saturday, May 19, 2018 1:06 PM
  • You could use a timer. However it would be simplest to alter the dropdown style of the ComboBox such that it is not user editable.

    Option Strict On
    
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.Location = New Point(CInt((Screen.PrimaryScreen.WorkingArea.Width / 2) - (Me.Width / 2)), CInt((Screen.PrimaryScreen.WorkingArea.Height / 2) - (Me.Height / 2)))
    
            ComboBox1.Items.Add("Apple")
            ComboBox1.Items.Add("Orange")
            ComboBox1.Items.Add("Pear")
            ComboBox1.Items.Add("Banana")
            Timer1.Interval = 20
        End Sub
    
        Private Sub ComboBox1_SelectedValueChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedValueChanged
            Timer1.Enabled = True
        End Sub
    
        ' to prevent user input on ComboBox 
        Private Sub ComboBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ComboBox1.KeyPress
            e.Handled = True
        End Sub
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            Timer1.Enabled = False
            ComboBox1.SelectionLength = 0
            ComboBox1.SelectionStart = ComboBox1.Text.Length
        End Sub
    
    End Class


    La vida loca

    Saturday, May 19, 2018 4:56 PM
  • By default, the cursor will be positioned at the end of the text after selecting an item, so no code is necessary.


    @Reed,

    I get a full selected text without any caret. Maybe because of a setting somewhere from me, which I don't like or maybe because I use W10. Maybe you know more. I keep out of this thread as long the user has not replied to you. 

    :-)


    Success
    Cor

    Saturday, May 19, 2018 5:12 PM
  • Check this solution too:

    Private Sub ComboBox1_SelectedValueChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedValueChanged
       BeginInvoke(Sub() ComboBox1.SelectionStart = ComboBox1.Text.Length)
    End Sub



    • Edited by Viorel_MVP Saturday, May 19, 2018 5:16 PM
    • Proposed as answer by Reed KimbleMVP Sunday, May 20, 2018 12:18 PM
    • Marked as answer by Claudio111 Monday, May 21, 2018 11:02 PM
    Saturday, May 19, 2018 5:15 PM
  • @Reed,

    I get a full selected text without any caret.

    Yes, the text is selected and the caret is at the end of the text.  It can be hard to see without looking very closely.  This is of course with a DropDownStyle of DropDown.  I'm on Win10 as well, but AFAIK this is the normal behavior with that drop down style.

    I'm guessing the OP is actually trying to do away with the highlight, which would require setting the SelectionLength to zero rather than modifying the SelectionStart.  But the selection seems to be something the control does after is raises all "change" related events. Viorel's suggestion may work since it should execute right after the control finishes its internal logic.  But there's really no reason for the extra code if all they really need is a different dropdown style.

    As you say, until they post back it is all guessing at this point.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Saturday, May 19, 2018 8:55 PM
  • Hi all

    What I wish is the user to select an item that cannot be editable.

    Then have the carot at the end of text. and not text selected

    For now I get no caret and full text selected (with blue blackcolor) 

    Sunday, May 20, 2018 11:23 AM

  • What I wish is the user to select an item that cannot be editable.


    Hi,

    try the DropDownStyle "DropDownList" [as Reed already suggested].

    Regards,

      Thorsten



    Sunday, May 20, 2018 11:32 AM
  • Yes this is ok but I still have the full text selected and no carot at the end of text.

    The result I wish is like the one a get if, after item selected in combobox,  I set focus on next control with

    SendKeys.Send("{TAB}")

    Sunday, May 20, 2018 11:40 AM
  • Hi,

    so then handle the SelectedIndexChanged event of that combobox with dropdownstyle DropDownList and in this event-handler set the Focus to what ever Control you wish, eg:

        Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
            Me.Button1.Focus()
        End Sub

    Regards,

      Thorsten

    Sunday, May 20, 2018 12:04 PM
  • Yes this is ok but I still have the full text selected and no carot at the end of text.

    The result I wish is like the one a get if, after item selected in combobox,  I set focus on next control with

    SendKeys.Send("{TAB}")


    There is no highlight if you use DropDownList for the DropDownStyle, as I have been suggesting from the beginning.  The combobox will have a gray background and there will be no highlight or caret. 

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Sunday, May 20, 2018 12:17 PM
  • ok Reed, >I have to say that i dont use standard graphics( i dont like it)  but  I'm using this code 

    Public Class Form1
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
    	'''''''''ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
    
    		ComboBox1.FlatStyle = FlatStyle.Flat
    		AddHandler Panel1.Paint, AddressOf gFocus
    	End Sub
    
    	Private Sub ComboBox1_SelectedValueChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedValueChanged
    
    		ComboBox1.SelectionStart = ComboBox1.Text.Length
    		''''''''SendKeys.Send("{TAB}") ' simula TAB
    
    
    	End Sub
    	''''' to prevent user input 
    	Private Sub ComboBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ComboBox1.KeyPress
    		e.Handled = True
    	End Sub
    
    	
    	Private Sub gFocus(ByVal sender As Object, ByVal e As PaintEventArgs)
    		Dim rect As New Rectangle(Me.ComboBox1.Location.X - 1, Me.ComboBox1.Location.Y - 1, Me.ComboBox1.Width + 1, Me.ComboBox1.Height + 1)
    		Dim pen As New Pen(Color.DimGray, 1)
    		Dim g As Graphics = e.Graphics
    		g.DrawRectangle(pen, rect)
    		Me.ComboBox1.Refresh()
    	End Sub
    End Class

    and I get full text selected and no Carot. 

    Any idea to have a carot at the end of text (highlighted)

    Sunday, May 20, 2018 6:54 PM
  • There are pinvoke functions to set a carat but I believe they only work on focused control.

    I could not find a paint event, even in custom control inheriting ComboBox, that allows drawing in the display area of the ComboBox. Not even for owner drawn ComboBox.

    This is C# converted to VB.Net code for a Caret. It would place a Caret in the ComboBox but there was no event to constantly place one there even when the control is not in Focus. And you have to destroy the carets apparently to keep from using resources overtime and not destroying previously used carets.

    Imports System
    Imports System.Drawing
    Imports System.Windows.Forms
    Imports System.Runtime.InteropServices
    
    Public Class MyWidget
        Inherits Control
    
        Public Sub New()
            Me.BackColor = Color.Yellow
        End Sub
    
        Protected Overrides Sub OnGotFocus(ByVal e As EventArgs)
            CreateCaret(Me.Handle, IntPtr.Zero, 2, Me.Height - 2)
            SetCaretPos(2, 1)
            ShowCaret(Me.Handle)
            MyBase.OnGotFocus(e)
        End Sub
    
        Protected Overrides Sub OnLostFocus(ByVal e As EventArgs)
            DestroyCaret()
            MyBase.OnLostFocus(e)
        End Sub
    
        <DllImport("user32.dll", SetLastError:=True)>
        Private Shared Function CreateCaret(ByVal hWnd As IntPtr, ByVal hBmp As IntPtr, ByVal w As Integer, ByVal h As Integer) As Boolean
        End Function
        <DllImport("user32.dll", SetLastError:=True)>
        Private Shared Function SetCaretPos(ByVal x As Integer, ByVal y As Integer) As Boolean
        End Function
        <DllImport("user32.dll", SetLastError:=True)>
        Private Shared Function ShowCaret(ByVal hWnd As IntPtr) As Boolean
        End Function
        <DllImport("user32.dll", SetLastError:=True)>
        Private Shared Function DestroyCaret() As Boolean
        End Function
    End Class


    La vida loca

    Sunday, May 20, 2018 9:51 PM
  • Hi all

    What I wish is the user to select an item that cannot be editable.

    Then have the carot at the end of text. and not text selected

    For now I get no caret and full text selected (with blue blackcolor) 

    Hi Caludio111,

    Do you want to get this?

    If yes, you can do like this:

     Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
            SendKeys.Send("{End}")
        End Sub

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Cor Ligthert Monday, May 21, 2018 2:03 PM
    • Marked as answer by Claudio111 Monday, May 21, 2018 10:56 PM
    Monday, May 21, 2018 5:42 AM
  • Yesssssssssss Cherry, wow,  so simple, so wonderful !

    Thanks very very much Cherry

    Claudio

    Monday, May 21, 2018 9:51 AM
  • Yesssssssssss Cherry, wow,  so simple, so wonderful !

    Thanks very very much Cherry

    Claudio

    If your issue is resolved, please close your thread by clicking the Mark as answer link at the bottom of one or more relevant posts.

    It's also worth noting that the solution provided by Viorel_ three days ago should have the same effect.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Monday, May 21, 2018 12:39 PM
  • Cherry,

    If you found this yourself without Internet of help from some others, then I give you my compliments. 

    It is just like the egg of Columbus.

    :-)

    Strange is of course that the also working solution from Viorel was never tried. 

    )-:


    Success
    Cor




    Monday, May 21, 2018 2:02 PM
  • Solution By Viorel did not work to me. I have to tray it again if it gives you the same effect.
    Monday, May 21, 2018 10:58 PM
  • I tried Solution by Viorel now and it works.

    Maybe It did not work before due to too many change in program in the bad combination.

    Thank to Viorel too.

    Monday, May 21, 2018 11:02 PM