PC Review


Reply
Thread Tools Rate Thread

Combo Box Simple Style

 
 
=?Utf-8?B?TWlrZSBM?=
Guest
Posts: n/a
 
      28th Sep 2005
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.

 
Reply With Quote
 
 
 
 
Kevin Yu [MSFT]
Guest
Posts: n/a
 
      29th Sep 2005
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."

 
Reply With Quote
 
=?Utf-8?B?TWlrZSBM?=
Guest
Posts: n/a
 
      29th Sep 2005
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."
>
>

 
Reply With Quote
 
Kevin Yu [MSFT]
Guest
Posts: n/a
 
      30th Sep 2005
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."

 
Reply With Quote
 
=?Utf-8?B?TWlrZSBM?=
Guest
Posts: n/a
 
      3rd Oct 2005
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."
>
>

 
Reply With Quote
 
Kevin Yu [MSFT]
Guest
Posts: n/a
 
      4th Oct 2005
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."

 
Reply With Quote
 
=?Utf-8?B?TWlrZSBM?=
Guest
Posts: n/a
 
      4th Oct 2005
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."


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

 
Reply With Quote
 
Kevin Yu [MSFT]
Guest
Posts: n/a
 
      6th Oct 2005
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."

 
Reply With Quote
 
=?Utf-8?B?TWlrZSBM?=
Guest
Posts: n/a
 
      6th Oct 2005
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."
>
>

 
Reply With Quote
 
Kevin Yu [MSFT]
Guest
Posts: n/a
 
      7th Oct 2005
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."

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple scatter style chart help =?Utf-8?B?QW5keSB0aGUgWWV0aQ==?= Microsoft Excel Charting 1 10th Aug 2006 08:47 AM
Simple style sheet / ASP question Roy Microsoft ASP .NET 1 2nd Aug 2005 04:00 PM
Simple style sheet / ASP question Roy Microsoft ASP .NET 0 2nd Aug 2005 02:30 PM
Simple query from simple combo box. =?Utf-8?B?R1NAVU0=?= Microsoft Access Queries 2 5th Jul 2005 08:29 PM
Simple details View style with 1.1 =?Utf-8?B?WWlheg==?= Microsoft ASP .NET 0 30th Mar 2004 09:01 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:50 PM.