combine 2 listboxes into a new listbox c# - c#

I have 2 listboxes each displaying 2 different lists which are being populated by user input. I was wondering if I could somehow combine the data in each listbox and show it in a third listbox. This is a windowsForms application in visual Studio. I also want to make sure that it is updated properly when a new value is added into the 2 different listboxes. So far what I have done is combined the two lists that I have as so:
public List<String> listAll()
{
List<String> all = new List<string>();
all.AddRange(listFirstName());
all.AddRange(listSecondName());
return all;
}
The problem with this is first of all i dont know if this will update when a new value is added to the other two lists. and secondly now that I have this new list i still dont know how to display it in a listbox. keep in mind that i still need to have the other listboxes containing the first 2 lists displayed in the main form along with this new listbox which will contain the values for both of them.
Cheers, any help is welcome and appreciated.

You can have seperate method to populate listBox3. in that method you can clear existing items and add all the items from listBox1 and listBox3.
when you add items to listBox1 or listBox2 you can call same method after adding items.
listBox3.Items.Clear();
listBox3.Items.AddRange(listAll().ToArray());

Usually, if you want to make sure that your Listbox updates dynamically, use an ObservableCollection instead of a List.
ObservableCollection has the same format as a List:
ObservableCollection<String> all = new ObservableCollection<string>();
Make sure you add the following
using System.Collections.ObjectModel;

Related

Can't manually add item to listbox that is populated iterating through a list

In a C# WinForms project I'm querying a database to get a list of values to populate a listbox with. The query populates a List and then I iterate through that to add the list items to the listbox.
lsNewValuesList = dbGetNewValueInfo.GetNewValuesDgvData(strNewValuesQuery);
foreach (string strItem in lsNewValuesList)
{
lsBxNewValues.Items.Add(strItem);
}
After that's done, I need to manually add an item to the top of the list, which I'm doing via, lsBoxNewValues.Items.Insert(0, "DELETE");. But when I run it I get the message,
Items collection cannot be modified when the DataSource property is set.
Here is a screenshot of the error (to clarify some questions):
Looking into that I'm understanding that error arises when the listbox is populated with a datasource, but I'm just populating it from a string list. Is that, technically, a datasource then?
How do I accomplish what I'm trying to do?
[UPDATE]
Okay, I've done some fiddling around, albeit without resolving my issue, and I've tried the following:
lsNewValuesList = dbGetNewValueInfo.GetNewValuesDgvData(strNewValuesQuery);
lsNewValuesList.Insert(0, "DELETE");
lsBxNewValues.DataSource = lsNewValuesList;
//foreach (string strItem in lsNewValuesList)
//{
// lsBxNewValues.Items.Add(strItem);
//}
Instead of inserting "DELETE" in the ListBox (which is what I was originally trying and was causing the error), I inserted it at index 0 of the List, the datasource of the ListBox, and then set the ListBox's datasource. "DELETE" is showing up in the ListBox, but it's getting alphabetized along with the rest of the items. I'm not doing any sorting of the list or the ListBox - I'm using ORDER BY in the database query, however. CyberZeus suggested I use Refresh() on the ListBox, but that didn't have any effect.
Yes, a list of strings is a datasource.
You have to add the data retrieved from the database to the datasource of the ListBox. You may need to refresh the ListBox.

ComboBox binded to observable collection how to add 1 extra value

I have a combobox binded to an observable collection through
cmbBladesTab1.ItemsSource = easyRunData.olstBlades;
that works fine.
I want the combobox to be binded to all that values plus one.
E.g.
easyRunData.olstBlades; contains "PL1", "PL2", "PL3", "PL4"
while cmbBladesTab1 contains "ALL BLADES", "PL1", "PL2", "PL3", "PL4"
--ADD all work has to be done from code-behind
Thanks for your help.
You could add a property, that adds the particular item to the list.
ObservableCollection<string> myCollection;
ObservableCollection<string> MyCollectionViewProp
{
get
{
var tempCollection = new ObservableCollection<string>(myCollection);
tempCollection.Add("Extra element");
return tempCollection;
}
}
Depending on the size of the collection and the number of times it is accessed, this is probably the programmatically simplest solution. If you need to access it often, the worse this solution gets, as it creates a new collection every time.
In this case you should probably listen to the CollectionChanged event and keep a separate redundant list.
Easiest way would be to add an extra item in the observable collection with some prefixed text / key.
That way, because it's in the collection, it will be visible in the combobox and when the user selects this item you can evaluate it to see if it's the added item or not.
A good example is indeed given as an answer on this question add an item to combobox before bind data from data base

Remove or add items in DataGridComboboxColumn on row add or delete

Here's my problem: I need to make a DataGrid with dynamic comboboxes using the WPF. If the value of a combobox is already used in the previous rows, the next ones, that will be added by the user, shouldn't contain the item already used.
In this image, the ITEM A shouldn't apear on the combobox of the second line.
I don't have ideia how to accomplish this, can anyone show me a light?
OBS: The DataGrid ItemsSource is binded to an ObservableCollection, and the DataGridComboBoxColumn ItemsSource is a List.
Thanks !!!
The ItemsSource of the combo doesn't have to be bound to an ObservableCollection, but it can help depending on exactly how you solve this.
When that cell goes in to edit mode the property the ItemsSource is bound to gets hit - so you can return a new list of items each time the getter is hit. Here is a very basic example to give you an idea:
public List<string> MyItemsSource
{
get
{
var myNewList = MyMasterList.ToList(); //create a (reference) copy of the master list (the items are not copied though, they remain the same in both lists)
if (PropertyA != null)
myNewList.Remove(PropertyA);
return myNewList;
}
}
So what you are creating and returning is a filtered version of your master list of all possible items. LINQ will be of great help to you here.
Alternatively you could keep just one static copy of the master list as an ObservableCollection, and simply remove items from that static copy as they get selected (and add them back in as they get unselected). Which option you choose will depend on how many times the list can be modified due to items being selected and how complicated it is to generate the list. I've used the dynamically generated list many times in the past, it's an option that works well in most cases.

How to select multiple items in multilistpicker windows phone 8

I need to display a list of items in list picker,Im using list picker with multiple selection mode.Is there any way to check the check boxes of multiple items from c#(without user's interaction).I found many ways to find the list of selected items but couldnt find how to set multiple items as selected from code behind.Please help.I tried using
for(int i=0;i<selecteditems.count;i++)
{
listpicker.SelectedItem=listpicker.Items[i];
}
This doesnt work ..
Use the SelectedItems property for setting multiple items as selected.
This may be help you
listpicker.SelectedItems = List<your selected items>;

Unable to update listbox from database

In my application I have listBox and its items source is ObservableCollection, but when I add data, this listBox won't update. In constructor i use:
listBoxPersons.ItemsSource = UserContacts;
and user UserContacts is static ObservableCollection, which is initialised from database and when I open the programm, it shows correct data. But when I add new item into database and in same time using same code listBoxPersons.ItemsSource = UserContacts;, then listBox won't display added item. But when I logout and login again to my application, it displays my added items.
You literally said add new item into the database. You need to also add the item to the ObservableCollection.
Perhaps the problem is with the static ObservableCollection.
Try it with a non-static collection.

Categories

Resources