Get last item added in a BindlingList - c#

I have a BindingList variable and I need to get the last item that was added to it. The following code works, but I'd like to know if there's a better way or something that I'm missing:
WorkoutScheduleList[WorkoutScheduleList.IndexOf(WorkoutScheduleList[(WorkoutScheduleList.Count - 1)])].WorkoutScheduleID);
It's not the easiest to read, but it basically takes the count of the list and subtracts 1 from it, and uses that for the IndexOf method to get the last item that was added to the list.

You can handle the ListChanged event of the binding list, and keep a reference of the item that was added.
Example:
class Program
{
static object last_item;
static void Main(string[] args)
{
BindingList<object> WorkoutScheduleList = new BindingList<object>();
WorkoutScheduleList.ListChanged += (s, e) => {
if (e.ListChangedType == ListChangedType.ItemAdded)
last_item = WorkoutScheduleList[e.NewIndex];
};
WorkoutScheduleList.Add("Foo");
WorkoutScheduleList.Add("Bar");
WorkoutScheduleList.Insert(1, "FooBar");
//prints FooBar
Console.WriteLine(String.Format("last item added: {0}", last_item));
}
}

var lastItem = WorkoutScheduleList[WorkoutScheduleList.Count - 1].WorkoutScheduleID;
or using LinQ
var lastLinqItem = WorkoutScheduleList.Last().WorkoutScheduleID;

Related

Is it possible to use substring with OfType<string>?

I'm in a school project and I was wondering how show in a multiline textBox - not a listBox, that was part of the question - how to show the first five letters of checked items of a checkedListBox. We can't show them directly, we have to create an array that will be saved in a User class by using the constructor. My first thought was to use selectedIndexChanged event of the checkListBox and use an incremented, .Items and .substring to put the substring selected into an array. Here is my first code:
private void checkedListBox_programmes_SelectedIndexChanged(object sender, EventArgs e)
{
int selection = checkedListBox_programmes.SelectedIndex;
if (selection != -1)
{
if (indice < 5)
{
tabProgrammes[indice] = checkedListBox_programmes.Items[selection].ToString().Substring(0, 6);
indice++;
}
else
{
MessageBox.Show("Tableau de données est rempli");
}
}
}
}
*tabProgrammes and indice are initialise in my partial class
That didn't worked because unchecking index would add a string in my array.
So, second code (I use a buton_click event):
// remplissage du tableau avec les éléments sélectionnées
tabProgrammes = checkedListBox_programmes.CheckedItems.OfType<string>().ToArray();
// création du profile
Profil unProfil = new Profil(textBox_Prenom.Text, textBox_Nom.Text, textBox_Utilisateur.Text, dateTimePicker_Naissance.Value, tabProgrammes);
// affichage des donnees
textBox_Affichaqe.Text = unProfil.ToString();
That work super well because I can check and uncheck without problems. SO, my problem is that I need to put in my array only the substring(0,6) of my checked items.
An idea?
Yes, you can use Substring() on the result of OfType<string>().
To do so, you need to use the Linq Select() operator in your specific case.
Here is an example:
static void Main(string[] args)
{
var dataSource = new object[]
{
42,
"bonjour",
"allo",
1000,
"salut"
};
var myStrings = dataSource
.OfType<string>()
// In this case, we use substring only if the length of the string is above 6.
.Select(s => s.Length > 6 ? s.Substring(0, 6) : s)
.ToArray();
foreach (var item in myStrings)
{
Debug.WriteLine(item);
}
}
It outputs the following:
bonjou
allo
salut

Refer List entrys to a ComboBoxItem

Situation: I have 3 TextBoxes, a button and a ComboBox. When I enter something in every TextBox and trigger the button, I want that the strings I've written in the TextBoxes can be choosen in the ComboBox as a ComboBoxItem. I've got the idea of putting the strings from the TextBoxes into a list and refer the ComboBoxItem to the correct list-entrys. Or is there a more efficient way to set and get these strings?
Would be nice if some could help me writing the code for it.
private void bAdd_Click(object sender, RoutedEventArgs e)
{
Random random = new Random();
int randomNumber = random.Next(0, 100);
int txBetrag;
txBetrag = int.Parse(betrag1.Text);
int txMonate;
txMonate = int.Parse(monate1.Text);
int txZins;
txZins = int.Parse(zins1.Text);
List<int> abList = new List<int>();
comboBox.Items.Add("1");
}
If you simply want to add txBetrag, txMonate and txZins to your combobox then you don't need a list. At the moment you're creating a list, adding nothing to it and then simply adding 1 entry to your comboBox (not even from your list).
To add your items just do:
comboBox.Items.Add(int.Parse(betrag1.Text));
comboBox.Items.Add(int.Parse(monate1.Text));
comboBox.Items.Add(int.Parse(zins1.Text));
If you really need the list as well (perhaps because it is used elsewhere as well) then you can do the following:
abList.Add(int.Parse(betrag1.Text));
abList.Add(int.Parse(monate1.Text));
abList.Add(int.Parse(zins1.Text));
Then use the list to populate the comboBox:
foreach(var item in abList)
{
comboBox.Items.Add(item);
}
UPDATE
Based off your comment it seems like you want ONE entry in the comboBox that is effectively the 3 textbox values concatenated together. So you could do something like this:
comboBox.Items.Add(betrag1.Text + monate1.Text + zins1.Text);
UPDATE 2
Following you're last comment regarding the fact that you want 1 comboBox item that refers to the 3 values, but doesn't display them, yes you can do this.
Assuming you won't have duplicate entries in the comboBox you could use a Dictionary to map your values to an entry rather than use a list. I'm assuming here that you will have more than 1 entry in your comboBox eventually, otherwise it's a bit pointless having a combobox.
var valueComboMapping = new Dictionary<string, int[]>();
valueComboMapping.Add("Entry 1", new int[] {int.Parse(betrag1.Text), int.Parse(monate1.Text), int.Parse(zins1.Text)};
This will enable you to add mappings at a later date. You can then use the Dictionary to create the listings in the comboBox like this:
foreach(var entry in valueComboMapping.Keys)
{
comboBox.Items.Add(entry);
}
To trigger an event off selecting an item in the comboBox using the SelectedIndexChanged event.
Retrieving the Values
To retrieve your mapped values in the SelectedIndexChanged event you can do something like:
private void comboBox_SelectedIndexChanged(object sender, System.EventArgs e)
{
ComboBox comboBox = (ComboBox) sender;
string entryName = (string) comboBox.SelectedItem;
//retrieve the values from the dictionary using the value of 'entryName'.
List values = new List<int>();
if (valueComboMapping.TryGetValue(entryName, out values)
{
//do something if the key is found.
}
else
{
//do something else if the key isn't found in the dictionary.
}
}
To get this to work you would need to create the dictionary as follows:
var valueComboMapping = Dictionary<string, List<int>>();
Instead of:
var valueComboMapping = Dictionary<string, int[]>();
You can add directly to combobox,no need to add to list.You are using int.Parse, it will generate Format exception.
private void button1_Click(object sender, EventArgs e)
{
comboBox1.Items.Add(betrag1.Text);
comboBox1.Items.Add(monate1.Text);
comboBox1.Items.Add(zins1.Text);
}

Get single listView SelectedItem

I have the MultiSelect property of the listView set to false and I'm trying to get a single listViewItem. But the available property is SelectedItems. I've been using the following code...
foreach (ListViewItem item in listView1.SelectedItems)
{
//do something with item.text or whatever
}
Because I know there will only be one item selected. What is the correct way of doing this?
Usually SelectedItems returns either a collection, an array or an IQueryable.
Either way you can access items via the index as with an array:
String text = listView1.SelectedItems[0].Text;
By the way, you can save an item you want to look at into a variable, and check its structure in the locals after setting a breakpoint.
I do this like that:
if (listView1.SelectedItems.Count > 0)
{
var item = listView1.SelectedItems[0];
//rest of your logic
}
Sometimes using only the line below throws me an Exception,
String text = listView1.SelectedItems[0].Text;
so I use this code below:
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listView1.SelectedIndices.Count <= 0)
{
return;
}
int intselectedindex = listView1.SelectedIndices[0];
if (intselectedindex >= 0)
{
String text = listView1.Items[intselectedindex].Text;
//do something
//MessageBox.Show(listView1.Items[intselectedindex].Text);
}
}
If its just a natty little app with one or two ListViews I normally just create a little helper property:
private ListViewItem SelectedItem { get { return (listView1.SelectedItems.Count > 0 ? listView1.SelectedItems[0] : null); } }
If I have loads, then move it out to a helper class:
internal static class ListViewEx
{
internal static ListViewItem GetSelectedItem(this ListView listView1)
{
return (listView1.SelectedItems.Count > 0 ? listView1.SelectedItems[0] : null);
}
}
so:
ListViewItem item = lstFixtures.GetSelectedItem();
The ListView interface is a bit rubbish so I normally find the helper class grows quite quickly.
For a shopping cart situation here's what I recommend. I'm gonna break it down into it's simplest form.
Assuming we start with this(a list view with 2 colums, 2 buttons, and a label):
First things first, removing the items, to do that we'll enter our remove button:
private void button2_Click(object sender, EventArgs e)
{
listView1.Items.Remove(listView1.SelectedItems[0]);
label1.Text = updateCartTotal().ToString();
}
Now the second line is updating our labels total using the next function i'll post to addup all the total of column 2 in the listview:
private decimal updateCartTotal()
{
decimal runningTotal = 0;
foreach(ListViewItem l in listView1.Items)
{
runningTotal += Convert.ToDecimal(l.SubItems[1].Text);
}
return runningTotal;
}
You don't have to use decimal like I did, you can use float or int if you don't have decimals. So let's break it down. We use a for loop to total all the items in the column 2(SubItems[1].Text). Add that to a decimal we declared prior to the foreach loop to keep a total. If you want to do tax you can do something like:
return runningTotal * 1.15;
or whatever your tax rate is.
Long and short of it, using this function you can retotal your listview by just calling the function. You can change the labels text like I demo'd prior if that's what you're after.
None of the answers above, at least to me, show how to actually handle determining whether you have 1 item or multiple, and how to actually get the values out of your items in a generic way that doesn't depend on there actually only being one item, or multiple, so I'm throwing my hat in the ring.
This is quite easily and generically done by checking your count to see that you have at least one item, then doing a foreach loop on the .SelectedItems, casting each item as a DataRowView:
if (listView1.SelectedItems.Count > 0)
{
foreach (DataRowView drv in listView1.SelectedItems)
{
string firstColumn = drv.Row[0] != null ? drv.Row[0].ToString() : String.Empty;
string secondColumn = drv.Row[1] != null ? drv.Row[1].ToString() : String.Empty;
// ... do something with these values before they are replaced
// by the next run of the loop that will get the next row
}
}
This will work, whether you have 1 item or many. It's funny that MSDN says to use ListView.SelectedListViewItemCollection to capture listView1.SelectedItems and iterate through that, but I found that this gave an error in my WPF app: The type name 'SelectedListViewItemCollection' does not exist in type 'ListView'.
foreach (ListViewItem itemRow in taskShowListView.Items)
{
if (itemRow.Items[0].Checked == true)
{
int taskId = Convert.ToInt32(itemRow.SubItems[0].Text);
string taskDate = itemRow.SubItems[1].ToString();
string taskDescription = itemRow.SubItems[2].ToString();
}
}
If you want to select single listview item no mouse click over it try this.
private void timeTable_listView_MouseUp(object sender, MouseEventArgs e)
{
Point mousePos = timeTable_listView.PointToClient(Control.MousePosition);
ListViewHitTestInfo hitTest = timeTable_listView.HitTest(mousePos);
try
{
int columnIndex = hitTest.Item.SubItems.IndexOf(hitTest.SubItem);
edit_textBox.Text = timeTable_listView.SelectedItems[0].SubItems[columnIndex].Text;
}
catch(Exception)
{
}
}
This works for single as well as multi selection list:
foreach (ListViewItem item in listView1.SelectedItems)
{
int index = item.Index;
//index is now zero based index of selected item
}
On mouse click, I would do it like this:
public static string GetSelectedItem(ListView list)
{
foreach (ListViewItem item in list.Items)
{
if (item.Selected)
return item.Text;
}
return null;
}

Item of one combobox should not come into the other

Im using c# .net windows form application. I have a database with some tables.I have two comboboxes (A & B). I have populated a combo box A with column names of a table using sys.columns. Now when i select an item in combo box A ,combo box B should be populated with the same items except the selected item which was selected in combobox A .
You should delete either this question or this one which are about identical things. anyway, here is my identical answer:
in the selected item changed event of A, add code which clears B, then loops round each item in A's Item collection and adds it to B as long as the index of the current item is different from the index of the SelectedItem in A.
Something like (pseudo code, not tested)
b.Items.Clear;
for(int i=0; i<A.Items.Count; i++)
{
if (i!=A.SelectedItemIndex)
{
b.Items.Add(A.Items[i]);
}
}
or
B.Items.Clear;
foreach(object o in A.Items)
{
b.Items.Add(o);
}
b.Items.Remove(A.SelectedItem);
should do it as well.
I'd use a combination of static extension methods and LINQ.
The static extension part would look like this:
// static class...
public static class ComboBoxHelper
{
public static string GetSelectedIndexText(this ComboBox target)
{
return target.Items[target.SelectedIndex].ToString();
}
public static object[] GetNonSelectedItems(this ComboBox target)
{
string selected = GetSelectedIndexText(target);
try
{
object[] result =
target.Items.Cast<object>().Where(c => c.ToString()
!= selected).ToArray();
return result;
}
catch
{
return new object[] { };
}
}
public static void ReplaceItems(this ComboBox target, object[] newRange)
{
target.Items.Clear();
target.Items.AddRange(newRange);
}
}
And the LINQ:
// LINQ:
private void ComboBoxA_SelectedIndexChanged(object sender, EventArgs e)
{
comboBoxB.ReplaceItems(comboBoxA.GetNonSelectedItems());
}
HTH!
Note: there's probably more efficient way than returning an array of list items, but I haven't found this to be a big issue in terms of the big picture (e.g. overall performance, etc).....
I think you will have to code the filling/removal of B in the Change Event of A
m_comboB.Items.AddRange((from item in m_comboA.Items.Cast<object>()
where item != m_comboA.SelectedItem
select item).ToArray());
Or you can use this way which don't remove duplicate items (Sam pointed this out in his comment):
m_comboB.Items.AddRange(Enumerable.Range(0, m_comboA.Items.Count)
.Where(index => index != m_comboA.SelectedIndex)
.Select(index => m_comboA.Items[index]).ToArray());

Update List element at specified list item position

I am trying to do this:
foreach (Settings sets in MySets)
{
if (sets.pName == item.SubItems[2].Text)
{
var ss = new SettingsForm(sets);
if (ss.ShowDialog() == DialogResult.OK)
{
if (ss.ResultSave)
{
sets = ss.getSettings();
}
}
return;
}
}
But since the sets spawned variable is readonly, I cant override it.
I would also like to do something like this
foreach (Settings sets in MySets)
{
if(sets.pName == someName)
sets.RemoveFromList();
}
How can I accomplish this? Lists have a very nice Add() method, but they forgot the rest :(
You can use:
MySets.RemoveAll(sets => sets.pName == someName);
to remove all the items that satisfy a specific condition.
If you want to grab all the items satisfying a condition without touching the original list, you can try:
List<Settings> selectedItems = MySets.FindAll(sets => sets.pName == someName);
foreach loops don't work here as trying to change the underlying list will cause an exception in the next iteration of the loop. Of course, you can use a for loop and manually index the list. However, you should be very careful not to miss any items in the process of removing an item from the list (since the index of all the following items will get decremented if an element is removed):
for (int i = 0; i < MySets.Count; ++i) {
var sets = MySets[i]; // simulate `foreach` current variable
// The rest of the code will be pretty much unchanged.
// Now, you can set `MySets[i]` to a new object if you wish so:
// MySets[i] = new Settings();
//
// If you need to remove the item from a list and need to continue processing
// the next item: (decrementing the index var is important here)
// MySets.RemoveAt(i--);
// continue;
if (sets.pName == item.SubItems[2].Text)
{
var ss = new SettingsForm(sets);
if (ss.ShowDialog() == DialogResult.OK)
{
if (ss.ResultSave)
{
// Assigning to `sets` is not useful. Directly modify the list:
MySets[i] = ss.getSettings();
}
}
return;
}
}
You can't do it in a 'regular' for loop?

Categories