Manipulating combBox depending on another comboBox Properties - c#

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.

Related

DataGridView CellValueChanged event

the event is triggered when the Form with the DataGridView is initialized, when the column headers texts are placed. So the function handling the event runs at start-up as many time as there are columns. How can I stop that and have it triggered only after the user made some changes in the datagrid?
Added:
It seems that changing from CellValueChanged to CellEndEdit does the trick.
Have you tried using the SelectionChanged property of the datagrid, triggering an event when the content changes?
<Grid>
<DataGrid> Name="TestDataGrid" SelectionChanged="DataGrid_EventName" />
</Grid>
Within the Code-Behind, the event will be captured:
private void DataGrid_EventName(object sender, SelectionChangedEventArgs e)
{
//Conduct work here
}
Edit: I'm quite sure that you will still need to initialize the DataGrid.
For example, you can use this event to extract the row-specific information from within the DataGrid:
private void DataGrid_EventName(object sender, SelectionChangedEventArgs e)
{
DataGrid _dataGrid = (DataGrid)sender;
DataRowView selectedRow = _dataGrid.SelectedItem as DataRowView;
if (selectedRow != null)
{
RowItemList = new List<string>(); //List declared outside of this method...
//add row information to the list
RowItemList.Add(selectedRow.ToString());
//or, get column-specific information
string ColmnEntry = selectedRow[ColumnName].ToString();
}
}

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.

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

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;
}

Categories

Resources