I have a Gtk TreeView displaying a list of items in an ArrayList. The TreeView is set to reorderable, so the user can drag and drop items in the list to re-order them.
My question is, how do I update the original ArrayList to match the new TreeView ordering once reordering has taken place?
I have managed to come up with a solution - the code below simply prints out all items in the list; hopefully you can see how by doing this you can find the correct index for the items and thus reorder them in the associated ArrayList.
TreeIter iter;
yourNodeView.Model.GetIterFirst (out iter);
for (int i = 0; i < yourNodeView.Model.IterNChildren(); i++)
{
string result = ((TypeOfListItem)sequencerNodeView.Model.GetValue (iter, 0)).ToString();
//Here you would add the TypeOfListItem instance to a new ArrayList, and then after the loop swap out the old ArrayList for the new one.
Console.WriteLine("print the item "+result);
yourNodeView.Model.IterNext(ref iter);
}
The TypeOfListItem could of course be any time. I have written a Class called MyListItem which has parameters such as title and description, so that I can access them with:
string result = ((MyListItem)sequencerNodeView.Model.GetValue (iter, 0)).title;
Related
As the title says, I'm trying to take an item from one list of objects add it to another list and delete the item from the original list without it being deleted from my new list.
This is what I've tried so far:
_ToUpdateQnA.Add(_qnaPair[rowIndex]);
int delIndex = Int32.Parse((e.CommandArgument).ToString());
ListViewDataItem item = (e.CommandSource) as ListViewDataItem;
TextBox deletedQtn = (TextBox)item.FindControl("QuestionTb");
string delQtn = deletedQtn.Text;
_qnaPair[rowIndex].Questions[delIndex].delta_question_description = delQtn;
_qnaPair[rowIndex].DelQuestions.Add(delQtn);
_qnaPair[rowIndex].Questions[delIndex].status = "toDelete";
_qnaPair[rowIndex].Questions.RemoveAt(delIndex);
After this code runs, _ToUpdateQnA keeps the _qnaPair object however, deletes the '_qnaPair[rowIndex].Questions[delIndex]' which has now had it's status updated to "toDelete" which I'd like to remain in the _ToUpdateQnA List.
Questions is a list of objects within a QnAPair object.
Thanks I'm quite new at this.
I have 2 observablecollection type list in which one list already contains data and in second list I am adding data from first list. But when I shuffle data in second list it also shuffles the data in first list.
First I tried to add data manually by using foreach loop
foreach(var dat in list1){
list2.add(new model(){
name=dat.name
});
}
then used this method to copy data.
list2 = new ObservableCollection<Model>(list1);
but nothing works for me
Here is my code which I am currently using
list1 and 2 are property of
ObservableCollection<Model> type
list2 = new ObservableCollection<Model>(list1);
to shuffle data in list2
var templist = list2.Select(c => c).ToList();
templist.Shuffler();
int j = 0;
for (int i = 0; i < 5; i++)
{
list2[i].name = templist[j].name;
list2[i].Id = i + 1;
j++;
}
after this code the data in list1 is also shuffled.
What I want is whenever I shuffle data it should not affect data in my main list i.e. list1
Thanks to #jdweng for letting me know about Clone.
To make it work Correctly I need to implement ICloneable interface in my model.
Here is the good example of ICloneable implementation
https://www.c-sharpcorner.com/UploadFile/dacca2/important-interface-in-net-work-with-icloneable-interface/
Then I have used
list2 = new ObservableCollection<Model>(list1.Select(c=>(Model)c.Clone()));
and it worked without any problem
I’m trying to add and remove items dynamically from the Items collection and in the end also maintain a numerical order (not alphabetically).
I know the use of:
myCheckedListBox.Sorted = true;
But it does not perform sorting after the first time, and also sorting is done in an alphabetically and non-numerical way.
List<int> lstNumbers = GetListOfNumbers();
//Remove items that need to be deleted
var itemsForDelete = myCheckedListBox.Items.OfType<int>().Where(chkList => !lstNumbers.Any(num => num == chkList)).ToList();
foreach (int item in itemsForDelete)
myCheckedListBox.Items.Remove(item);
//Add items that need to be added
foreach (int item in lstNumbers)
if (!myCheckedListBox.Items.Contains(item))
myCheckedListBox.Items.Add(item);
//Sort timebases list
myCheckedListBox.Sorted = true;
The final collection is accepted in GetListOfNumbers() but since I want to keep the checked\unchecked state of previous items, I cannot clear the items and only add the items in lstNumbers
Which way this can be done effectively?
I wish for my ListBox to update the old values with new values rather than simply adding more and more lines to the ListBox like it does at the moment. However, I'm not sure where to look to implement something that can handle this.
My current code looks like this:
private void DisplayText(string rawData)
{
textArduinoData.Text = rawData;
string[] sortedData = rawData.Split(';');
for (int i = 0; i < sortedData.Length; i++)
{
listPortData.Items.Add(sortedData[i].ToString());
}
}
Could someone please point me in the right direction to implementing this update feature? Any advice would be much appreciated.
You need to manage the process. It is easy in concept but depending on how much data is needed to be processed, it could get slow quickly. Steps
Create a specialized token class which implements to INotifyPropertyChanged.
Have an ObservableCollection hold the class items from #1. The observable collection notifies the ListBox when an item is added or removed. This will allow your code to add items one at a time. (Solves 1 problem)
To solve the next problem of data changing: Have a property named Text, on the class in #1 which will hold the data, provide a property change notification.
In the list box bind to the list of items created in step 1 and specify to bind to the Text. Use of a data template for the listbox will allow you to bind to the Text property of the list's instance.
Provide the heuristics/ smarts to read incoming data and find the associated data in the observable collection from step 2. When found change the Text property of the existing data to the new and the binding of that list item will change accordingly.
You could check if the ListBox contains the string using the IndexOf method and then update the existing string (or simply do nothing) or add a new one depending on whether you get an index other than the default value of -1 back:
private void DisplayText(string rawData)
{
textArduinoData.Text = rawData;
string[] sortedData = rawData.Split(';');
int index;
for (int i = 0; i < sortedData.Length; i++)
{
if ((index = listPortData.Items.IndexOf(sortedData[i])) == -1)
{
listPortData.Items.Add(sortedData[i]);
}
}
}
how to delete from listbox ?
deleting many indexes at the same time
Note that im using list of car types with their details
the objects in the listBox have car type cost rate
Update: If you want to remove all selected items as commented:
foreach (int i in listBox1.SelectedIndices)
listBox1.Items.RemoveAt(i);
If you want to delete all items instead, use Clear:
listBox1.Items.Clear();
if you want to remove at a specific index, use RemoveAt:
listBox1.Items.RemoveAt(0);
or in a loop:
for(int i = 0; i < listBox1.Items.Count; i++)
listBox1.Items.RemoveAt();
if you want to remove a speicific item, use Remove:
Car car = (Car) listBox1.Items[0];
listBox1.Items.Remove(car);
You have to use loop.
Something like this:
List<int> indexesToDelete = new List<int>();
// add items you want to remove to List like this:
indexesToDelete.Add(1);
indexesToDelete.Add(2);
indexesToDelete.Add(4);
// loop will execute code inside inside for all items added to list
foreach (int indexToDelete in indexesToDelete)
{
listbox1.RemoveAt(indexToDelete);
}
Edit: itemsToDelete renamed to indexesToDelete in code.