By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,241 Members | 799 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,241 IT Pros & Developers. It's quick & easy.

.NET ComboBox Behavior Help

P: n/a
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 OnSelectionChangeCommitted,
OnSelectedValueChanged, OnSelectedItemChanged, and
OnSelectedIndexChanged, 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

Jul 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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(DrawItemEventArgs e)
{
if (e.Index == -1)
return;
SolidBrush front;
SolidBrush back;

// determine front and back brush based on selected status
if ((e.State & DrawItemState.Selected) > 0)
{
front = new SolidBrush(SystemColors.HighlightText);
back = new SolidBrush(SystemColors.Highlight);
}
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.ComboBoxEdit) > 0 ? this.tex :
this.Items[e.Index].ToString();
e.Graphics.FillRectangle(back, e.Bounds);
e.Graphics.DrawString(s, this.Font, front, e.Bounds);

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

// each selection is appended to previously selected text
protected override void OnSelectionChangeCommitted(EventArgs e)
{
if (this.SelectedIndex > -1)
this.tex += this.Items[this.SelectedIndex];
}
}
--
Happy Coding!
Morten Wennevik [C# MVP]
Jul 21 '05 #2

P: n/a
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.

Jul 21 '05 #3

P: n/a
On 30 Dec 2004 18:04:35 -0800, hasden <bh*****@alltel.net> wrote:
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.


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]
Jul 21 '05 #4

P: n/a
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.

Jul 21 '05 #5

P: n/a
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!
Jul 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.