how i can select five items in the single click on the list box??
if i click any item, i just want to +2 and -2 from the selected index. so my single click need to select five items in the listview.
Am using C#(WPF).
I am not sure what you want to do exactly, but trying. =)
Have a look at the Click event of the ListBox. You can do anything in there, including selecting five items of your choice. You can do it like this (untested, but gives you an idea):
int sindex = listBox1.SelectedIndex;
listBox1.SelectedItems.Clear();
for(int i = Math.Max(sindex - 2, 0); i < Math.Min(sindex + 2, listBox1.Items.Count()), i++)
{
listBox1.SelectedItems.Add(listBox1.Items[i]);
}
Another thing would be setting the SelectionMode to Multiple or Extended. Does this result in the behaviour you are looking for?
have a look at selectionchanged event, and get the index of the selected item and make it +2 and -2
i tried it like this and it works:
void list_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int idx = list.SelectedIndex;
int startIdx = idx - 2;
int endIdx = idx + 2;
if (startIdx < 0)
{
startIdx = 0;
}
if (endIdx >= list.Items.Count)
{
endIdx = list.Items.Count-1;
}
for (int i = startIdx; i <= endIdx; i++)
{
if (i != idx)
{
list.SelectedItems.Add(list.Items[i]);
}
}
}
one problem with this code is you can still use ctrl to select another item so it makes the selecteditems count increased
Related
I'm using C# Adapter method, I add elements to a list and after that I call that list to fill a listbox, here is my button for do that:
private void button1_Click(object sender, EventArgs e) {
listCustomers.Items.Clear();
List < Customer > customers = CustomerList.GetCustomers();
List < CustomerSaldo > customersSaldo = CustomerListSaldo.GetCustomers();
int total = 0;
int totalCustomer = 0;
foreach(Customer customer in customers)
total++;
listCustomers.Items.Clear();
totalCustomer = total;
int x = totalCustomer;
foreach(CustomerSaldo customer2 in customersSaldo) {
if (x >= 1) {
listCustomers.Items.Add("Costumer ID # " + x + " is: " + customer2.Saldo);
x--;
}
}
}
This is what I get, it's ok but I want to know if exists a way to do this like this example:
Costumer #1 Saldo...
Costumer #2 Saldo...
Costumer #3 Saldo...
If you see my code I have a variable x, this variable is the total number of costumers, so I think that my sort has to start with this variable, but I don't know exactly how to do it, what can I do?
Reverse the list and start counting untill you reach the end:
//Reverse the list
customersSaldo.Reverse();
//Add new items
for(int i = 0; i < customers.Count; i++)
listCustomers.Items.Add(string.Format("Costumer ID # {0} is: {1}", (i+1).ToString(), customersSaldo[i].Saldo);
you can probably reverse (Using the Reverse method) your list "customersSaldo" before adding it, and then use the variable 'x' in increment order.
https://msdn.microsoft.com/en-us/library/b0axc2h2(v=vs.110).aspx
I have a C# Winform with a ListBox. I am trying to remove all the items except the last 5 items. The ListBox sort is set to Ascending.
The items in the ListBox look like the following:
2016-3-1
2016-3-2
2016-3-3
2016-3-4
...
2016-03-28
Here is my code to remove the beginning items.
for (int i = 0; i < HomeTeamListBox.Items.Count - 5; i++)
{
try
{
HomeTeamListBox.Items.RemoveAt(i);
}
catch { }
}
I've also tried HomeTeamListBox.Items.RemoveAt(HomeTeamListBox.Items[i]);
While there are more than n items in the list, you should remove items from start of the list.
This way you can keep the last n items of ListBox:
var n = 5;
while (listBox1.Items.Count > n)
{
listBox1.Items.RemoveAt(0);
}
Your index i is going to increase by one each time it loops, but you are going to be removing an element each time you loop. What you want to do is remove each element at index 0 for the first 5 passes. So using your current For Loop
HomeTeamListBox.Items.RemoveAt(HomeTeamListBox.Items[0]);
Is what you want in the body.
This should work for you;
if(HomeTeamListBox.Items.Count > 5)
{
var lastIndex = HomeTeamListBox.Items.Count - 5;
for(int i=0; i < lastIndex; i++)
{
HomeTeamListBox.Items.RemoveAt(i);
}
}
for(int i = HomeTeamListBox.Items.Count-5; i>=0; i--)
{
HomeTeamListBox.Items.RemoveAt(i);
}
I'm trying to multiply the values of 2 listboxes together and make their product appear at another list box I'm getting the results I need but the problem is when I rerun the loop using a command button the listbox removes the next instance of the first value calculated by ppc[i] * qty[i] but when I try to remove the the listBox4.Items.Remove(ppc[i] * qty[i]) it reprints the whole array again from first element to last element
string myString = textBox1.Text.ToString();
int index = listBox6.FindString(myString, -1);
int[] qty = new int[99];
int[] ppc = new int[99];
int[] gt1 = new int[99];
listBox3.Items.Add(listBox5.Items[index]);
listBox1.Items.Add(textBox2.Text.ToString());
if (index != -1)
{
listBox6.SetSelected(index, true);
listBox2.Items.Add(textBox1.Text); //name
}
listBox3.Items.Add(listBox5.Items[index]);
listBox3.Items.Remove(listBox5.Items[index]);
for (int i = 0; i != listBox2.Items.Count ; i++)
{
ppc[i] = Convert.ToInt32(listBox3.Items[i]);
qty[i] = Convert.ToInt32(listBox1.Items[i]);
listBox4.Items.Remove(ppc[i] * qty[i]);
listBox4.Items.Add((ppc[i] * qty[i]));
}
My understanding is that this loop works once, and then when it is re-run it is out of order. Are you making sure to clear listbox4 each time this loop is executed? Also since listBox2 isn't used, it is probably better not to use it for your loop bounds.
if(listBox1.Items.Count == listBox3.Items.Count)
{
int rowCount = listBox1.Items.Count;
listBox4.Items.Clear();
for (int i=0; i < rowCount; i++)
{
ppc[i] = Convert.ToInt32(listBox3.Items[i]);
qty[i] = Convert.ToInt32(listBox1.Items[i]);
listBox4.Items.Insert(i , (ppc[i] * qty[i]));
}
}
lets say i got combobox populated with items (items are some numbers for this particular example), and i want to tell C# something like this: IF there is some item selected in combobox, get that item and multiply it by 2. Is there any way that i can do this?
Assuming it was populated by a list of ints. e.g
for(int index = 0; index < 10; index++)
{
myCombobox.Add(index);
}
if (myCombobox.SelectedItem != null)
{
int value = ((int)myCombobox.SelectedItem) * 2;
}
Winforms of course
If they are strings then it would be something ike
if (myCombobox.SelectedItem != null)
{
int value = int.Parse(myCombobox.SelectedItem.ToString()) * 2;
}
I have a list box with items like A B C D E.
I also have two buttons Move UP and Move Down with it.
I have already made their properties false in the property window (F4).
When a user selects B or all the items below then my Move Up button should get enabled. It should be disabled for A item
In the same way my Move Down button should be enabled when the user selects D or all the items above it. It should be disabled for E.
Can you please provide me the right part of code to be written here.
Thanks....
I am doing a similar thing in my app. It also handles selection of multiple items and also checks if the multiple items that are selected are continuous or not.
Here's the code:
private bool SelectionIsContiguous(ListBox lb)
{
for (int i = 0; i < lb.SelectedIndices.Count - 1; i++)
if (lb.SelectedIndices[i] < lb.SelectedIndices[i + 1] - 1)
return false;
return true;
}
private void SetMoveButtonStates()
{
if (this.listBox.SelectedIndices.Count > 0)
{
if (this.listBox.SelectedIndices.Count > 1 && !SelectionIsContiguous(this.listBox))
{
this.btnMoveUp.Enabled = false;
this.btnMoveDown.Enabled = false;
return;
}
int firstSelectedIndex = this.listBox.SelectedIndices[0];
this.btnMoveUp.Enabled = firstSelectedIndex == 0 ? false : true;
int lastIndex = this.listBox.Items.Count - 1;
int lastSelectedIndex = this.listBox.SelectedIndices[this.listBox.SelectedIndices.Count - 1];
this.btnMoveDown.Enabled = lastSelectedIndex == lastIndex ? false : true;
}
}
Handle the SelectedIndexChanged event of the ListBox. If the SelectedIndex is greater than 0, enable "move up". If it is lesser than count - 1, enable "move down"
Here's the code I use in listBox_SelectedIndexChanged:
this.moveUp.Enabled = this.listBox.SelectedIndex > 0;
this.moveDown.Enabled = this.listBox.SelectedIndex > -1 && listBox.SelectedIndex < listBox.Items.Count - 1;
Actually it's in a method called from there as the code's called when the dialog's initialised too.