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

Combo Box Simple Style

P: n/a
This is for a Win form.

My combo box DropDownStyle is set to simple. When the user types in a 3
character string in the combo box, the closest item in the collection is
shown, right below where the user typed in the 3 character string.

When the user leaves the combo box I want it to automatically select the
item in the collection that is right below where the user typed in the 3
character string.

Nov 17 '05 #1
Share this Question
Share on Google+
16 Replies


P: n/a
Hi Cadel,

You can handle the ComboBox.LostFocus event. When the event fires, just set
the ComboBox.Text = ComboBox.SelectedItem.ToString().

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 17 '05 #2

P: n/a
I get an error message, "Object reference not set to an instance of an
object." on the code you gave me, "cboPrivilege.Text =
cboPrivilege.SelectedItem.ToString();"

Here is my code for the combo box and for the button I have after the combo
box.

private void cboPrivilege_Leave(object sender, System.EventArgs e)
{
cboPrivilege.Text = cboPrivilege.SelectedItem.ToString();
}

private void cmdAddPrivileges_Click(object sender, System.EventArgs e)
{

DataRowView drv = cboPrivilege.SelectedItem as DataRowView;

int intSalesRevKey = (int)drv[0];
string sLicenseCode = (string)drv[1];
object flGrossFee = drv[2];

DataTable dt = dgPrivileges.DataSource as DataTable;

DataRow dr = dt.NewRow();

dr[0] = intSalesRevKey;
dr[1] = sLicenseCode;
dr[2] = flGrossFee;

dt.Rows.Add(dr);

cboPrivilege.Focus();
decimal calTotal = Convert.ToDecimal(txtTotal.Text) +
Convert.ToDecimal(flGrossFee);
txtTotal.Text = calTotal.ToString();

}

I tried

"Kevin Yu [MSFT]" wrote:
Hi Cadel,

You can handle the ComboBox.LostFocus event. When the event fires, just set
the ComboBox.Text = ComboBox.SelectedItem.ToString().

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 17 '05 #3

P: n/a
Hi Cadel,

If a NullReferenceException is thrown at this point, please check if an
item in the collection has been set. If not, please try to select the
desired item in the code that try to search for the input. You can use
ComboBox.SelectedItem or use ComboBox.SelectedIndex to select the item that
matches the input strings.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 17 '05 #4

P: n/a
How do I check if an item in the collection has been sent?

I tried the following code but it didn't work.

if (cboPrivilege.SelectedValue.ToString() == "")
{
cboPrivilege.SelectedItem = cboPrivilege.Text;
}
else
{
cboPrivilege.Text = cboPrivilege.SelectedItem.ToString();
}


"Kevin Yu [MSFT]" wrote:
Hi Cadel,

If a NullReferenceException is thrown at this point, please check if an
item in the collection has been set. If not, please try to select the
desired item in the code that try to search for the input. You can use
ComboBox.SelectedItem or use ComboBox.SelectedIndex to select the item that
matches the input strings.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 17 '05 #5

P: n/a
Hi Cadel,

As I mentioned in my last post, you can set a breakpoint on line

if (cboPrivilege.SelectedValue.ToString() == "")

And put cboPrivilege.SelectedItem and cboPrivilege.SelectedValue in the
watch window to check for nulls. According to the error message, there must
be a null reference here.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 17 '05 #6

P: n/a
cboPrivilege.SelectedItem has <undefined value>
cboPrivilege.SelectedValue has <undefined value>

If I run the code again, and select from the list, then I get:
cboPrivilege.SelectedItem has {System.Data.DataRowView}
[System.Data.DataRowView]:
{System.Data.DataRowView}cboPrivilege.SelectedValu e has {2337}
[System.Int32]: {2337}
Does the user HAVE TO select from the list, so I can retrieve data from the
combo box? Which has been the question in the begin of this thread, "When
the user leaves the combo box I want it to automatically select the
item in the collection that is right below where the user typed in the 3
character string."
"Kevin Yu [MSFT]" wrote:
Hi Cadel,

As I mentioned in my last post, you can set a breakpoint on line

if (cboPrivilege.SelectedValue.ToString() == "")

And put cboPrivilege.SelectedItem and cboPrivilege.SelectedValue in the
watch window to check for nulls. According to the error message, there must
be a null reference here.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 17 '05 #7

P: n/a
Hi Cadel,

When the ComboBox loses focus, there is no item selected. We can't use
cboPrivilege.SelectedItem = cboPrivilege.Text to select the item, because
SelectedItem has to be an item in the item collection of the ComboBox. Also
the three chars you have typed doesn't exist in the collection. So, in this
case, we have to write more logics, to find out which item is the most
likely to be used, get the index of this item and use
cboPrivilege.SelectedIndex = index to get the item selected.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 17 '05 #8

P: n/a
I am typing in an Item that is in the item collection.

In my test, I type HIP, and HIP is in the item collection. HIP shows up
right under where I type HIP.

As I step through the code, on Leave of the combobox named cboPrivilege, the
value of cboPrivilege.SelectedValue = <undefined value>.

Also the DropDownStyle for the combobox is set to Simple.

"Kevin Yu [MSFT]" wrote:
Hi Cadel,

When the ComboBox loses focus, there is no item selected. We can't use
cboPrivilege.SelectedItem = cboPrivilege.Text to select the item, because
SelectedItem has to be an item in the item collection of the ComboBox. Also
the three chars you have typed doesn't exist in the collection. So, in this
case, we have to write more logics, to find out which item is the most
likely to be used, get the index of this item and use
cboPrivilege.SelectedIndex = index to get the item selected.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 17 '05 #9

P: n/a
Hi Cadel,

Because in the ComboBox, we just typed these three chars. At this time, no
item is selected in the ComboBox, so the SelectedValue property is
undefined value.

Since looking up in the collection is not the original behavior of the
ComboBox, you need to implement how to look up in the item collection with
the keyword that user has typed. When the item is found, set SelectedIndex
property to the value of item index (Do not set the SelectedValue property).

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 17 '05 #10

P: n/a
I tried the following code but received errors.

int intSelectedIndex = cboPrivilege.SelectedItem(cboPrivilege.Text);
cboPrivilege.SelectedItem = intSelectedIndex;

I also tried; cboPrivilege.SelectedText.StartsWith(cboPrivilege. Text);

None of my tries worked, What is the code to "to implement how to look up in
the item collection with the keyword that user has typed. When the item is
found, set SelectedIndex
property to the value of item index (Do not set the SelectedValue
property)." ?
"Kevin Yu [MSFT]" wrote:
Hi Cadel,

Because in the ComboBox, we just typed these three chars. At this time, no
item is selected in the ComboBox, so the SelectedValue property is
undefined value.

Since looking up in the collection is not the original behavior of the
ComboBox, you need to implement how to look up in the item collection with
the keyword that user has typed. When the item is found, set SelectedIndex
property to the value of item index (Do not set the SelectedValue property).

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 17 '05 #11

P: n/a
Hi Cadel,

There is no ready-made methods in the ComboBox class for us to find the
item starts with certain text. You have to write your own method which
accepts the user input text as argument and then use a for loop to find the
item starts with user input text. This method returns the index of the
found item and we set this index to the SelectedIndex property. Here is
some pseudocode.

private int someMethod(string t)
{
for(int i=0;i<this.comboBox1.Items.Count;i++)
{
if(this.comboBox1.Items[i] STARTS WITH t)
return i;
}
return -1
}

this.ComboBox1.SelectedIndex = someMethod(this.ComboBox1.Text);

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 17 '05 #12

P: n/a
Why would I get the error message, "not all code paths return a value"?

Here is my code.

private void cboPrivilege_Leave(object sender, System.EventArgs e)
{
cboPrivilege.SelectedIndex = GetComboIndex(cboPrivilege.Text);
}

private int GetComboIndex(string t)
{
for(int i=0;i<this.cboPrivilege.Items.Count;i++)
{
if(t.StartsWith(cboPrivilege.Items[i].ToString()))
return i;
}
}

"Kevin Yu [MSFT]" wrote:
Hi Cadel,

There is no ready-made methods in the ComboBox class for us to find the
item starts with certain text. You have to write your own method which
accepts the user input text as argument and then use a for loop to find the
item starts with user input text. This method returns the index of the
found item and we set this index to the SelectedIndex property. Here is
some pseudocode.

private int someMethod(string t)
{
for(int i=0;i<this.comboBox1.Items.Count;i++)
{
if(this.comboBox1.Items[i] STARTS WITH t)
return i;
}
return -1
}

this.ComboBox1.SelectedIndex = someMethod(this.ComboBox1.Text);

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 17 '05 #13

P: n/a
Hi,

if(t.StartsWith(cboPrivilege.Items[i].ToString())) mean the user input
string starts with the item string. This is wrong. What we are checking is
if the item begins with the input string. Also did you notice the return -1
in my code? If we cannot find the item in the collection, we should also
return a value, that is -1 in my case.

private int GetComboIndex(string t)
{
for(int i=0;i<this.cboPrivilege.Items.Count;i++)
{
if(cboPrivilege.Items[i].ToString().StartsWith(t))
return i;
}
return -1
}

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 17 '05 #14

P: n/a
There is never a match, even when I put in a value(item) that is in the
collection.

For testing I added MessageBoxes, here is my code.

private int GetComboIndex(string t)
{
int intValue = -1;
for(int i=0;i<this.cboPrivilege.Items.Count;i++)
{
MessageBox.Show(cboPrivilege.Items[i].ToString());
MessageBox.Show(t);
//if(t.StartsWith(cboPrivilege.Items[i].ToString()))
if(cboPrivilege.Items[i].ToString().StartsWith(t))
intValue = i;
}
return intValue;
}
The first message box, pops up "System.Data.DataRowView".
The second message box, pops up "FHL".

The second message box is right, I typed in FHL. FHL is in the Item
Collection.
The first message box, I think is wrong, it should pop up "C01", because
it's the first item in the collection. As the code, loops through the FOR
loop, the next Item the first message box should be "C02".

Why is cboPrivilege.Items[i].ToString()) showing "System.Data.DataRowView"?

"Kevin Yu [MSFT]" wrote:
Hi,

if(t.StartsWith(cboPrivilege.Items[i].ToString())) mean the user input
string starts with the item string. This is wrong. What we are checking is
if the item begins with the input string. Also did you notice the return -1
in my code? If we cannot find the item in the collection, we should also
return a value, that is -1 in my case.

private int GetComboIndex(string t)
{
for(int i=0;i<this.cboPrivilege.Items.Count;i++)
{
if(cboPrivilege.Items[i].ToString().StartsWith(t))
return i;
}
return -1
}

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 17 '05 #15

P: n/a
Because you're using databinding on the ComboBox. In this case, you have to
use

DataRowView drv = (DataRowView)cboPrivilege.Items[i];
if(drv[cboPrivilege.DisplayMember].TosString().StartsWith(t))

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 17 '05 #16

P: n/a
It works, finally works!!!! Thank you.

Here is the code, if anyone out there wants a simple combo box to select the
value from the collection automatically. Modify drv[3] to the column number
of the items displayed.

private void cboPrivilege_Leave(object sender, System.EventArgs e)
{
cboPrivilege.SelectedIndex = GetComboIndex(cboPrivilege.Text);
}

private int GetComboIndex(string t)
{
int intValue = -1;
for(int i=0;i<this.cboPrivilege.Items.Count;i++)
{
DataRowView drv = (DataRowView)cboPrivilege.Items[i];
if(drv[3].ToString().StartsWith(t))
intValue = i;
}
return intValue;
}
"Kevin Yu [MSFT]" wrote:
Because you're using databinding on the ComboBox. In this case, you have to
use

DataRowView drv = (DataRowView)cboPrivilege.Items[i];
if(drv[cboPrivilege.DisplayMember].TosString().StartsWith(t))

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 17 '05 #17

This discussion thread is closed

Replies have been disabled for this discussion.