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!!
Related
I created a project that collects Personal Informations. I added a combobox to get title of Personal. Combobx items which is in ENUM, added at load. I save informations at listview, everythings Runs Perfectly i have double click event for listview. It returns all information to the their first position like Textboxes. But Combobox doesnt get its value to back. Can you show me how i can do ?
Enums Added ComboBox
cbUnvan.Items.AddRange(Enum.GetNames(typeof(Unvan)));
if Combobox item (which is Enum) selected, goes to personel.Unvan Property. (Personel is Class)
personel.Unvan = (Unvan)Enum.Parse(typeof(Unvan), cbUnvan.SelectedItem.ToString());
When i double click to listview item informations goes to their places. example is at below
txtTC.Text = lvTablo.SelectedItems[0].SubItems[0].Text;
dtpIseGirisTarihi.Value = Convert.ToDateTime(lvTablo.SelectedItems[0].SubItems[3].Text);
MY PROBLEM IS HERE -- But Combobox doesnt do same action with this code below
cbUnvan.SelectedItem = (Unvan)Enum.Parse(typeof(Unvan), lvTablo.SelectedItems[0].SubItems[8].Text);
Well, i'm using ASP.NET c# listbox. I also have a button, the idea is the following: whenever I click the button an item x has to be shown in the listbox. However, I click the button many times, and instead of replacing the x value in the list for the new value (pretend it's y), it shows the following:
x
y
The expected result would be:
y
I mean, i don't want values to be on top of eachother, just one at a time.
You should be having a code behind file where the selected items (selected by clicking the button) should be getting added to the listBox object.
When you run a loop to add items to this listBox object, use the Contains method to check if the listBox object already contains the current item. Add only if it does not contain.
At code behind :
Before that you need to create list and store all the listbox value. if listbox doesn't have value then keep list as null.
Every time you need to check whether the list contain the value or not if list contain value then you don't need to add, if not contain then you need to add.
in Jquery :
on button click first get all the listbox list item value and check that in that list whether that list contain the value or not which you want to add.
May be this is help full for you.
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!
Uses: VS 2012;
I've a combobox attached to a datasource in my form. And things work fine. When I run the form, again everything works fine; I can select an item in the dropdown list and it updates to the datasource as well. My problem comes when I need to deselect/revert what I have selected after I saved or Remove what I have select (basically should go as null for that field value).
Our legacy system was built in Delphi 3 & 5, and users got a feature of right-clicking on the dropdown list and get a small popup like button named
Blank
which blanks what have been selected. I could not find anything that will do the same what ever user have selected in .NET's combo box.
You can add a new item in dropdown named -Select-( or something similar name) by using following code:
drp.DataSource = dataSet;
drp.DataBind();
// do it after binding
drp.Items.Insert(0, new ListItem("-Select-", "NA"));
If you are binding in xaml then on page_load event you can write only this line
drp.Items.Insert(0, new ListItem("-Select-", "NA"));
Now if user want to deselect choice, he/she will simply select -Select- item.
Whilst thanking all of your Answers and suggestions, I used #V4Vendetta's idea and composed my solution.
Similarly to deleting a record in a datagridview where you click Delete key, I took the same concept and associated my solution with Delete Key.
What I did was created a handler for ComboBox's Keypress Event like:
private void comboBox_KeyPress(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete)
{
(sender as ComboBox).SelectedIndex = -1;
}
}
And linked to every ComboBox's available
ComboBox1.KeyDown += new KeyEventHandler(comboBox_KeyPress);
ComboBox2.KeyDown += new KeyEventHandler(comboBox_KeyPress);
Now when user clicks Delete key while the ComboBox selected/active, it gets blank.
I have a ListBox which when clicked displays data to the user. If the user enters invalid data and they click another item in the list I want to prevent the ListBoxItem they clicked from being selected. However, currently the ListBoxItem_Selected event is firing after the ListBoxItem is being selected, so I don't know how to stop the next list box item from being selected.
In the ListBoxItem_Selected event, I'm validating the data but by this point it's too late.
Simply executing
((ListBoxItem)this.ListBox.Items[previousIndex]).IsSelected = true;
or
this.ListBox.SelectedIndex = previousIndex;
does not work, i.e. the next list item is still selected.
How can I prevent the next item from being selected if the data they entered is invalid?
So the best way to do this would be to Bind the Listbox enabled to a bool in your code.
Something like this
public bool enabledOrNot { get; set; }
<Listbox isEnabled={Binding enabledOrNot}>...
Then when you do the validation on your first input you can set this bool and it will enable or disable as needed.
Another way to do this would be to use ValidationRules. I know we use these in our code at work but they are to arcane for me to understand. All I know is they somehow validate before anything else takes place. Its a built in .NET feature
http://msdn.microsoft.com/en-us/library/system.windows.data.binding.validationrules.aspx