C# combobox selected index changed fires old value in another combobox - c#

I have found the closest reference of my problem on these links:
ComboBox has its old value after Clear()
Why selected index fires only once the Index of the ListItem is changed?
I have 4 comboboxes: cmbcountry, cmbstate, cmbdistrict and cmbcity. cmbcountry populates on load event by GetCountry() method. cmbstate populates by GetState(countryid) method, which takes cmbcountry's selectedvalue as argument and returns a list of relevant states and so on for cmbdistrict and cmbcity...
Problem is on selecting different item in cmbState, cmbDistrct is not populating with the proper items.
.E.g. states :"madhya pradesh", "Utter pradesh" , "Rajsthan"
For "Rajsthan", cmbdistrict is populating with relavent value, but for "Utter pradesh", cmbdistrict still has old value.
Here is the relevant code:
private void TestForm1_Load(object sender, EventArgs e)
{
cmbCountry.ItemSource = Lookups.Lookup.GetCountries();
}
private void cmbCountry_SelectionChangeCommitted(object sender, EventArgs e)
{
cmbState.Text = "";
cmbState.Clear();
cmbState.SelectedIndex = -1;
cmbState.SelectedItem = null;
//cmbState.Items.Clear();
int countryId = Convert.ToInt32(cmbCountry.SelectedValue);
cmbState.ItemSource = Lookups.Lookup.GetStates(countryId);
}
private void cmbState_SelectionChangeCommitted(object sender, EventArgs e)
{
cmbDistrict.Text = "";
cmbDistrict.Clear();
cmbDistrict.SelectedIndex = -1;
cmbDistrict.SelectedItem = null;
//cmbDistrict.Items.Clear();
int stateId = Convert.ToInt32(cmbState.SelectedValue);
cmbDistrict.ItemSource = Lookups.Lookup.GetDistricts(stateId);
}
private void cmbDistrict_SelectionChangeCommitted(object sender, EventArgs e)
{
cmbCity.Text = "";
cmbCity.Clear();
cmbCity.SelectedIndex = -1;
cmbCity.SelectedItem = null;
//cmbCity.Items.Clear();
int DistrictId = Convert.ToInt32(cmbDistrict.SelectedValue);
cmbCity.ItemSource = Lookups.Lookup.GetCities(DistrictId);
}
private void cmbBank_SelectionChangeCommitted(object sender, EventArgs e)
{
int bankId = Convert.ToInt32(cmbBank.SelectedValue);
cmbControlBranch.ItemSource = Lookups.Lookup.GetBranches(bankId);
}
I have all above methods to clear previous data in comboboxes ...and items.clear() is giving an error if i am using it.
Please tell me if there is any other relevant reference which I am missing .

Related

Can't delete second or third selected index

I have 3 list boxes and when I have three things selected and click the delete button it deleted the text in the first list box but then gives a exception saying a value of -1 is not valid for index. This is my current code:
lstBoxOne.Items.RemoveAt(lstBoxOne.SelectedIndex);
lstBoxTwo.Items.RemoveAt(lstBoxTwo.SelectedIndex);
lstBoxThree.Items.RemoveAt(lstBoxThree.SelectedIndex);
wondered if theirs a way to delete all three selected text with a click of a button
Edit: I have now put image of full expection
Full exception
Edit 2: Here is an example with full code
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void LstBoxOne_SelectedIndexChanged(object sender, EventArgs e)
{
var deleteD = lstBoxOne.SelectedIndex;
lstBoxTwo.SelectedIndex = deleteD;
lstBoxThree.SelectedIndex = deleteD;
}
private void BtnInsert_Click(object sender, EventArgs e)
{
lstBoxOne.Items.Add(this.txtOne.Text);
lstBoxTwo.Items.Add(this.txtTwo.Text);
lstBoxThree.Items.Add(this.txtThree.Text);
this.txtOne.Focus();
this.txtOne.Clear();
this.txtTwo.Focus();
this.txtTwo.Clear();
this.txtThree.Focus();
this.txtThree.Clear();
}
private void BtnDelete_Click(object sender, EventArgs e)
{
lstBoxOne.Items.RemoveAt(lstBoxOne.SelectedIndex);
lstBoxTwo.Items.RemoveAt(lstBoxTwo.SelectedIndex);
lstBoxThree.Items.RemoveAt(lstBoxThree.SelectedIndex);
}
private void LstBoxTwo_SelectedIndexChanged(object sender, EventArgs e)
{
var deleteD = lstBoxTwo.SelectedIndex;
lstBoxOne.SelectedIndex = deleteD;
lstBoxThree.SelectedIndex = deleteD;
}
private void LstBoxThree_SelectedIndexChanged(object sender, EventArgs e)
{
var deleteD = lstBoxThree.SelectedIndex;
lstBoxOne.SelectedIndex = deleteD;
lstBoxTwo.SelectedIndex = deleteD;
}
}
}
}
The problem is due to the SelectedIndexChanged event on lstBoxOne.
Inside of BtnDelete_Click, when you remove the selected item from lstBoxOne this causes lstBoxOne.SelectedIndex to change. This causes LstBoxOne_SelectedIndexChanged to fire BEFORE the item is removed from lstBoxTwo and lstBoxThree. When LstBoxOne_SelectedIndexChanged finishes and returns to BtnDelete_Click, lstBoxTwo.SelectedIndex and lstBoxThree.SelectedIndex are both -1 and the Exception is occurring due to passing -1 to RemoveAt.
private void LstBoxOne_SelectedIndexChanged(object sender, EventArgs e)
{
var deleteD = lstBoxOne.SelectedIndex;
lstBoxTwo.SelectedIndex = deleteD;
lstBoxThree.SelecteIndex = deleteD;
}
private void DeleteBtn_Click(object sender, EventArgs e)
{
lstBoxOne.Items.RemoveAt(lstBoxOne.SelectedIndex); //This line triggers the SelectedIndexChanged event on lstBoxOne.
// By the time we get back to this function, lstBoxTwo and lstBoxThree have had
// their SelectedIndex set to -1. That is what is causing the exception to occur.
lstBoxTwo.Items.RemoveAt(lstBoxTwo.SelectedIndex); //Ooops! lstBoxTwo.SelectedIndex was set to -1 in LstBoxOne_SelectedIndexChanged.
}
A simple way to correct this would be to store the selected index of each ListBox before trying to remove any items. Also, protect yourself by checking for -1.
private void DeleteBtn_Click(object sender, EventArgs e)
{
int lstBoxOneIdx = lstBoxOne.SelectedIndex;
int lstBoxTwoIdx = lstBoxTwo.SelectedIndex;
int lstBoxthreeIdx = lstBoxThree.SelectedIndex;
if (lstBoxOneIdx > -1)
{
lstBoxOne.Items.RemoveAt(lstBoxOneIdx);
}
if (lstBoxTwoIdx > -1)
{
lstBoxTwo.Items.RemoveAt(lstBoxTwoIdx);
}
if (lstBoxThreeIdx > -1)
{
lstBoxThree.Items.RemoveAt(lstBoxThreeIdx);
}
}

dropdown list goes back to first value after selected index changes

I have a drop down list with a button event that should send it's value for a textbox.But,even if I choose a value that is not the first one in the DDL,it only sends the value of the first item in the DDL. I was told to add the !IsPostBack in the page load,but it didn't help.
Codes:
protected void Page_Load(object sender, EventArgs e)
{
string testeddl;
codProfessor = Request.QueryString["id"];
if (db.conecta())
{
ddlTeste.Items.Clear();
ddlTesteAltDel.Items.Clear();
ddlQuestoes.Items.Clear();
listaX = db.retornaTestes(codProfessor);
for (int i = 0; i < listaX.Count; i++)
{
testeddl = listaX[i].nometeste;
ddlTesteAltDel.Items.Add(testeddl);
}
protected void btnBuscarTeste_Click(object sender, EventArgs e)
{
if (db.conecta())
{
int posic = ddlTesteAltDel.SelectedIndex;
txtNomeTeste.Text = listaX[posic].nometeste;
ddlaltdelTeste.Text = listaX[posic].materiateste;
}
}
}
}
In Page_Load, just need to indicate:
if(!IsPostBack()
{
// rest of the code.
}

CopyListBoxItem from one ListBox to another

I have two ListBoxes. I want to copy SelectedItem from the first ListBox into Second one.
Why this code does not work ?
private void frm_addDispatchBoard2_Load(object sender, EventArgs e)
{
using(propertiesManagementDataContext db = new propertiesManagementDataContext())
{
var Buildings = db.Buildings.Select(q => new { q.BuildingLandNumber, q.BuildingId });
listBox_allBuildings.DataSource = Buildings;
listBox_allBuildings.DisplayMember = "BuildingLandNumber";
listBox_allBuildings.ValueMember = "BuildingId";
}
}
private void btn_addBuilding_Click(object sender, EventArgs e)
{
if(listBox_allBuildings.SelectedIndex > 0)
{
listBox_selectedBuildings.Items.Add(listBox_allBuildings.SelectedItem);
}
}
The result I got:
try this I am not sure why you are looking for a Contains but if you really need that look at the difference between SelectedValue and SelectedItem
Use this code right here as a test to see if the expected value shows up in a MessageBox
string selected = listBox_allBuildings.GetItemText(listBox_allBuildings.SelectedValue);
MessageBox.Show(selected);
this should help you to see the values in the Listbox on the right
private void btn_addBuilding_Click(object sender, EventArgs e)
{
if(listBox_allBuildings.SelectedIndex != -1)
{
var selected = listBox_allBuildings.GetItemText(listBox_allBuildings.SelectedValue);
listBox_selectedBuildings.Items.Add(selected);
}
}

How to obtain values from multiple Combo Boxes

As the titles suggest I am interested in obtaining the value from 4 combo boxes. All 4 combo boxes are the same in the that they list numbers from 0-9. I would like to take those numbers and assigning them to a single string variable. So for example if users selects (CB1 = 4)(CB2 = 3) (CB3 = 2) (CB4 = 1) I would like to take those selection and assign them to a string variable. Thanks in Advance.
-Nogard
if you are using winforms
string s"";
private void combobox1_SelectedIndexChanged(object sender, EventArgs e)
{
s = combobox1.Text+ combobox2.Text+ combobox3.Text+ combobox4.Text;
}
private void combobox2_SelectedIndexChanged(object sender, EventArgs e)
{
s = combobox1.Text+ combobox2.Text+ combobox3.Text+ combobox4.Text;
}
private void combobox3_SelectedIndexChanged(object sender, EventArgs e)
{
s = combobox1.Text+ combobox2.Text+ combobox3.Text+ combobox4.Text;
}
private void combobox4_SelectedIndexChanged(object sender, EventArgs e)
{
s = combobox1.Text+ combobox2.Text+ combobox3.Text+ combobox4.Text;
}
or call only a selected index change event in all comboboxes since all are doing same

Working with items in a ComboBox and ListBox

I want to move items back and forth between a ComboBox and a ListBox using C# 2010 (form)
My code seems to work. However, when I move the items back to the ComboBox (from the ListBox) I have a space in between the items. If anyone has a suggestion on how to remove the space between the items in the ComboBox I would greatly appreciate it.
private void stateslistcomboBox_SelectedIndexChanged(object sender, EventArgs e)
{
stateslistBox.Items.Add(statescomboBox.SelectedItem);
statescomboBox.Items.RemoveAt(statescomboBox.SelectedIndex);
}
private void stateslistBox_SelectedIndexChanged(object sender, EventArgs e)
{
string item = "";
item = Convert.ToString(stateslistBox.SelectedItem);
statescomboBox.Items.Add(item);
stateslistBox.Items.Remove(stateslistBox.SelectedItem);
}
The statescomboBox.Items.Add(item); triggers Another SelectIndexChanged that adds an empty item.
Try
private void stateslistBox_SelectedIndexChanged(object sender, EventArgs e)
{
string item = "";
item = Convert.ToString(stateslistBox.SelectedItem);
statescombobox.SelectIndexChanged -= stateslistBox_SelectedIndexChanged;
statescomboBox.Items.Add(item);
statescombobox.SelectIndexChanged += stateslistBox_SelectedIndexChanged;
stateslistBox.Items.Remove(stateslistBox.SelectedItem);
}
alternatively, you can prevent empty items being added.
private void stateslistBox_SelectedIndexChanged(object sender, EventArgs e)
{
string item = "";
item = Convert.ToString(stateslistBox.SelectedItem);
if (!string.IsNullOrEmpty(item)
{
statescomboBox.Items.Add(item);
stateslistBox.Items.Remove(stateslistBox.SelectedItem);
}
}

Categories

Resources