What is ListBox SelectedIndex Equivalent in ListView ?
How i can write the below code in ListView ?
if(listbox.SelectedIndex == -1)
{
}
There are two ways to check if you have an item selected
if(listview.SelectedItems.Count > 0)
{
}
or
if(listview.SelectedIndices.Count > 0)
{
}
If multisect is true you could have more than one element in this collections else only the Item selected
Related
I have a RadListBox which has 11 items. And I need to have the first 7 items as static which means they shouldn't be reordered. I have written the below javascript and it works just fine.
The 8th item in listbox is still movable(up) and this shouldn't happen. I need to reorder the items only after 7th item till 11th item and only within themselves. Means out of 11 items, first 7 items order should be static and from 8 to 11 these fields can be reordered.
Can anyone suggest how this can be achieved?
HTML:
<telerik:RadListBox ID="RadListBox" runat="server"
AllowReorder="true OnClientSelectedIndexChanging="RadListBox_Reordering"/>
JS:
function RadListBox_Reordering(sender, eventArgs) {
var value = eventArgs.get_item().get_value();
if (value == "Item1" || value == "Item2" || value == "Item3" || value == "Item4" || value == "Item5" || value == "Item6" || value == "Item7") {
eventArgs.set_cancel(true);
}
}
You can use the OnClientDropping event, which is cancelable, to detect where the reordered item is landing.
function OnClientDroppingHandler(sender, eventArgs) {
var index = args.get_destinationItem().get_index();
if (index < 8) {
alert('you may not drop on the first 7 items');
args.set_cancel(true);
}
}
https://www.telerik.com/forums/how-to-disable-dragging-dropping-to-first-position
It should reset the Combobox to no value, Combobox is in the same panel, but the code sets the index to 0 wich is the first value of the data binded list.
It works on the second click tho...ON the first click it sets the index to 0, on the second to -1.
if (((Button)sender).Parent.Controls.OfType<ComboBox>().Count() > 0)
{
foreach(ComboBox C in ((Button)sender).Parent.Controls.OfType<ComboBox>().ToList())
{
if(C.SelectedIndex != -1)
{
C.SelectedIndex = -1;
}
}
}
Perhaps you could try adding a dummy first item to your ComboBox to act as a placeholder.
This way you can deselect simply using ComboBox.SelectedIndex = 0;
Just be sure not to interpret this item in the ComboBox as a real item anywhere.
Also, try:
ComboBox.ResetText();
ComboBox.SelectedIndex = -1;
Or:
ComboBox.SelectedItem = null;
thanks all for answers...in the end I solved the issue with a workaround. The problem was, that button of the code above needed two clicks to set index to -1.
On the first click it moved to 0 and on the second to -1. I dont know why tho...Another problem was i had a index changed event on combobox and i wanted to fire it only once - not twice.
I solved the problem this way...
if (((Button)sender).Parent.Controls.OfType<ComboBox>().Count() > 0)
{
foreach(ComboBox C in ((Button)sender).Parent.Controls.OfType<ComboBox>().ToList())
{
if(C.SelectedIndex != -1)
{
C.SelectedIndexChanged -= this.ComboBox_Promo_SelectedIndexChanged;
while (C.SelectedIndex != -1)
{
C.SelectedIndex = -1;
}
C.SelectedIndexChanged += this.ComboBox_Promo_SelectedIndexChanged;
this.ComboBox_Promo_SelectedIndexChanged(C, EventArgs.Empty);
}
}
}
I'm trying to move the selection from listbox1 to listbox2 with this code
if (listBox1.SelectedItems.Count > 0)
{
int selectedindex = listBox1.SelectedIndex;
for (int i =0 ; i < listBox1 .SelectedItems.Count; i++)
{
listBox2.SetSelected(selectedindex , true);
}
}
I got a result , but when I select more than one item in listbox1 it will only select one item in listbox2 which is the first one I've selected in listbox1.
You are only storing one selection index of listbox1 in selectedIndex. Later in the loop you are always instructing listbox2 to set its selections to this one index.
Try this instead:
foreach (int index in listBox1.SelectedIndices)
{
listBox2.SetSelected(index, true);
}
You have to set the SelectionMode property to allow multiple selections. You can set in property window or on load event.
listBox2.SelectionMode.MultiExtended = SelectionMode.MultiExtended;
I am new to c#. In my project I have two controls ListBox and ListView
ListBox --> lbxEmpName
ListView --> lvEmpDetails
I tried the below code:
if (lvEmpDetails.Items.Count > 0)
{
for (int intCount = 0; intCount < lbxEmpName.Items.Count; intCount++)
{
for (int intSubCount = 0; intSubCount < lvEmpDetails.Items.Count; intSubCount++)
{
if (lvEmpDetails.Items[intSubCount].Equals(lbxEmpName.Items[intCount]))
{
lbxEmpName.Items.Remove(lbxEmpName.Items[intCount]);
}
}
}
}
If I run the above code, there are no matches between ListView Items and ListBox Items (Infact there must be some matches). When I debug my code, I saw the below thing: It is saying SelectedItem whereas I am giving here Items (Thats why my program is not matching items)
why it is showing SelectedItem = "" instead of Items ?
Am I doing something wrong in my code? Please suggest.
ListView's Items contains objects of type ListViewItem. So there is no use in comparing those with objects in ListBox's Items.
If you want to compare their text, you must write something like this:
if (lvEmpDetails.Items[intSubCount].Text == (string)lbxEmpName.Items[intCount])
{
// Do something here
}
Please note that a ListViewItem can have multiple sub-items and its Text property returns the first column of its data.
Compare the string values that you want to compare not the object themselves.
How can I check if no items "At all" selected from listview?
Thanks.
if( myListView.SelectedItems == null || myListView.SelectedItems.Count == 0 )
{
}
See ListView..::.SelectedItems Property for more info.
EDIT: As per the MSDN documentation:
If no items are currently selected, an
empty
ListView..::.SelectedListViewItemCollection
is returned.
So the null check is not needed in this case and you can simply do:
if( myListView.SelectedItems.Count == 0 )
{
}