In my application I have 2 ComboBox. When I select a ComboBoxItem in the first ComboBox the second one generates the relative ComboBoxItem. But if I create a SelectionChanged event on the second ComboBox I get this error. Why? Thanks!
private void scarpeBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBox combo = (ComboBox)sender;
ComboBoxItem item = (ComboBoxItem)combo.SelectedItem;
for (int i = 0; i < 16; i++)
if (combo.Items[i] == item) id = i;
}
Your SelectedItem is of value String, it's not a control like you thought it would be. You're trying to convert your String to a ComboboxItem, which throws your exception.
In your example, I would use the SelectedIndex property:
private void scarpeBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBox combo = (ComboBox)sender;
id = combo.SelectedIndex;
}
Your loop seems to look for the position of your SelectedItem, so replace your code with above, and it will return the position of the item in the ComboBox.
Related
I am populating a list-view with passwords.
Then I want to take the selected items text and pass it to a text box when it is clicked.
So far I have:
private void passwordListView_SelectedIndexChanged(object sender, EventArgs e)
{
ListViewItem listViewItem = new ListViewItem();
listViewItem = passwordListView.SelectedItems[0];
passwordTextBox.Text = listViewItem.Text;
}
It works the first time I press it and it populates the textbox but then if I click a different password in the list-view it throws an exception.
Have I left out something blatantly obvious?
When the Selected Index is changed in a winforms ListView the first event is for the item that has now been deselected. So at that point SelectedItems is empty.
Check for this through if (passwordListView.SelectedItems.Count == 0) return;
After this you will get a second event, which will be for the new selection, and you can act on this.
Btw, you don't need to make a new ListViewItem as you are in your snippet, this will save the extra unnecessary creation:
ListViewItem listViewItem = passwordListView.SelectedItems[0];
Maybe you could try this :
private void passwordListView_SelectedIndexChanged(object sender, EventArgs e)
{
if(passwordListView.SelectedItems.Count > 0)
passwordTextBox.Text = passwordListView.SelectedItems.First().Text;
}
I have a ComboBox which is populated on Page Load event. Just after populating the ComboBox I call another method which returns a value that I want to make the default value of the ComboBox when Page loads. How can I change the selectedIndex to a value that is returned when I call another method?
XAML for ComboBox
<ComboBox Name="cboProductType" DisplayMemberPath="ProductTypeName" SelectedValuePath="ProductTypeID" SelectedIndex="0"/>
Page Load event:
void OnProductDetailLoad(object sender, RoutedEventArgs e)
{
GetServiceReference.Service1Client service = new GetServiceReference.Service1Client();
service.GetProductDetailsCompleted += service_GetProductDetailsCompleted;
service.GetProductTypeCompleted += service_GetProductTypeCompleted;
service.GetProductTypeAsync();
service.GetProductDetailsAsync(ProductId);
}
Populating ComboBox when Page Loads:
void service_GetProductTypeCompleted(object sender, GetProductTypeCompletedEventArgs e)
{
cboProductType.ItemsSource = e.Result;
}
Calling the other method which returns a particular ProductTypeName. I tried to get the index of that particular ProductTypeName but returns -1 always.
void service_GetProductDetailsCompleted(object sender, GetServiceReference.GetProductDetailsCompletedEventArgs e)
{
if (e.Result.Count != 0)
{
p.ProductID = e.Result[0].ProductID;
int index = cboProductType.Items.IndexOf(e.Result[0].ProductTypeName);
cboProductType.SelectedIndex = index;
}
Also is this a right approach for setting the SelectedIndex property?
Say I have following items loaded in ComboBox in following Index order:
Index DisplayMemberName(ProductTypeName)
0 Color
1 Size
2 Variant
3 N-size
and e.Result[0].ProductTypeName contains Variant so I now want SelectedIndex = 2 of ComboBox
I hope my question is clear
The SelectedIndex is only valid when you manually fill a combobox. If you are using the ItemsSource to fill the items in a combobox, you must define a SelectedValuePath which will then allows you to use the SelectedItem property to SET/GET the selected item. When using binding to a DataContext to bind the controls, the SelectedValuePath should be the property that links the Parent/Child together.
i want to add selected item from a data bounded listbox (listbox1) to another listbox (listbox2)
Here is the code on a click event of a button.
private void btnrgt_Click(object sender, EventArgs e)
{
string x = listBox1.SelectedItem.ToString();
listBox2.Items.Add(x.ToString());
txttestno.Text = listBox2.Items.Count.ToString();
}
When i run this code System.data.datarowview get displayed in the listbox2.
Kindly help.
thank you in advance.
When you bind a ListBox datasource to a DataTable every item inside the ListBox is a DataRowView, not a simple string. In the ListBox you see a string displayed because you set the DisplayMember property of the ListBox with the name of a column in that DataRowView.
So, taking the current SelectedItem doesn't return a string but a DataRowView and calling ToString() for a DataRowView returns the full qualified name of the class (System.Data.DataRowView).
You need something like this
private void btnrgt_Click(object sender, EventArgs e)
{
DataRowView x = listBox1.SelectedItem as DataRowView;
if ( x != null)
{
listBox2.Items.Add(x["NameOfTheColumnDisplayed"].ToString());
txttestno.Text = listBox2.Items.Count.ToString();
}
}
EDIT
It is not clear what is the source of the error stated in your comment below, however you could try to avoid adding an item from the first listbox to the second one if that item exists in the second listbox with code like this
private void btnrgt_Click(object sender, EventArgs e)
{
DataRowView x = listBox1.SelectedItem as DataRowView;
if ( x != null)
{
string source = x"NameOfTheColumnDisplayed".ToString();
if(!listbox2.Items.Cast<string>().Any(x => x == source))
{
listbox2.Items.Add(source);
txttestno.Text = listBox2.Items.Count.ToString();
}
}
}
This solutions works if your second listbox is really populated adding simple strings to its Items collection.
On click button use below code.
protected void btnGo_Click(object sender,EventArgs e) {
string x = ListBox1.SelectedItem.Text;
ListBox2.Items.Add(x);
}
I'm having some problem to get the index of the selected row in a listview. I wonder why this code isn't working? I get a red line below the SelectedIndex
private void lvRegAnimals_SelectedIndexChanged(object sender, EventArgs e)
{
int index = lvRegAnimals.SelectedIndex;
string specialData = motelManager.GetInfoFromList(index);
UppdateSpecialData(specialData);
}
Help is preciated. Thanks!
EDIT:
For some strange reason I get two messages when I click on one of the lines in the listView!? First I get the previous number and then the number for the last clicked line. What could be wrong?
private void lvRegAnimals_SelectedIndexChanged(object sender, EventArgs e)
{
int index = lvRegAnimals.FocusedItem.Index;
MessageBox.Show(Convert.ToString(index));
}
It's working now when I added a check like this:
if(lvRegAnimals.SelectedIndices.Count > 0)
Because ListView doesn't contain any SelectedIndex, instead there is a property of SelectedIndices.
var indices = lvRegAnimals.SelectedIndices;
//indices[0] you can use that to access the first selected index
ListView.SelectedIndices
When the MultiSelect property is set to true, this property returns a
collection containing the indexes of all items that are selected in
the ListView. For a single-selection ListView, this property returns a
collection containing a single element containing the index of the
only selected item in the ListView.
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
// Acquire SelectedItems reference.
var selectedItems = listView1.SelectedItems;
if (selectedItems.Count > 0)
{
// Display text of first item selected.
this.Text = selectedItems[0].Text;
}
else
{
// Display default string.
this.Text = "Empty";
}
}
Try :
listView1.FocusedItem.Index
This give you the index of the selected row.
There is another thread like this one, but here it goes again.
It can return NULL. Also the SelectedIndexChanged event can be FIRED TWICE. And the first time, there nothing selected yet.
So the only safe way to find it is like this:
private void lv1_SelectedIndexChanged(object sender, EventArgs e)
{
if (lv1.FocusedItem == null) return;
int p = lv1.FocusedItem.Index;
... now int p has the correct value...
The ListView is a darn hassle to work with sometimes.
A simple solution i've used is a for loop that checks for the
selected Item.
I've put my solution in the "When index change trigger" within the ListView.
Example:
int sel_item = 0; //an int to store the selected item index.
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
for (int i = 0; i < listView1.Items.Count; i++)
{
if (listView1.Items[i].Selected == true)
{
sel_item = i;
}
}
}
This would ofcourse only work correctly with the "Multiselection" option set as false.
I'm currently using DataGridView with three cells, and the first cell is
DataGridViewComboBoxColumn object, and I want to ensure whenever I select any new item in DataGridViewComboBoxColumn object other cells of dataGridview get empty. It doesn't matter if I reselect the same item again.
Could anyone please tell me how should I ensure that I've selected new item in DataGridViewComboBoxColumn object? Which property or method should I use for this approach?
You can declare a global List<int> gridComboSelections and when you bind your DataSource to your grid, you can fill this list with the SelectedValues of the comboboxes. When any of the combobox' value is changed, find the position of the combobox and check if it is same with gridComboSelections[i]. If it is same end operation, if not do what you want with it. If the value is changed, remember to change the corresponding value on your List.
You can refer following code which does same thing.
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
ComboBox combo = e.Control as ComboBox;
if (combo != null)
{
combo.SelectionChangeCommitted += new EventHandler(combo_SelectionChangeCommitted);
}
}
void combo_SelectionChangeCommitted(object sender, EventArgs e)
{
DataGridViewComboBoxEditingControl combo = sender as DataGridViewComboBoxEditingControl;
if (combo != null)
{
for (int columnIndex = 0; columnIndex < dataGridView1.ColumnCount; columnIndex++)
{
if (columnIndex != combo.EditingControlDataGridView.CurrentCell.ColumnIndex)
{
dataGridView1[columnIndex, combo.EditingControlRowIndex].Value = null;
}
}
}
}