Dynamic Combobox at SelectedIndex of ListBox - c#

In WPF application, I have a ListBox and binded with data from Sql. and also have a stackpanel inthe listbox.
once i double click the item of the Listbox, Where i need to place/add a dynamic combobox.
I can get the selected index of a ListBox.
int seleteditem = lstbxusername.SelectedIndex;
and i created dynamic Combobox
System.Windows.Controls.ComboBox cmb = new ComboBox();
cmb.Background = Brushes.Green;
Using Stackpanel to add a combobox
newstckpnl.Children.Add(cmb);
My question is how to add a dynamic combobox in the selected index of a listbox.
Is this possible or not?Helps appreciated.

Sure you can. You just need to get the ListBoxItem out of the ListBox first, as can be seen below.
var listBox = new ListBox();
var listBoxItem = listBox.SelectedItem as ListBoxItem;
var listBoxItemMargin = listBoxItem.Margin;
Currently you're adding strings to your ListBox. Strings obviously don't have margins. In order for the code above to work, you will need to add ListBoxItems to your ListBox as below.
listBox.Items.Add(new ListBoxItem {Content = dr.GetString(1) });

i did some thing , its worked for me!!
I get the selected index of a listbox
int seleteditem = lstbxusername.SelectedIndex;
Then created combobox
System.Windows.Controls.ComboBox cmb = new ComboBox();
By using combobox object , i added it in listbox selected index
lstbxusername.Items.Insert(seleteditem, cmb);

Related

Trying to disable few items in a ListBox c#

I'm trying to disable a few items dynamically from a Listbox. I used the following code:
How To Disable Selected Item In List Box
But, all I see is System.Data.DataRow in the Listbox, not the actual string.
I tried adding new BindingContext()
lbInsTypes.BindingContext = new BindingContext();
Also,
lbInsTypes.DataBindings.Add("DisplayMember", dtInsType, "InsuranceDescription", false, DataSourceUpdateMode.OnPropertyChanged);
but nothing worked.
DataSet dsInsType = db.GetAllInsuranceTypes(InstutionId);
lbInsTypes.DisplayMember = "InsuranceDescription";
lbInsTypes.ValueMember = "InsuranceTypeId";
lbInsTypes.DataSource = dsInsType.Tables[0];
I want to see the actual InsuranceDescription values in the ListBox from the table. Please show me how to use datasource with custom ListBox control.

Binding TextBox Text value to ComboBox.SelectedItem, and list from selected item of a ComboBox to another ComboBox

So I have a class "Worker" and a BindingList which is bound to a ComboBox.
workers = new BindingList<Worker>();
//// .. An "Add" button is somewhere there to add "Worker" objects to the list
//ComboBox Workers List
cbWorkersList.DataSource = workers;
cbWorkersList.DisplayMember = "Name";
I want to bind the "Text" field in a TextBox to the "Name" property of the selected "Worker" object, in the ComboBox.
Now I've done it by using the "cbWorkersList_SelectedIndexChanged" event handler, manually modifying txtWorkerName.Text property:
private void cbWorkersList_SelectedIndexChanged(object sender, EventArgs e)
{
if (cbWorkersList.SelectedItem != null)
{
txtWorkerName.Text = workers[workers.IndexOf((Worker)cbWorkersList.SelectedItem)].Name;
}
}
I tried to bound the TextBox in this way:
InitializeComponent();
txtWorkerName.DataBindings.Add("Text", cbWorkersList, "Text");
But it is only updating the TextBox if I click on other item on the list, if I use arrow keys to change the selection, the TextBox text field is not changing.
Is there a way to truly bind them so that if a ComboBox item is selected, the text field will change accordingly?
While on this topic, I also have other 2 ComboBoxes, and I would like to bind 1 to a list which is inside the others' ComboBox object.
What I want is a solution with the following logic:
cbPositions.Items = ((<Shift>)cbShifts.SelcetedItem).PositionsList
when
cbPositions is a ComboBox which should be populated with "Position" objects.
And cbShifts is a ComboBox which is populated with "Shift" objects, where each "Shift" contains a list of Positions.
How can I bind them so every time I choose an other Shift from the list, the Positions ComboBox will be populated with the Position objects from the Shift.PositionList?
And also, I would like to know if it's possible to bind the "Items" of a ComboBox to a List (my workers list in this example), not with code, but in the designer(Application Settings or DataBindings)?

SelectIndexChanged Combobox event won't fire when items list is empty

In my code I have combobox and I bound it by bindingData control.. And I used selectIndexChanged to do some code for each active item in the combobox..
My problem is in this case:
My bindingData is empty (so combobox index =-1) then I add item "item1" to my bindingData (active combobox item is "item1") but SelectIndexChanged is not firing. Why?
the code:
//first combobox is empty
myBindingSource.DataSource = null;
comboBox_Choixpiece.DataSource = myBindingSource;
//then combobox Contains 1 item but SelectIndexChanged event is not firing !!
myBindingSource.DataSource = someSource;
comboBox_Choixpiece.DataSource = myBindingSource;
please help me

ComboBox.SelectedValue not working

What is wrong with this code?
myComboBox.Items.Clear();
myComboBox.Items.AddRange(new string[]{"one","two"});
myComboBox.SelectedValue = "one";
It is showing up with nothing selected.
If you populate the combobox like this:
myComboBox.Items.AddRange(new string[]{"one","two"});
You must use the ComboBox.SelectedItem or the ComboBox.SelectedIndex property to set/get the selected item:
myComboBox.SelectedItem = "one"; //or
myComboBox.SelectedIndex = 0;
The ComboBox.SelectedValue property is inherited from
ListControl and must be used ONLY when:
the control is bound to a DataSource
and ValueMember and DisplayMember properties are definied.
A couple of different options:
1) change SelectedValue to SelectedIndex
myComboBox.SelectedIndex = 0; //your first item
Please ignore this, this is for asp.net
2) add in ListItems manualy
myComboBox.Items.Clear();
myComboBox.Items.Add(new ListItem() { Text = "one", Selected = true };
myComboBox.Items.Add(new ListItem() { Text = "two" };
Just make sure you don't have more than one item selected at a given time.

DataBind listBox selected item to textboxes

I have ListBox data bind to list of users (collection):
usersListBox.DataSource = null;
usersListBox.DataSource = _users;
usersListBox.DisplayMember = "Name";
usersListBox.ValueMember = "Id";
Now I want properties of selected item to be shown in different text boxes, so I do the binding:
nameTextBox.DataBindings.Add("Text", usersListBox.SelectedItem, "Name");
loginTextBox.DataBindings.Add("Text", usersListBox.SelectedItem, "Login");
When form load I can see that values of selected item appear in textboxes, but when selected item in listBox is changed, values in text boxes are still the same. Do I have to catch selectedItemChanged of listbox and repeat binding of textboxes? Or I'm missing something and values in textboxes should change with changing selected item?
If someone needs answer: you have to create binding source object and assign it list box and textboxes:
usersBindingSource = new BindingSource();
usersBindingSource.DataSource = _presenter.Users;
usersListBox.DataSource = usersBindingSource;
usersListBox.DisplayMember = "Name";
usersListBox.ValueMember = "Id";
nameTextBox.DataBindings.Add("Text", usersBindingSource, "Name", true, DataSourceUpdateMode.OnPropertyChanged);
loginTextBox.DataBindings.Add("Text", usersBindingSource, "Login", true, DataSourceUpdateMode.OnPropertyChanged);

Categories

Resources