473,394 Members | 1,769 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,394 software developers and data experts.

Combo Box Simple Style

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

Similar topics

4
by: Simon | last post by:
I would like to have div tag over combo box, not combo box over div tag. I have this problem on many pages usually when I use drop down menus. I try to solve this problem with z-index but it...
3
by: vgrssrtrs | last post by:
<html> <head> <script language="JavaScript"> <!-- /* *** Multiple dynamic combo boxes *** by Mirko Elviro, 9 Mar 2005 *** ***Please do not remove this comment
1
by: kashifsulemani | last post by:
Combo Box DropDown Style. A combo box is bound to a data table field name. When combo box has simple / drop down style then bound is successful. Other hand if Combo box has drop down list...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.