how to do: what i pick on combobox1 will show on combobox2? - c#

how to do: what i pick on combobox1 will show on combobox2 ?
is it possible ? (in C#)
thank's in advance

You need to subscride second combobox on the SelectedIndexChanged event of the first combobox and change value when event triggers. Also you need to make sure that both combobox have several items or you will need to add missing items to second combobox dynamically.
Event handler example:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
object selectedValue = this.comboBox1.SelectedValue;
this.comboBox2.SelectedValue = selectedValue;
}

Related

SelectionChangedCommited functionality in Combobox (XAML)

In the example below, when i try to set values to the combobox, the event is triggered and change objRunSettings.xxx before the value is set to cmbxxx.SelectedValue.
I suppose i need something like SelectionChangedCommited instead of SelectionChanged but i am really confused how to do it, as in xaml my only option is SelectionChanged
In xaml
<ComboBox SelectionChanged ="cmbxxx_selectionChanged"/>
In .cs -> set combobox values
cmbxxx.SelectedValue = objRunSettings.xxx;
Event
private void cmbxxx_selectionChanged(object sender, SelectionChangedEventArgs e)
{
objRunSettings.xxx = cmbxxx.SelectedValue.ToString();
}
The first item in the e.AddedItems IList will be the value you are looking for.
MSDN SelectionChangedEventArgs

ComboBox SelectedIndexChanged event fired twice for items with same DisplayMember

I have a combobox as below with 3 items ("abc", "abc" & "bbb").
The combobox also has AutoCompleteMode "SuggestAppend" & AutoCompleteSource "ListItems" properties. Now I want the text box to show the SelectedIndex of the combobox as below:
http://i.imgur.com/MJ4JdDN.png
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
this.textBox1.Text = this.comboBox1.SelectedIndex.ToString();
}
Everything seems fine until I select the 2nd "abc", the SelectedIndexChanged event will enter for the 1st time & display the index properly on the text box. But when the combobox lost focus, the SelectedIndexChanged event will fire again, causing the index to be displayed wrongly. I found that it only happens to items with the same value. Is there a way I can stop the event from firing twice?
http://i.imgur.com/gEw46xf.png
this.comboBox1.DataSource = dt;
this.comboBox1.DisplayMember = "Description"; //Some descriptive field to be shown in combobox
this.comboBox1.ValueMember = "Code"; //Unique code that user won't understand
this.comboBox1.SelectedIndex = -1;
put comboBox1_SelectedIndexChanged code in comboBox1_ValueChanged and there u'll get index in event arguments(sender i.e your comboBox cast it in combobox).
use comboBox1.SelectedIndex and u'll get the index.

get the clicked item from a IsItemClickEnabled ListView in C# and WinRT

how can I get the clicket elevent from a ListView which has the IsItemClickEnabled enabled?
I know how to get the selected Item/Index but not the clicked item.
ItemClick is working but I can not say s.th. like:
Object selection = listView1.SelectedItem;
EDIT:
I have a ListView and I need to catch the clicked item from this list in the following method:
private void listView1_ItemClick(object sender, ItemClickEventArgs e)
{
...
}
I may be missing something, but doesn't the following work for you?
private void lv_ItemClick_1(object sender, ItemClickEventArgs e)
{
var item = e.ClickedItem as String;
}
Here I assume the items in the list are simple strings, but in general they'll be whatever type you are using in the collection you've bound to the ItemsSource property of the ListView.
I guess you can also try the SelectionChanged event, and get the clicked item as e.AddedItems or MyListView.SelectedItem or MyListView.SelectedItems.

Manipulating combBox depending on another comboBox Properties

I have two comboBoxes comboBox1 & comboBox2. I would change some properties of comboBox1 & comboBox2 depending on the SelectedItem of comboBox1. I wrote the following code in Form1_Load block:
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
if(this->comboBox1->SelectedItem=="Letters"){
this->comboBox1->Size=System::Drawing::Size(238,27);
this->comboBox1->Location=System::Drawing::Point(301,73);
this->comboBox2->Visible= true;
}
else{
this->comboBox1->Size=System::Drawing::Size(473,27);
this->comboBox2->Visible= false;
}
}
But it doesn't work!
Is that code supposed to be inside the Form1_Load event? I think it should be under the comboBox1's SelectedIndexChanged event.

comboBox selected value change

I'd like to give the customer an option to choose a city from COMBOBOX, and once the city's chosen, a list of that city's streets should be in COMBOBOX2. I tried the following code and I got an error, during the first run, maybe someone can explain this to me?
private void Search_by_Apartment_Load(object sender, EventArgs e)
{
List<Cities> city = DAL.cities();
cmBxCity.DataSource = city;//Here he ran the second function, why?
cmBxCity.DisplayMember = "city";
cmBxCity.ValueMember = "cityID";
}
private void cmBxCity_SelectedIndexChanged(object sender, EventArgs e)
{
List<Streets> street = DAL.streets(Convert.ToInt32(cmBxCity.SelectedText));
// List<Streets> street = DAL.streets(Convert.ToInt32(cmBxCity.SelectedValue));
comBxStreet.DataSource = street;
comBxStreet.DisplayMember = "street";
//cmBxCity.ValueMember = "cityID";
}
The SelectedIndexChanged event is fired whenever the selected index is changed programatically or by the user.
As Davide Pirsa points out, when you change the DataSource of cmBxCity, you are programatically changing the selected index, hence firing the 'cmBxCity.SelectedIndexChanged' event at this line:
cmBxCity.DataSource = city;//Here he ran the second function, why?
One possible solution is to use the SelectionChangeCommitted event instead, which is only fired for changes made by the user.
when you assign the DataSource of the cmBxCity control its selectedItem changes from nothing to one item and this triggers the event handler cmBxCity_SelectedIndexChanged.
in the question you are talking about COMBOBOX and COMBOBOX2 but in the code there is only one control which is: cmBxCity.
shouldn't you show the streets in the second control called: cmBxStreet ?

Categories

Resources