Easy one.
I added programatically a few items to a radiuo button list:
foreach (var item in res)
{
rbl.Items.Add(new ListItem(item.text, item.value.ToString()));
}
After a change of view (Multiview), I need to come back to the view with the radio button list, but this view added again the items.
How can I reset the items already added to the radio button list?
Used, and not working:
rbl.ClearSelection()
rbl.Datasource = null;
rbl.DataBind();
rbl = new RadioButtonList();
Thanks.
The "ClearSelection()" method simply puts the selection to be "-1", in order to remove the data in the list you will need
RadioButtonList1.Items.Clear();
Also be careful, where you actually add the items, that might cause a problem as well, since I cannot see additional code it is just my prediction.
Hope this solves your problem.
Cheers!
Related
I’ve designed windows form application with two drop down lists.
Each drop list contains four items which are hard coded.
When a button is clicked, it should reverse the selected items. I thought of using if conditions to arrive at the solution. But, any short code will be helpful.
Thanks
When the button is clicked, you could reasign the data sources of both drop downs:
// create new list of items in order to not lose them when clearin
var oldSource = comboBox1.Items.ToList();
// clear old items to make them not reappear
comboBox1.Items.Clear();
// reverse items
var reversedSource = oldSource.Reverse();
// iterating over the reversedItems and adding all items
foreach(var item in reversedSource)
{
comboBox1.Items.Add(item);
}
I have a List (named actors) of "Actors", a custom class to be displayed in a ListBox (named listBoxActors) in C#. The user will be able to click an item to highlight and select it, after which they can press a button that Removes that Actor from the list. Here is my code for the button when it is pressed:
Actor current = (Actor) listBoxActors.SelectedItem;
actors.Remove(current);
listBoxActors.DataSource = null;
listBoxActors.DataSource = actors;
However, even after pressing the button, the Actor still displays in the list box as if it had never been removed at all. Setting the DataSource to null and back to the actors list should refresh it (works fine for that purpose when I add actors), but the list remains the same. What should I add/remove? What am I doing wrong?
You can simply use Remove function:
listBoxActors.Items.Remove(listBoxActors.SelectedItem);
Hope it helps!!
So I want to be able to pass a combobox from one form to another as its the only things that remains the same. When I do it, is passes fine, has the correct items, however when I open the drop down there are no items, any idea why?
Hmmm, I don't know why that is, but what you might try doing is the following:
Rather than just passing in the entire combobox, just pass in the items from the previous combobox, and then make a new combobox on the form you are trying to pass it to, then populate it with the items you previously passed in as a parameter. Hope this helps!
I have now tried doing this and it now contains the items as it should
foreach (var loc in locations.Items)
Location_Selector.Items.Add(loc.ToString());
Location_Selector.SelectedIndex = locations.SelectedIndex;
But just setting one combo box to equal the other does not work, which makes no sense to me
Location_Selector = locations;
It is probably just easier to pass the items like this:
List<String> items = new List<string>();
items.AddRange(comboBox1.Items.Cast<String>());
int index = comboBox1.SelectedIndex;
Form2 form2 = new Form2();
form2.comboBox1.Items.AddRange(items.ToArray<object>());
form2.comboBox1.SelectedIndex = index;
form2.Show();
So you'd get the items to a list, then access a combobox on the next form and add the list to it. This will also copy the selected index too.
You could copy the ComboBox over, but it's more practical to just copy the items over.
i'm trying to change 'selectedItem' in the list box, but 'selectedItem' is staying as is even though i create a new item with different data. appreciate your help
this.listBox1.SelectedItem = new ListBoxItem(m_CurrentItem);
//next line operate the event list item changed
this.listBox1.Items[index] = this.listBox1.SelectedItem;
I think you're doing the operations in the reverse order ^^
You should first add the new item in the list box with
this.listBox1.Items.Add(YourNewItem);
Then you can select the newly inserted item with
this.listBox1.SelectedItem = YourNewItem;
Or, since the .Add Method adds the element in the last position of the Items array, you can use
this.listBox1.SelectedIndex = this.listBox1.Items.Count - 1
See this article for reference on the ListBox.SelectedItem Property: http://msdn.microsoft.com/it-it/library/system.windows.forms.listbox.selecteditem(v=vs.110).aspx
:)
I have listview control.There is an option to remove selected items.After the user removes an item.I need to programatically select the previous item just before the removed item or the First item.I have tried
listView.Items[0].Selected = true;
listView.Select();
No item is selected or Highlighted.What could be the problem?
ListView.Select doesn't select an item in the items collection.
The right syntax is
if(this.listView1.Items.Count > 0)
{
this.listView1.Focus();
this.listView1.Items[0].Focused = true;
this.listView1.Items[0].Selected = true;
}
See MSDN here
The listView's Items Collection does not have a Select() method. Instead call the listView's Select() method. However, in most cases it should work without it.
listView.Items[0].Selected = true;
listView.Select();
By the way, "it's not working" is not a good explanation of what is causing you trouble. Be a bit more specific next time.
The code you have posted will work fine. Are you doing anything else like giving the focus to another control? The default behavior of the listView is to hide selected items when it loses focus.
Set the property HideSelection of the listView to false and see if you are able to see the selection.