.NET ComboBox Behavior Help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • hasden

    .NET ComboBox Behavior Help

    I'm working in .NET and I can't seem to get the behavior that I need
    from the combobox. I'm sure it's possible, I just don't have the .NET
    experience. Any help would be greatly appreciated. I need a combobox
    with the following behavior:

    - The user can type in any text they want
    - When the user clicks the drop down button and selects an item from
    the combobox the text for that item should be appended to the end of
    the text already existing in the combo box.

    For example: if the user types 'hello' into the combo box and then
    clicks the drop down button and selects 'world', the control would then
    append 'world' to the end of the existing 'hello' to end with 'hello
    world'. I've tried overriding OnSelectionChan geCommitted,
    OnSelectedValue Changed, OnSelectedItemC hanged, and
    OnSelectedIndex Changed, but to no avail. When I modify the .Text
    property of the combobox while inside any of those events it does not
    appear to change (I can force the change by refreshing the control with
    Invalidate() or Refresh()), but once it exists the event, the .Text
    property is then overwritten with the text that the user clicked from
    the drop down. I'm sure this all sounds crazy easy, but I just can't
    get it. Once again, I appreciate everyone's help. Hope you all had a
    happy holiday season.

    Respectfully,
    Brian Hasden

  • Morten Wennevik

    #2
    Re: .NET ComboBox Behavior Help

    Hi Brian,

    It's certainly an odd ComboBox and one that might be easier to create
    using a separate TextBox for the user typing.

    The problem is that once you select anything the selected value that is
    drawn is whatever is on the SelectedIndex position of the Items property.
    To override any ComboBox editbox feature you need to specify DrawMode to
    either of the OwnerDrawn ones. However, you can't draw inside the editbox
    unless you also specify DropDownList as the DropDownStyle, which
    eliminated easy user typing.

    This ComboBox will let you append selected words to whatever text is
    already written inside the editbox, but won't let you type anything of
    your own. Note however that using the up/down arrow keys each item in
    turn will be selected and appended, and there is no undo option.

    When you do ownerdrawn comboboxes you are responsible to draw selection
    backgrounds and highlights or the ComboBox might look odd.

    public class MyCombo : ComboBox
    {
    // this holds what you be shown in the editbox
    private string tex = "";

    // this method will be called once for each item in the dropdown list
    // as well as the editbox if dropdownlist is specified
    protected override void OnDrawItem(Draw ItemEventArgs e)
    {
    if (e.Index == -1)
    return;
    SolidBrush front;
    SolidBrush back;

    // determine front and back brush based on selected status
    if ((e.State & DrawItemState.S elected) > 0)
    {
    front = new SolidBrush(Syst emColors.Highli ghtText);
    back = new SolidBrush(Syst emColors.Highli ght);
    }
    else
    {
    front = new SolidBrush(this .ForeColor);
    back = new SolidBrush(this .BackColor);
    }

    // if we are drawing the editbox use the appended text
    string s = (e.State & DrawItemState.C omboBoxEdit) > 0 ? this.tex :
    this.Items[e.Index].ToString();
    e.Graphics.Fill Rectangle(back, e.Bounds);
    e.Graphics.Draw String(s, this.Font, front, e.Bounds);

    back.Dispose();
    front.Dispose() ;
    }

    // each selection is appended to previously selected text
    protected override void OnSelectionChan geCommitted(Eve ntArgs e)
    {
    if (this.SelectedI ndex > -1)
    this.tex += this.Items[this.SelectedIn dex];
    }
    }


    --
    Happy Coding!
    Morten Wennevik [C# MVP]

    Comment

    • hasden

      #3
      Re: .NET ComboBox Behavior Help

      Well that's not exactly what I was looking for but thanks anyways. One
      more question real quick if you don't mind. What is the problem with
      having a variable to hold what the combo box should be displaying and
      polling to make sure that the combobox equals that variable? I know
      polling isn't the best idea, but it would create the functionality that
      I really need.

      Comment

      • Morten Wennevik

        #4
        Re: .NET ComboBox Behavior Help

        On 30 Dec 2004 18:04:35 -0800, hasden <bhasden@alltel .net> wrote:
        [color=blue]
        > Well that's not exactly what I was looking for but thanks anyways. One
        > more question real quick if you don't mind. What is the problem with
        > having a variable to hold what the combo box should be displaying and
        > polling to make sure that the combobox equals that variable? I know
        > polling isn't the best idea, but it would create the functionality that
        > I really need.
        >[/color]

        No problem, and may even be the best solution for what you want to do.
        All my samples end up with the Text property of the ComboBox being
        updated after any of the events I have tried effectively overruling any
        manual changes. You could activate a timer in the event and set the
        Text property when the timer sets in.


        --
        Happy Coding!
        Morten Wennevik [C# MVP]

        Comment

        • hasden

          #5
          Re: .NET ComboBox Behavior Help

          Yeah that's exactly what happened to me. I couldn't trap any event that
          let me have final say over what the Text property ended up with. Thank
          you for all your help. I really just needed confirmation that I wasn't
          going crazy and couldn't find/handle the events correctly. Thanks again.

          Comment

          • Vishal Saboo

            #6
            Re: .NET ComboBox Behavior Help

            We have a similar situation (in a .NET, C# application). We have an
            application with a few drop downs that have a few thousand items. The
            ..NET combo box lets the user type in the first letter to narrow the list
            down...however, with a very long list, the user still has to scroll a
            good bit to get to the item they are looking for. Obviously, this is
            tedious and undesirable.

            Anyone has any ideas, how we could give our users the desired
            functionality?

            Thank you.



            *** Sent via Developersdex http://www.developersdex.com ***
            Don't just participate in USENET...get rewarded for it!

            Comment

            Working...