473,805 Members | 2,164 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Combo Box, finding the right Item

I have a Combo box, populated from a List<Objects>.

The Object has a .name, and .id property. The combo box has been
populated with these using DisplayMember and ValueMember.

When I load the screen, and am editing an item, the combo must go to
the correct 'Manufacturer' that the main screen wants to see. This is
based on a Model object I have. The model has a manufacturer.

So, I populate the combo with the possible Manufacturers, but then, I
need to default the combo to the one that the main screen needs. The
code below does that, by iterating through the items in the combo.
However, I am sure there is a better way to do it. I did it the other
day in a test app, and it worked. But for the life of me, I can't get
it to work now.

The code below DOES work, but could someone tell me the easier/correct
way? I don't want to use data binding right now. I just want to move
the index to the correct item.
private void frmAddEditModel _Load(object sender, EventArgs e)
{
List<IManufactu rermanufacturer s =
doManufacturers .GetListOfManuf acturers();
cmbManufacturer .DisplayMember = "name";
cmbManufacturer .ValueMember = "id";
foreach (IManufacturer man in manufacturers)
{
cmbManufacturer .Items.Add(man) ;
}

if (ModelID 0) // Are we editing?
{
IModel model = doModels.GetMod el( ModelID );

ebModel.Text = model.model;
ebEdition.Text = model.edition;

int cmbIndex = 0;
foreach (IManufacturer man in cmbManufacturer .Items)
{

if (man.id == model.manufactu rer)
{
break;

}
cmbIndex++;

}
cmbManufacturer .SelectedIndex = cmbIndex;
if (model.deleted == true)
{
tsStatus.Text = "Model is marked as deleted.";
}
else
{
tsStatus.Text = string.Empty;
}

}

}

Jun 2 '07 #1
4 1730
the easier way? you want the faster way?
Jun 2 '07 #2
u can use caching maybe for the faster access or you can use dataview
component.u can set the rowfilter property of the dataview
component...
Jun 2 '07 #3
On 2 Jun, 15:11, Hakan Fatih YILDIRIM <hfysilis...@gm ail.comwrote:
the easier way? you want the faster way?
I'm sure I saw a way that meant no itterating through the items.
So, yes, the faster way.

Jun 2 '07 #4
Instead of this code:

On Sat, 02 Jun 2007 06:42:35 -0700, Cralis <ad***@myschool mates.comwrote:
int cmbIndex = 0;
foreach (IManufacturer man in cmbManufacturer .Items)
{

if (man.id == model.manufactu rer)
{
break;

}
cmbIndex++;

}
cmbManufacturer .SelectedIndex = cmbIndex;
Try "cmbManufacture r.SelectedValue = model.id".

One thing to keep in mind is that the SelectedValue property doesn't work
unless you've added your data to the ComboBox by building an ArrayList and
then setting the ComboBox.DataSo urce property to that ArrayList. I don't
know why it doesn't work when you simply put the object into the Items
collection for the ComboBox, especially since the DisplayMember property
does work in that case. But it doesn't. :(

Sure seems like it should. I wonder if anyone else has a good explanation
for the discrepancy.

Anyway, that means that this code:
foreach (IManufacturer man in manufacturers)
{
cmbManufacturer .Items.Add(man) ;
}
Should read instead:

ArrayList rgman = new ArrayList();
foreach (IManufacturer man in manufacturers)
{
rgman.Add(man);
}
cmbManufacturer .DataSource = rgman;

Pete
Jun 2 '07 #5

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

Similar topics

1
3741
by: Daniel Hill | last post by:
OK, I have very, VERY basic background knowledge of VB6 and have now upgraded to VB.NET and now I'm struggling to bring up the forms I want. What I am looking to do is to have a click a command button bring up a form, and to have which form is brought up determined by which item is selected in the accompanying combo box. I've gathered that you have to declare the item as a
4
5783
by: Yuk Cheng | last post by:
<<<start index.htm>>> <html> <head> <script> function perform(action){ } </script> </head>
2
2047
by: A | last post by:
I have a combo box that is databound to an ArrayList (IList to be more exact) I have a situation where if the user cancels a save I want the combo value to go back to blank. This should be SIMPLE. I have set the SelectedIndex to -1 I have set the SelectedValue to any option under the sun I have set the every other known property to a value that would clear the currently selected option.
14
5349
by: Gary Shell | last post by:
I have five comboboxes each has a item listed populated by hand in the IDE. Each has the text property bound to a text field in the database. When I initially fill the dataset and display the page all of these combo boxes display the correct data, but all of them are highlighted as if they have focus. If I click in on and then click another control the highlight goes away. I have a tabbed interface and If I navigate to another tab and...
9
4463
by: Kay | last post by:
Hi all, Could you tell me the best way to add a blank item(as first item) in a data binded combo box? Because I think I didn't do it right and it generate an error if the second item(after the top blank item) is selected by using Combo.SelectedValue : "Specified argument was out of the range of valid values. Parameter name: '-2147483648' is not a valid value for 'index'.
1
1291
by: HDMorales | last post by:
I have a combo box on a form that displays a list of a text field with item numbers. All items start with a number and then alpha characters. When I display the list on the combo box and try to type more than one digit to do the search and shorten the finding of an item, the combo box just picks the very first digit and gets the first item that starts with that digit, ignoring the rest of the search string. If all members of the items...
2
1984
by: sara | last post by:
I can't find this in a post - I am not sure how to describe and search, so here goes: I have a combo box for the user to choose and existing vendor (there are about 1200 vendors). The user can start typing the name "David Or" and it will take him to the records that match the data he's entered. Typing "David Or" will show "David Ortiz" in the combo box, and if that is the user's vendor, he STILL has to click the arrow of the
6
5208
by: Smokey Grindle | last post by:
Say I have a combo box with the following simple object Public class MyObject public ID as integer public Name as string public overrides sub ToString() as string return name end sub end Class
0
9718
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...
1
10368
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
10107
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...
0
9186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7649
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
5544
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4327
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
3846
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.