determine listbox items origin - c#

I'm working on an application of which requires me to have one list-box to hold data.
I'm having a little issue when deleting an object from a list-box. The issue comes into play when I fill the list-box with items from TWO separate lists.
Normally to delete the object I would get it's index and then remove it from the list in a separate class then reload the list-box to reflect the changes but in certain instances I need to fill the list-box with objects from two different lists and determining the origin of the object to delete from one of the two lists, well I'm not entirely sure how to do this.
this code is populating the list-box control.
//clear all items in the listbox
ViewListBox.Items.Clear();
//create the lists
List listOfPickups = visits.listPickups();
List listOfdeliveries = visits.listDeliveries();
//populate
ViewListBox.Items.AddRange(listOfPickups.ToArray());
ViewListBox.Items.AddRange(listOfdeliveries.ToArray());
this is how i delete when i am only loading the listbox from one list.
if (ViewListBox.SelectedIndex < 0)
{
EditSelectBtn.Enabled = false;
DeleteSelectBtn.Enabled = false;
}
else
{
if (MessageBox.Show("are you sure you want to delete the selected item?", "Are You Sure?", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
visits.removePickup(this.ViewListBox.SelectedIndex);
//refresh listbox.
updateList("pickups");
}
else
{
//clicked no so do nothing!
ViewListBox.ClearSelected();
}
}
Any help would be greatly appretiated.

You can define type of selected item, and remove it from list with simple condition. Also removing by index will not help you here. Pass whole object instead
object item = ViewListBox.SelectedItem;
if (item is Pickup)
visits.removePickup(item);
else
visits.removeDelivery(item);
If items have same type, then use some other way to get item type (e.g. value of some property).
UPDATE One catch - you can determine item origin by comparing SelectedIndex with listOfPickups length, because you are adding pickup items first. If index is greater, than pickups count, then you are removing delivery. Subtract pickups count from selected index to get index of delivery item you need to remove.
List<Pickup> listOfPickups = visits.listPickups();
List<Delivery> listOfdeliveries = visits.listDeliveries();
ViewListBox.Items.AddRange(listOfPickups.ToArray());
ViewListBox.Items.AddRange(listOfdeliveries.ToArray());
//...
if (ViewListBox.SelectedIndex < listOfPickups.Count)
{
// this is a Pickup
visits.removePickup(ViewListBox.SelectedIndex);
}
else
{
// this is a delivery
int deliveryIndex = ViewListBox.SelectedIndex - listOfPickups.Count;
visits.removeDelivery(deliveryIndex);
}

Related

Move to Next Row in a SharePoint Document View List

May I know how can I identify the item in a SharePoint list view via an ID?
Due to some changes, I am unable to use foreach as in the situation where a row remains in the list, it resulted in repetitive processing of the same item.
Below is my code snippet and sample scenario.
foreach (SPListItem item in unprocessedView)
//list only contains items with "Processed" = false
{
//if "Type" = "report",
//set "Processed" = true.
//item does not appear in list anymore.
//if "Type" = "result"
//do nothing.
//item remains in list.
}
Let's say there are 2 items in unprocessedView.
1. result type.
2. report type.
With the conditions above, item 1. will not be processed, and remains in the list.
On the loop, 1. is processed again instead of moving on to item 2.
May I know how can I workaround this?
Thank you.
Modify the code snippet as below to make it works.
foreach (SPListItem item in unprocessedView)
{
if (item["Type"]!=null&&item["Type"].ToString() == "report")
{
item["Processed"] = true;
item.Update();
}
}

How to take an item out of a list box and another item in a different list box in the same position

I am trying yo create a method that will take a value of one list box and will also be taken out of another list box at the same index. I am just a beginner to C# which is why I am having this problem. Thanks in advance for any help
if (lstCheckoutProduct.)
{
lstCheckoutProduct.Items.Remove(lstCheckoutProduct.SelectedItem);
int productIndex = lstCheckoutProduct.Items.IndexOf(lstCheckoutProduct.SelectedIndex);
lstCheckoutPrice.Items.Remove(productIndex);
}
else
{
lstCheckoutPrice.Items.Remove(lstCheckoutPrice.SelectedItem);
int priceIndex = lstCheckoutPrice.Items.IndexOf(lstCheckoutPrice.SelectedIndex);
lstCheckoutPrice.Items.Remove(priceIndex);
}
You need to get the SelectedIndex before removing the items. Also I assume your first line should check if the listbox is focused
And if you want to remove an item at a specific index you need to use RemoveAt instead of Remove.
if (lstCheckoutProduct.IsFocused)
{
int productIndex = lstCheckoutProduct.SelectedIndex;
lstCheckoutProduct.Items.Remove(lstCheckoutProduct.SelectedItem);
lstCheckoutPrice.Items.RemoveAt(productIndex);
}
else
{
int priceIndex = lstCheckoutPrice.SelectedIndex;
lstCheckoutPrice.Items.Remove(lstCheckoutPrice.SelectedItem);
lstCheckoutProduct.Items.RemoveAt(priceIndex);
}
EDIT: The first line is just a guess as you left it out in your question. Note that IsFocused will be false if the user has clicked a "Remove"-button (and thereby focussed the button instead of the listbox) to call this method.
EDIT: and you can reduce the code to this:
int index = lstCheckoutProduct.IsFocused ? lstCheckoutProduct.SelectedIndex : lstCheckoutPrice.SelectedIndex;
lstCheckoutProduct.Items.RemoveAt(index);
lstCheckoutPrice.Items.RemoveAt(index);

Combo Box Size Issue After All Items Are Removed

My application contains a ComboBox that the user can delete items from. When the program starts up it populates the ComboBox from a list of strings read in from a configuration file.
Here is the code to add items:
// version list is an array of strings
foreach (string version in versionList)
{
versionComboBox.Items.Add(version);
}
if (versionComboBox.Items.Count > 0)
{
versionComboBox.SelectedIndex = 0;
}
Here is a screenshot of the combo box after it's been populated:
If the user clicks the Delete button the program removes the selected item from the ComboBox using the following code:
if (versionComboBox.SelectedIndex >= 0)
{
versionComboBox.Items.Remove(versionComboBox.SelectedItem);
}
if (versionComboBox.Items.Count > 0)
{
versionComboBox.SelectedIndex = 0;
}
Here is a screenshot of the combo box after a few items have been removed:
The problem I am having is when the last item is removed the ComboBox resizes itself to the size it was when it was initially populated. There aren't any items in the ComboBox but it sizes itself as if there were.
Here is a screenshot after all the items have been removed:
As you can see the size is too big. I would think that after all the items were cleared it would look like the following:
Any ideas as to why this is happening?
Try to use this at the end of your code when you are filling the combobox items:
comboBoxNumTreno.IntegralHeight = true; // auto fit dropdown list
Then to clear it up:
comboBoxNumTreno.ResetText();
comboBoxNumTreno.Items.Clear();
comboBoxNumTreno.SelectedIndex = -1;
comboBoxNumTreno.DropDownHeight = 106; // default value
comboBoxNumTreno.IntegralHeight = false;
I know this is an old post, but it took me a long time to figure this out and I wanted to let anyone in the future know. After you clear your combo box just do a blank add items and it resets the height.
comboBox1.Items.Clear();
comboBox1.Items.Add("");
To clear your combo box you can add this:
if (versionComboBox.Items.Count == 0)
{
versionComboBox.Text = string.Empty;
versionComboBox.Items.Clear();
versionComboBox.SelectedIndex = -1;
}
Another approach is to manipulate the items in the data source and rebind the control each time (a lot less for you to worry about).
set DropDownHeight property to fix size
versionComboBox.DropDownHeight = 106; // default value

C# Selecting first row in CategorizedAlphabetical sorted ProperyGrid

I have ProperyGrid loaded with categorised PropertySpec and set to CategorizedAlphabetical sort. When form runs categories then items within categories are sorted. An annoying artefact is that PropertyGrid by default selects the first item after list was sorted and sometimes it scrolls view to selection. If item list is long you end up seeing list scrolled to somewhere in the middle.
Since PropertySpec can be created at runtime I want to always show the top of list on form load. PropertyGrid does not 'easily' expose collections and certainly not in ordered sequence. After googling around I am lead to believe this is not possible?
I came up with below code which proves otherwise.
Snippet will select fist category of sorted list. One could also select first item in that category expanding on the method but for my needs that was unnecessary.
// bind the PropertyTable to PropertyGrid
this.pg_Prefs.SelectedObject = proptable;
// get selected item
GridItem gi = this.pg_Prefs.SelectedGridItem;
// get category for selected item
GridItem pgi = gi.Parent.Parent;
//sort categories
List<GridItem> sortedCats = new List<GridItem>(pgi.GridItems.Cast<GridItem>());
sortedCats.Sort(delegate(GridItem gi1, GridItem gi2) { return gi1.Label.CompareTo(gi2.Label); });
// loop to first category
for (int i = 0; i < pgi.GridItems.Count; i++)
{
if (pgi.GridItems[i] == gi) break; // in case full circle done
// select if first category
if (pgi.GridItems[i].Label == sortedCats[0].Label)
{
pgi.GridItems[i].Select();
break;
}
}
Hope this will help others as well.
The simplified method of actually selecting category once you have sorted list would be to sortedCats[0].Select(); instead of looping through and checking each item. You would have to assert the list is not empty if you wanted to use that shortcut but that would gives some performance improvement...

How do I obtain selected rows in a DataGridView from different pages

I have a windows forms DataGridView, where I have data and a checkbox for each row.
I will select check box for a particular row and all the selected rows will be populated in another page.
if (grdEmp.Rows.Count > 0)
{
var selectedEmpIDs= from DataGridViewRow coll in grdEmp.Rows
where Convert.ToBoolean(coll.Cells["Select"].Value) == true
select coll;
if (selectedEmpIDs.Count() > 0)
{
foreach (DataGridViewRow row in selectedEmpIDs)
{
selectedEmp+= row.Cells["EmpId"].Value + ",";
}
}
}
This works good only for one page.
When I navigate to another page, and click the selected rows, the previous one goes off.
How do I resolve it.
Thanks
cmrhema
Note :Sorry for the confusion, When I meant it works good for a page, I meant paging.
I think I need to add more inputs,
There are 10 pages in the gridview.
I select the first record from each page of the gridview, one after another by clicking next page( Page next button).
But only the record that was selected the last is getting displayed and others and ignored off.
What could be the prblm
You can use a List or Dictionary or any other collection type globally, using Program.cs or using a static class. And store the selected rows into the list before you leave the page.
Rather than using a comma delimited string string for your list of ids you can instead use a List.
Your code will then become something like this:
if (grdEmp.Rows.Count > 0)
{
var selectedEmpIDs= from DataGridViewRow coll in grdEmp.Rows
where Convert.ToBoolean(coll.Cells["Select"].Value) == true s
select coll;
if (selectedEmpIDs.Count() > 0)
{
foreach (DataGridViewRow row in selectedEmpIDs)
{
if (!listOfIds.Contains((int)row.Cells["EmpId"].Value))
{
listOfIds.Add(((int)row.Cells["EmpId"].Value));
}
}
}
}
You will need methods to remove items from this list so adding event handlers for the checkbox selected event will probably work better.
The List object itself can simple live as a class level object of the form that containst your DataGridView.
This gets a little bit more complicated if you are managing your paging across forms, but the same principles of maintaining a list of selected ids applies.

Categories