473,836 Members | 2,189 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ComboBox - Searching the List Box

I am having trouble using the ComboBox in my app. I am attempting to use the
text from the ComboBox, to search the list box portion. However: In my
"TextChange d" event: I cannot get the value of the entered text. Below is
my code.

private void cbEdit_TextChan ged(object sender, System.EventArg s e)
{
bool bFound = false;
string sText = cbEdit.Text;
int iPosition = cbEdit.Selectio nStart;
Nov 17 '05 #1
5 4146
If your cbEdit Combobox does not have its DropDownStyle property set to
DropDownList, it should works.
but if your cbEdit Combobox has its DropDownStyle property set to
DropDownList, no TextChanged event would be fired.
Use the following lines instead :

private void cbEdit_Selected IndexChanged(ob ject sender, System.EventArg s e)
{
int iIndex = cbEdit.Selected Index;
string sText = cbEdit.Items[iIndex];
Nov 17 '05 #2
Ludovic:

Thank you for your response: but I am trying to get the value of the
entered text, not the value that has been selected. I want to capture the
value of the text when a user types in a value, not selects it from the drop
down list.

Thanks
Andy

"Ludovic SOEUR" wrote:
If your cbEdit Combobox does not have its DropDownStyle property set to
DropDownList, it should works.
but if your cbEdit Combobox has its DropDownStyle property set to
DropDownList, no TextChanged event would be fired.
Use the following lines instead :

private void cbEdit_Selected IndexChanged(ob ject sender, System.EventArg s e)
{
int iIndex = cbEdit.Selected Index;
string sText = cbEdit.Items[iIndex];
.
.
.
}

Hope it helps,

Ludovic SOEUR.
"Andy" <An**@discussio ns.microsoft.co m> a écrit dans le message de
news:D8******** *************** ***********@mic rosoft.com...
I am having trouble using the ComboBox in my app. I am attempting to use

the
text from the ComboBox, to search the list box portion. However: In my
"TextChange d" event: I cannot get the value of the entered text. Below

is
my code.

private void cbEdit_TextChan ged(object sender, System.EventArg s e)
{
bool bFound = false;
string sText = cbEdit.Text;
int iPosition = cbEdit.Selectio nStart;
.
.
.
}

my sText value is always "", even when I enter a value.

Any help would be appreciated


Nov 17 '05 #3
I tried the following lines and it works perfectly. Maybe you should provide
your source code to see what's wrong.

using System;
public class Form1 : System.Windows. Forms.Form {
static void Main() {System.Windows .Forms.Applicat ion.Run(new Form1());}
private System.Windows. Forms.ComboBox comboBox1;
private System.Windows. Forms.TextBox textBox1;
public Form1() {
comboBox1 = new System.Windows. Forms.ComboBox( );
comboBox1.Items .AddRange(new object[] {"abc","def","g hi","jkl"});
comboBox1.Locat ion = new System.Drawing. Point(0, 0);
comboBox1.Size = new System.Drawing. Size(300,20);
comboBox1.TextC hanged += new
System.EventHan dler(this.combo Box1_TextChange d);
Controls.Add(th is.comboBox1);
textBox1 = new System.Windows. Forms.TextBox() ;
textBox1.Locati on = new System.Drawing. Point(0, 30);
textBox1.Size = new System.Drawing. Size(300, 20);
Controls.Add(th is.textBox1);
ClientSize = new System.Drawing. Size(320, 60);
}
private void comboBox1_TextC hanged(object sender, System.EventArg s e) {
textBox1.Text=c omboBox1.Text;
}
}

Hope it helps,

Ludovic SOEUR.
Nov 17 '05 #4
Ludovic:

Here is my code. I declared my combo box using the GUI as cbEdit. I have
the dropdownstyle set to DropDown. The sText Variable is always "", even if
I enter a value in the ComboBox textbox on the Windows Form.

Here is my event handler declaration for the text changed event:
this.cbEdit.Tex tChanged += new System.EventHan dler(this.cbEdi t_TextChanged);

Here is my Method:

private void cbEdit_TextChan ged(object sender, System.EventArg s e)
{
bool bFound = false;
string sText = cbEdit.Text;
int iPosition = cbEdit.Selectio nStart;
if(iPosition != 0)
{
bFound = false;
for(int i = 0; i < cbEdit.Items.Co unt; i++)
{
if(cbEdit.Items[i].ToString().Sub string(0, iPosition).ToUp per()
==
sText.Substring (0, iPosition).ToUp per())
{
cbEdit.Text = cbEdit.Items[i].ToString();
cbEdit.Selectio nStart = iPosition;
bFound = true;
break;
}
}
if(!bFound)
{
cbEdit.Text = sText.Substring (0, iPosition) +
sText.Substring (iPosition + 1);
cbEdit.Selectio nStart = iPosition - 1;
}
}
}
"Ludovic SOEUR" wrote:
I tried the following lines and it works perfectly. Maybe you should provide
your source code to see what's wrong.

using System;
public class Form1 : System.Windows. Forms.Form {
static void Main() {System.Windows .Forms.Applicat ion.Run(new Form1());}
private System.Windows. Forms.ComboBox comboBox1;
private System.Windows. Forms.TextBox textBox1;
public Form1() {
comboBox1 = new System.Windows. Forms.ComboBox( );
comboBox1.Items .AddRange(new object[] {"abc","def","g hi","jkl"});
comboBox1.Locat ion = new System.Drawing. Point(0, 0);
comboBox1.Size = new System.Drawing. Size(300,20);
comboBox1.TextC hanged += new
System.EventHan dler(this.combo Box1_TextChange d);
Controls.Add(th is.comboBox1);
textBox1 = new System.Windows. Forms.TextBox() ;
textBox1.Locati on = new System.Drawing. Point(0, 30);
textBox1.Size = new System.Drawing. Size(300, 20);
Controls.Add(th is.textBox1);
ClientSize = new System.Drawing. Size(320, 60);
}
private void comboBox1_TextC hanged(object sender, System.EventArg s e) {
textBox1.Text=c omboBox1.Text;
}
}

Hope it helps,

Ludovic SOEUR.

Nov 17 '05 #5
There was a little mistake in your source code.
You attempt to modify Text attribute while TextChanged event is being
working...so you fire an other TextChanged event.You just have to add a var
(isTextChanging for example), set it to true at the beginning of the event
and false at the end, and to exit the function is this var is set to true.
You will be sure that the event is fired only once.

As I don't know you code, I created a new project with only one combobox.
Everything works properly. But there a bug in your source code (you use
Substring without checking if it's possible). In the example below, the
application crashes when it happens. If your application is multithreaded or
the combobox is being shown when dragging an element, no exception would be
thrown and that might explain why your Text attribute stays empty.

Have a look to the following lines that work on my computer :

using System;
using System.Drawing;
using System.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;

public class Form1 : System.Windows. Forms.Form {
private System.Windows. Forms.ComboBox cbEdit;
public Form1() {
InitializeCompo nent();
}
#region Windows Form Designer generated code
private void InitializeCompo nent() {
this.cbEdit = new System.Windows. Forms.ComboBox( );
this.SuspendLay out();
//
// cbEdit
//
this.cbEdit.Ite ms.AddRange(new object[] {"abc","def","g hi","jkl"});
this.cbEdit.Loc ation = new System.Drawing. Point(8, 16);
this.cbEdit.Nam e = "cbEdit";
this.cbEdit.Siz e = new System.Drawing. Size(256, 21);
this.cbEdit.Tab Index = 0;
this.cbEdit.Tex t = "comboBox1" ;
this.cbEdit.Tex tChanged += new
System.EventHan dler(this.cbEdi t_TextChanged);
//
// Form4
//
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.ClientSize = new System.Drawing. Size(292, 273);
this.Controls.A dd(this.cbEdit) ;
this.Name = "Form4";
this.Text = "Form4";
this.ResumeLayo ut(false);
}
#endregion
[STAThread]
static void Main() {
Application.Run (new Form1());
}
bool isTextChanging= false;
private void cbEdit_TextChan ged(object sender, System.EventArg s e) {
if (isTextChanging ) return; //No recursion
isTextChanging= true;
bool bFound = false;
string sText = cbEdit.Text;
int iPosition = cbEdit.Selectio nStart;
if(iPosition != 0) {
bFound = false;
for(int i = 0; i < cbEdit.Items.Co unt; i++) {
if(cbEdit.Items[i].ToString().Sub string(0,
iPosition).ToUp per() ==
sText.Substring (0, iPosition).ToUp per()) {
cbEdit.Text = cbEdit.Items[i].ToString();
cbEdit.Selectio nStart = iPosition;
bFound = true;
break;
}
}
if(!bFound) {
cbEdit.Text = sText.Substring (0, iPosition) +
sText.Substring (iPosition + 1);
cbEdit.Selectio nStart = iPosition - 1;
}
}
isTextChanging= false;
}
}

Hope it helps,
Ludovic SOEUR.
Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
6702
by: Niels | last post by:
Hello, I'm trying to get te index of a specific text in a ComboBox. The ComboBox is filled by searching for filenames in a directory. The text of the last item selected in the ComboBox is saved in a textfile in the OnExit Event of the Mainform. While loading the Mainform I want the text that was saved in the file to be compared with the items in the ComboBox ad get the index of the found item, so that I can set it as selected. I...
4
1755
by: David Sworder | last post by:
Hi, I have a ComboBox that contains 1,400 sorted items . Here's the list: abba apple ... cabba caccaa capple
3
4278
by: Bill C. | last post by:
Hello, I know this has been discussed a lot already because I've been searching around for information the last few weeks. I'm trying to implement a DataGridComboBoxColumn class. I've found several examples on the web. They all seem to have problems, though that I've been unable to resolve. The most promising example I have found is at:
2
2680
by: trevor.niemack | last post by:
Hello I want to have a combobox that saves all the input, so if I put the word test in the combobox and submit a query I want to have test saved so that the next time I go to that combobox I can click on the letter T and all the inputs with the letter T comes through.
7
26204
by: hung tran | last post by:
Hi, I'd like to drop down a combobox after validating the Leave() event, how can I do that ? Thanks
10
2730
by: ZaphodBBB | last post by:
HI I have a form with 2 comboBoxes. The first comboBox gives me a list of customers to select from. I would like to have the second comboBox populated with the names of Departments. I only want the departments listed that match the Customer selected in the first combobox. Unfortunatly the original database that the tables were drawn from (SQLServer) has one big Customers Table. It has a CustomerID for every Customer. There are multiple...
3
14377
by: Przemek M. Zawada | last post by:
Dear Group, I'm developing sample window form, using DataGridView control, which is filled with data through BindingSource, which is based on type of object, as follow: public sampleClass { public int itemNumber; public string itemName;
5
6791
by: =?Utf-8?B?bWljaGFlbCBzb3JlbnM=?= | last post by:
I want to create a customized ComboBox where the methods ComboBox.Items.Add and ComboBox.Items.Insert will behave thusly: -- If the item is not present, add it. -- If the item is present, set the selected index to the item (without adding a duplicate). I know the basics of inheriting from user controls, so if this was merely overriding a method of ComboBox I could do it. But how does one go about overriding a method of the Items...
5
18791
by: Kevinp | last post by:
I've spent the last four hours Google searching for a way to disable items in a Combobox. I found one example in C++ which I can't get to work and another in C# that I couldn't get to work either. Does anyone have some good code for an owner-drawn combobox?
0
9812
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9658
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10579
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10244
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7775
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5814
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4444
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4004
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3103
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.