I have a listbox with multiple items in it (20). I often need to select 4 of them. Instead of clicking on each item in the list box to select it, I would like to just click a button beside the listbox and have it select the 4 items.
<ListBox Name="lbExample" SelectionMode="Multiple">
<ListBoxItem>a</ListBoxItem>
<ListBoxItem>b</ListBoxItem>
<ListBoxItem>c</ListBoxItem>
<ListBoxItem>d</ListBoxItem>
<ListBoxItem>e</ListBoxItem>
<ListBoxItem>f</ListBoxItem>
<ListBoxItem>g</ListBoxItem>
<ListBoxItem>h</ListBoxItem>
<ListBoxItem>i</ListBoxItem>
<ListBoxItem>j</ListBoxItem>
...
</ListBox>
<Button Name="btnSelectGroupOne" Click="btnSelectGroupOne_Click" Content="Group One"></Button>
I have tried the following (trying to select items by Index):
private void btnSelectGroupOne_Click(object sender, RoutedEventArgs e)
{
lbExample.SelectedItems.Add(0);
lbExample.SelectedItems.Add(1);
lbExample.SelectedItems.Add(2);
lbExample.SelectedItems.Add(3);
}
I have also tried by string:
private void btnSelectGroupOne_Click(object sender, RoutedEventArgs e)
{
lbExample.SelectedItems.Add("a");
lbExample.SelectedItems.Add("b");
lbExample.SelectedItems.Add("c");
lbExample.SelectedItems.Add("d");
}
When I try either of these nothing is highlighted in the listbox.
You need to pass the Item of the listbox in Add() method. You can do it like:
lbExample.SelectedItems.Add(lbExample.Items[0]);
lbExample.SelectedItems.Add(lbExample.Items[1]);
lbExample.SelectedItems.Add(lbExample.Items[2]);
lbExample.SelectedItems.Add(lbExample.Items[3]);
Try calling
lbExample.SetSelectedItems(new List<string>{lbExample.Items[0]});
Related
I am going to drag an item from a ListView to drop a box to process something.
In this case, I am not able to get selected ListView item. Selected Index/Items always returns -1/null.
Note: I am able to get selected listview item when using SelectionChanged.
But not able to get drop event. Please advise.
The XAML source:
<ListView x:Name="lvMaster" CanDragItems="True" SelectionChanged="lvMaster_SelectionChanged" />
<Grid AllowDrop="True" Drop="Drop_Event" DragOver="DragOver_Event">
</Grid>
The C# source:
private void Drop_Event(object sender, DragEventArgs e)
{
lvObj = new ListView();
ListView)sender;
}
private void DragOver_Event(object sender, DragEventArgs e)
{
AcceptedOperation = DataPackageOperation.Copy;
DragUIOverride.IsCaptionVisible = true;
DragUIOverride.IsContentVisible = true;
}
You can register DragItemsStarting event or DragItemsCompleted event for your listview, then in its handler method, you could get all items you dragged.
private void SourceListView_DragItemsCompleted(ListViewBase sender, DragItemsCompletedEventArgs args)
{
var cc = args.Items;
}
Please refer to the official Drag and Drop sample for more details.
Please
I'm having some trouble with Drag and Drop between different ListViews. I have a GridView that contains multiple ListView all from the same ObservableCollection. Basically, I want to be able to drag items from one ListView into another Listview but within the same GridView.
Below is the ListView Which is part of the DataTemplate for the GridView. The ListView is Bound to a ObservableCollection of string. Which is a Property or the ObservableCollection the GridView is bound too.
<ListView Name="ListingView" Height="200" HorizontalAlignment="Center" Grid.Row="1" CanDragItems="True" AllowDrop="True"
DragItemsStarting="ListView_DragItemsStarting"
DragOver="ListView_DragOver"
CanReorderItems="True"
DragItemsCompleted="ListView_DragItemsCompleted"
Drop="ListView_Drop" ItemsSource="{Binding Listing}"
/>
And here is the Code Behind for the ListView
private void ListView_DragItemsStarting(object sender, DragItemsStartingEventArgs e)
{
var item = string.Join(",", e.Items.Cast<string>());
e.Data.SetText(item);
e.Data.RequestedOperation = DataPackageOperation.Move;
}
private void ListView_DragOver(object sender, DragEventArgs e)
{
if (e.DataView.Contains(StandardDataFormats.Text))
{
e.AcceptedOperation = DataPackageOperation.Move;
}
}
private async void ListView_Drop(object sender, DragEventArgs e)
{
if (e.DataView.Contains(StandardDataFormats.Text))
{
var item = await e.DataView.GetTextAsync();
var destinationListView = sender as ListView;
var listViewItemsSource = destinationListView?.ItemsSource as ObservableCollection<string>;
if (listViewItemsSource != null)
{
listViewItemsSource.Add(item);
}
}
}
private void ListView_DragItemsCompleted(ListViewBase sender, DragItemsCompletedEventArgs args)
{
var item = string.Join(",", args.Items.Cast<string>());
var destinationListView = sender as ListView;
var listViewItemsSource = destinationListView?.ItemsSource as ObservableCollection<string>;
listViewItemsSource.Remove(item);
}
}
The Problem I have is finding a way to stop a ListView from becoming completely empty if I drag all the items out of the a ListView. Once the ListView becomes empty I can't go back and drag something into it.
The Second problem is when I drag an item and then let go of it in the same ListView it was dragged from then it deletes and does no re-add the item.
I think I need to add an If statement to the DragItemCompleted method to see If the source is the same as the target before removing the item.
Edit I seem to have fixed" the second problem by removing the CanReorderItemsProperty from the listView. I'm not sure why but it seems to have fixed up that issue. But now i can't reorder the lists :(
Look into this windows sample program https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/XamlDragAndDrop/cs
I have a problem. I have three buttons of main categories and when you click one of these buttons some things appear in ListBox and the buttons with subcategories appear. If you click on subcategory, consequently different things appear in ListBox.
I have methods like that:
private void DisplayPeople(string category); //I use it ParentClicked and SubClicked
private void ParentClicked(object sender, EventArgs e); //for parent category
private void SubCatClicked(object sender, EventArgs e); //for subcategory
myListBox.SelectedIndexChanged+= new EventHandler(selectedIndexChange);
When you select sth in listbox then it should appear in my DataGridView and it works perfectly. However, when I click on the button and things appear in listbox, and I put sth like myListBox.ClearSelected(); or my.SelectedItem = null; I see NOTHING is selected at the begining but still SelectedIndexChange event works because it adds first row to my DataGridView. I have no idea why, could u help me?
MUCH MORE SHORTER:
In my program when you select sth in ListBox, it appears in DataGridView. When i set myListBox.ClearSelected(); or my.SelectedItem = null;, nothing is selected in the begining but SelectedIndexChange event works and first thing in listbox is added to DataGridView. I don't want that, I want it to appear in datagridview only when it is selected by the user.
NOW MY EVENT HANDLER LOOKS LIKE THAT:
private void selectedIndexChange(object sender, EventArgs e)
{
Person person = (Person)MyListBox.SelectedItem;
if (MyListBox.Items.Count > 0 && MyListBox.SelectedItems.Count > 0)
{
Basket.Add(person);
dataGridView1.DataSource = Basket;
}
}
PS.
I SOLVED THE PROBLEM. I did it that the thing from listbox is added to DataGridView when user clicks on ListBox and SelectedIndexChange event appears. But maybe there is simpler and prettier solution?
That's exactly how SelectedIndexChanged is supposed to work.
What you have to do is compare myListBox.SelectedIndex to -1 or myListBox.SelectedItem to null to see if something is actually selected in the ListBox.
http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.selectedindexchanged(v=vs.110).aspx
try like this
private void myListBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (myListBox.Items.Count > 0 && myListBox.SelectedItems.Count > 0)
{
//Do something in DatagridView
}
else
{
//clear the gridview
}
}
I have a ListBox which is populated dynamically from a database in the code behind. I have another button and when i click the button, the button click event will get the all the selected listitem and insert the list item text to the database. I set AutoPostBack as false and EnableViewState is true in the listbox property
The problem is when i click the button , it only see 1 selected item even i select multiple items.Here are the codes. I appreciate your help. I spend 1 day on this issue and not getting anywhere.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
loadrdlist();
}
}
protected void loadrdlist()
{
((ListBox)TestFormView.FindControl("ListBoxB")).Items.Clear();
foreach (FailureTempRD rd in FailureTempRD.SelectFailureTempRD())
((ListBox)TestFormView.FindControl("ListBoxB")).Items.Add(new ListItem(rd.ReferenceDesignator, rd.ReferenceDesignator));
}
protected void btn_AddRD_Click(object sender, EventArgs e)
{
foreach (ListItem rd in ((ListBox)TestFormView.FindControl("ListBoxB")).Items) //This is where it only see 1 selected item
{
if (rd.Selected == true)
//put selected item to database
}
}
}
Here are both listbox and button
<asp:ListBox ID="ListBoxB" runat="server" SelectionMode="Multiple" ></asp:ListBox>
<asp:Button ID="btn_AddRD" runat="server" CausesValidation="False" onclick= "btn_AddRD_Click" Text="Add" />
Update :
I figure why. When i load the listitem, i need to add ID as listitem value. So change the following code. I test few times and it works as intend.
((ListBox)TestFormView.FindControl("ListBoxB")).Items.Add(new ListItem(rd.ReferenceDesignator, rd.ReferenceDesignator));
To
((ListBox)TestFormView.FindControl("ListBoxB")).Items.Add(new ListItem(rd.ReferenceDesignator, rd.ID));
Try using the GetSelectedIndices Method.
From above link:
Use the GetSelectedIndices method to identify or access the selected
items in the ListBox control. Each element in the returned array
represents the index for a selected list item. You can use the index
values to access the items in the Items collection.
i have one combobox and one listview
i want to select item of listbox and click on one clumn of listview multi clumn
then clumn name that i clicked be equal whit item name of listbox
tanks for answer
i want to select one item of listbox,then click on one clumn of listview's clumns,
then this clumn name be equal to selected item in listbox
private void DataValuelst_ColumnClick(object sender, ColumnClickEventArgs e)
{
DataValuelst.Columns[?].Text = Schemacmb.SelectedItem.ToString();
}
the index of clumn that i clicked must be replace whit ?
You can use the SelectedIndex property... So it should be something like this...
private void DataValuelst_ColumnClick(object sender, ColumnClickEventArgs e)
{
DataValuelst.Columns[Schemacmb.SelectedIndex].Text = Schemacmb.SelectedItem.ToString();
}