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.
Related
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!
I have a comboBox and I can save an item to it in runtime, but, every time I close the application the items disappear, I have searched for it, but, i couldn't find anything to do so.
I have the following code:
if (!comboBox1.Items.Contains(comboBox1.Text))
comboBox1.Items.Add(comboBox1.Text);
It's a Windows form application, it uses C# and SQL for managing a DB and it's tables, I would like to use o comboBox so the user don't need to type the same thing over and over when is going to put a value.
Is there a way to "permanently" save the items? Maybe using an SQL DB ou something like that?
You can either define the ComboBox items in a database table or in your application. If you're looking for a quick solution and know what all of the ComboBox options should be, you can define them in an array and then assign that array to the ComboBox DataSource:
string[] items = new string[] {"--Select--", "Item1", "Item2"};
comboBox1.DataSource = items;
comboBox1.SelectedIndex = 0;
I have an ASP.Net web for that inserts values into a database, I want to reset the selected items in the dropdownList, how do I do it properly?? I know with textBoxes I can use TextBox1.Text = String.empty but it doesnt seem to work with the dropdownLists
If you want to clear all Items means you can use
aspDrop.Items.Clear(); // whill clear all the list items
If you want to change the selected index means you have to use
aspDrop.SelectedIndex = someIndex; // where index must be in the list
Or even you can use .Insert() method to insert an item to a particular index and make it as selected item.
For DropDownList it would be better to first add an empty string at first position of the DropDownList by using yourDropDownList.Items.Insert method and then select it. Something like this:
yourDropDownList.Items.Insert(0, new ListItem(string.Empty, string.Empty));
yourDropDownList.SelectedIndex = 0;
Well if you want to clear dropdownList fully so you can clear it's items source such in this way:
ddItems.Clear();
But if you only want to clear selected dropdownListItem than S.Akbari solution:
ddItems.Items.Insert(0, new ListItem(string.Empty, string.Empty));
ddItems.SelectedIndex = 0;
I guess, everyone has given answers. But remember one thing to do as follows:
AppendDataBoundItems="False"
Set it to false as this will cause the rebound data to be appended to the existing list which will not be cleared prior to binding. The above is applicable when data is bound to the DropDownList. The rest will be done clearing it.
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 a combobox that I want to be able to add items to the beginning of. For example, when you click it, you get 1,2,3 , but I want to be able to add an option, 0, so that when you click it you get 0,1,2,3.
Is this possible without rebuilding the combobox?
Just use the Insert method of the Items property of the combo box:
myComboBox.Items.Insert(0, "New item at top");
Yes, you can use the Insert() method on the Items property of your ComboBox object.
var comboBox = new ComboBox();
comboBox.Items.Add("1");
comboBox.Items.Add("2");
comboBox.Items.Add("3");
comboBox.Items.Insert(0, "0");