I am building a C# windows form that displays a listbox with some items to select from to a user. That selection will determine the criteria for a query. The query will return 2 fields, an ID and a description. I want to display the description in a second listbox on the form but use the ID for subsequent processing if that description is selected in the list box.
Here's some more details about what I am trying to do:
ListBox1
Accessories
Men's
Women's
Children
When the user selects 'Accesories' a query runs and returns:
1234 Belts
2345 Scarves
4566 Handbags
ListBox2 displays the descriptions
Belts
Scarves
Handbags
But when the user double clicks on Belts I want to add 1234 to a field in class instance. What kind of list should I put the results of the query into so that I can use it to add Items to my list box and still be able to know the ID?
Thanks!
Leslie
In the .NET list boxes, the item list is not just a list of strings, but objects. The component will use the string value of the object for display, but you can reference the ID when you look at the selected value.
Define a class thusly
class Choices
{
public string Name;
public int ID;
public override string ToString()
{
return name;
}
}
Populate the list with instance of this class, and you should be good to go.
Both WinForms and WPF are quite flexible. Just add the Accessory objects to the Items property. Or set DataSource to an existing list.
The default display is through ToString() but you can set DisplayMember to the name of a specific property.
You can get at the entire object through SelectedItem. And/or set ValueMember and read SelectedValue.
Related
I have a question about a user control I am creating.
The user control is only a ComboBox (with personal settings), with a datasource from a list from a database.
This list is very simple, it’s a list of vehicule type.
Currently, my database looks like this :
Id_Vehicule_Type Type Description
1 Car Car description
2 Truck Truck description
3 SUV SUV description
I can link my data to my user control, there’s no problem with that.
What I want is an option (property) to add or remove an “All” field.
If I create a new vehicule, I would like to be able to choose between Car/Truck/SUV.
If I search for a vehicule, I would like to be able to choose between Car/Truck/SUV/All. All will search for any vehicule type.
I already add a property to my user control, but for the moment, I retrieve the list from my database in the «Set» section of my property. It check if the _IncludeAll is true, then add a new item in the list before binding it, but I don’t find this really « well-made ».
List<VehiculeType> vehiculeTypes = Database.GetAllVehiculeType();
if (this._IncludeAll)
{
vehiculeTypes.Add(new VehiculeType(-1, "(All)"));
this.cbVehiculeTypes1.SelectedValue = -1;
}
I have no problem retrieving the id / type / description from my user control. I only can’t figure out how to manage this « option ».
Thank you and have a nice day.
I have developed a small application but ran into this problem, so I'm starting with a clean slate to try and isolate it. So far, I made a very simple Windows form (VS 2017) application connecting to an SQL data table. On the form, I have a combo box for selecting a table row that is bound to the data set. I have it displaying an order number, and have its value member as an EntryID number as shown, which is also the "selected value".
I also have a simple text box on the form displaying the EntryID.
When I run the app, the combo box initially displays an ordered list of order numbers like this:
Before selecting an item, if I scroll through the list using the form's tool bar selector, the EntryID text box value corresponds to the combo box value, so Ord40 selects the last data set row (where EntryID = 1003).
When an item is selected, however, the combo box list order changes. For example, after a few selections, I get:
But if I select the last display item, "Ord 20", I still get the data row EntryID = 1003. In other words, although the bound data set does not change, the combobox scrambles the displayed item. Put yet another way, if the combobox has a set of display fields and a corresponding set of value fields, the display field text get out of sync with the underlying value.
I hope that makes sense. This is straight, out of the box, no altered code on a fresh project, and I have tried different settings on the "selected value" property.
Any help would be much appreciated.
James
You are binding SelectedValue of the ComboBox to the same data source which you use as data source of the control. Setting DataSource is just for showing items in dropdown. Setting SelecetdValue if for changing the value of bound property/column.
You never want to bind SelectedValue to the same data source which you use for DataSource.
Just as a an example, assuming you have the following tables:
Product (Id, Name, CategoryId, Price)
Category (Id, Name)
Then if you want to show a ComboBox for CategoryId in the edit form of Product, the setup of the categoryIdComboBox should be:
DataSource: categoriesDataSource
DisplayMember: Name
ValueMember: Id
SelectedValue: productsBindigSource - CategoryId
I have a drop down list (Works as needed) but I want to be able to add details to the list when I am selecting an ID it will show the customer/venue's name so you don't have to memorise every ID. I want to drop the name after its selected and keep the ID but I can't get it by itself.
Have you reviewed how to implement combo box control? I believe you can bind data source and then configure the "value" and display properties so that its selected index would reveal value (the Id in your data set) while the displayed portion would show a user friendly name of your data.
Refer to... https://learn.microsoft.com/en-us/dotnet/framework/winforms/controls/how-to-bind-a-windows-forms-combobox-or-listbox-control-to-data
I am building a simple language learning helper application in WinForms. One of its modules is a dictionary. It consists of "Sets" and words are stored in a Set. User can create a new Set of words and store some of them in it.
I'm printing all the words from selected one or several Sets in a ListView with columns. Upon checking a Set or Sets in CheckedListBox the List clears and prints words (as string variables).
Trouble comes when there are few Sets checked with their words listed, and user wants to edit one of the listed words. I cannot use indexes (such as index of List item equals to word item in a Set), as those List string items are from different Sets.
Is there any way get a source object from a ListView item? I have not added objects to the list but only some of their variables, but are they somehow connected?
Thank you for your help.
Cheers!
EDIT: Explaining why to me setting a Tag is not a solution:
All the Sets are stored in a List<DictionarySet> dictionarySets. Every Set containts a List<Word> words where words are stored.
How do I fill the ListView:
private void UpdateList()
{
wordsListView.Items.Clear();
List<Word> currentSetWordList;
foreach (DictionarySet ds in setCheckedListBox.CheckedItems) //List<DictionarySets> dictionarySets inserted, DisplayMember set to its Name property
{
currentSetWordList = ds.words;
foreach (Word w in currentSetWordList)
{
ListViewItem newItem = new ListViewItem("--"); //for now a string, later an enum
newItem.Tag = ds;
newItem.SubItems.Add(w.GermanTranslation); //string property
newItem.SubItems.Add(w.PolishTranslation); //string property
wordsListView.Items.Add(newItem);
}
}
}
In this case the program loops through each Set and its word list and prints the words. Their tag is DictionarySet everywhere.
You could use the Tag property on the ListViewItem. The Tag property:
Gets or sets an object that contains data to associate with the item.
In short, when you create each item of the list view, you could add your object as the tag. This way you'll be able to get it back when the user selects an item.
See MSDN for details.
I know how to connect to an access database and so on.
My question is, I want to select an item from a list box, and then it must search the access database for the item I selected and display all its contents in textboxes. For example:
In the listbox I have the following added:
Car 1
Car 2
Car 3
etc.
If I select Car 2, I want it to read the database and display all Car 2's properties in textboxes. So for example, once I select it, it can display Horsepower in a specific textbox, max speed in a specific text box, year model in a specific text box etc.
Could anyone help me out with this?
Assuming you have 1 textbox per property that will change whenever you select something different from your listbox here is what I would do. It sounds like you are already populating your listbox with some category from your database, possibly CarType. I would use LINQ to run a query to populate the results within the given textbox.
var query = from record in myTable.AsEnumerable()
where record.CarType == myListBox.SelectedValue
select record;
foreach (var record in query)
{
horsePower.Text = record.HorsePower;
//and so on
}
It sounds like you are setting up a custom control for this. However, I would suggest using a Data Grid to do what you are trying to do, instead of Text Boxes.
Have you tried using data binding?
listBox.DataSource = dataTableCars;
listBox.DisplayMember = "car_name";
listBox.ValueMember = "car_id";
txtBox.DataBindings.Add("Text",dataTableCars,"car_name");
txtBox.DataBindings.Add("Text",dataTableCars,"car_driver_name");
etc.