473,396 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

.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 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
5 2165
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: jean | last post by:
hi: i am developing a custom combobox for my company's needs that is made up of a textbox, listbox, button. i am using c#. everything is fine except for one issue. in a normal combobox,...
5
by: Steve | last post by:
I have an unbound combobox in the form header of a continuous form. The selection in the combobox sets the where clause in a querydef which determines QryPFrmInventoryManagement. The following code...
7
by: charliewest | last post by:
Using .Net CF, i have created a 2 dimension ArrayList, and "binded" this list to a ComboBox control using the "DataSource" property. I have set the DisplaySource and ValueMember properties as well....
9
by: Strahimir Antoljak | last post by:
Could anyone help me with the following: Is there a quick, or at least known way to force a ComboBox, having DropDownStyle= DropDownList, to recognize keyboard input and perform Item selection...
4
by: Strahimir Antoljak | last post by:
Has anyone experienced problems with a combo box SelectIndex property? Is there a limit to the number of Items for a combo box? Namely, when I set programmatically ComboBox.SelectIndex...
5
by: ross kerr | last post by:
Hi All, I am extending the combobox to create a control that selects an item based on the text the user is typing into the text area of the control. I have an issue that occurs only when i...
0
by: dbuchanan | last post by:
ComboBox databindng Problem == How the ComboBox is setup and used: My comboBox is populated by a lookup table. The ValueMember is the lookup table's Id and the DisplayMember is the text from a...
5
by: hasden | last post by:
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...
6
by: dbuchanan | last post by:
VS2005 I've been reading all the help I can on the topic (MSDN, other) but I can't make sense of this. Desired behavior; The user is to choose from the displayed list of the databound combobox...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.