Combo Box Simple Style

G

Guest

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.
 
K

Kevin Yu [MSFT]

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."
 
G

Guest

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
 
K

Kevin Yu [MSFT]

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."
 
G

Guest

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();
}
 
K

Kevin Yu [MSFT]

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."
 
G

Guest

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.SelectedValue 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."
 
K

Kevin Yu [MSFT]

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."
 
G

Guest

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.
 
K

Kevin Yu [MSFT]

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."
 
G

Guest

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)." ?
 
K

Kevin Yu [MSFT]

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 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."
 
G

Guest

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.ToString()))
return i;
}
}
 
K

Kevin Yu [MSFT]

Hi,

if(t.StartsWith(cboPrivilege.Items.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.ToString().StartsWith(t))
return i;
}
return -1
}

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

Guest

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.ToString());
MessageBox.Show(t);
//if(t.StartsWith(cboPrivilege.Items.ToString()))
if(cboPrivilege.Items.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.ToString()) showing "System.Data.DataRowView"?
 
K

Kevin Yu [MSFT]

Because you're using databinding on the ComboBox. In this case, you have to
use

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

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

Guest

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;
if(drv[3].ToString().StartsWith(t))
intValue = i;
}
return intValue;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top