Question marking substring of combox.text

aw48

Active member
Joined
Feb 9, 2013
Messages
36
Location
Germany
Programming Experience
10+
hi,
is it possible to select a substring of a selected combobox-item by clicking it with the mouse and selecting a specific amount of characters. As I see clicking the text does not the cursor at the corresponding place.
Any suggestions or is there a different approach
thanks in advance
 
If you set the DropDownStyle property to DropDownList then the standard ComboBox will only allow you to select an item from the drop-down list. If you set it to DropDown, which is the default, then the main part of the control works like a TextBox, including allowing you to select part of the text. The SelectionStart, SelectionLength and SelectedText properties work exactly the same way. What is not possible with the standard ComboBox control is allowing the user to select arbitrary text without letting them enter arbitrary text. If the entry of arbitrary text is an issue then you'd have to write code to validate input against the drop-down list.
 
I do have DropDown and I can set the cursor, but will change its position on moving the mouse
the combobox items are urls
 
It's got nothing to do with the mouse. It seems that the ComboBox effectively clears its selection when it loses focus and then selects everything when it receives focus again. In testing, I determined that the selection is maintained when the Leave event is raised but lost when the Enter event is raised. Resetting it on the Enter event doesn't work, so the full text is obviously selected after that. Resetting it on the GotFocus event, on the other hand, does work. As such, you can do this to get the effect you want:
C#:
int ss;
int sl;

private void comboBox1_Leave(object sender, EventArgs e)
{
    // Remember the selection.
    ss = comboBox1.SelectionStart;
    sl = comboBox1.SelectionLength;
}

private void comboBox1_GotFocus(object sender, EventArgs e)
{
    // Reinstate the selection.
    comboBox1.SelectionStart = ss;
    comboBox1.SelectionLength = sl;
}
You'd have to use the fields to get the values while the control doesn't have focus, e.g. on the Click of a Button. You might add a field for SelectedText in that case, but you can always use Substring with those Integer fields.
 
You might choose to create a custom control and use that instead of the standard ComboBox, especially if you need it for multiple controls:
C#:
public class ComboBoxEx : ComboBox
{
    public int UnfocusedSelectionStart { get; set; }
    public int UnfocusedSelectionLength { get; set; }
    public string UnfocusedSelectedText { get; set; }

    protected override void OnLeave(EventArgs e)
    {
        base.OnLeave(e);

        UnfocusedSelectionStart = SelectionStart;
        UnfocusedSelectionLength = SelectionLength;
        UnfocusedSelectedText = SelectedText;
    }

    protected override void OnGotFocus(EventArgs e)
    {
        base.OnGotFocus(e);

        SelectionStart = UnfocusedSelectionStart;
        SelectionLength = UnfocusedSelectionLength;
    }
}
 
hi thanks a lot
there's absolutely no way to select a portion of the text displayed
I'm writing on a custom combobox that will behave like a textbox but I guess it's a bit tricky
at the moment I have a textbox of the comboboxes width below the combobox to show the seleted item and I can do the selection like I want to
greetings from cold germany
 
I'm writing on a custom combobox
That's rather important information to provide at the outset. It's a waste of our time and yours for us to provide advice on using a control that you're not using.
 
That's rather important information to provide at the outset. It's a waste of our time and yours for us to provide advice on using a control that you're not using.
And some people wonder why I ask a lot of questions, and why I am so pushy for specifics from the get-go. Although I don't think anyone could have seen that one coming. A good way to end a topic nonetheless. ;)
 
hi,
@jmcilhinney
sorry, you got me wrong. the code you provided made me decide to write a custom combobox
one whose edit field will behave like a textbox but at the moment using an additional textbox is ok with me though it looks rather unprofessional
 
Are you sure that it is really a combobox that you need, or are you trying to implement something closer to autocomplete?
 
Back
Top Bottom