Reputation: 171
I have this event for changing index of combo box;
DialogResult Result = MessageBox.Show("something", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (Result == DialogResult.No)
{
// the code write follow
}
if (Result == DialogResult.Yes)
{
NotGrazingradioButton.Checked = true;
if (CowTypeSelect.SelectedIndex == 0)
{
CowTypeDefaults.LactatingCow(this);
CowTypeVarlbl.Text = "گاو شیری";
}
else if (CowTypeSelect.SelectedIndex == 1)
{
CowTypeDefaults.DryCow(this);
CowTypeVarlbl.Text = "گاو خشک";
}
the problem is when user click on no button still the new index shows on combobox however it not selected,
I define a variable for previous selected index in an another event like this:
public int CowTypeSelect_SelectionChangeCommitted(object sender, EventArgs e)
{
int PrevIndex = CowTypeSelect.SelectedIndex;
return PrevIndex;
}
now I want to use this variable for changing combobox index to it's preview index like this but this code doesn't work and I don't know why
if (Result == DialogResult.No)
{
CowTypeSelect.SelectedIndex = CowTypeSelect_SelectionChangeCommitted.PrevIndex;
}
now if assume that these code for no button work it become another problem because it select an Index and now codes runs again and values of textbox that related to combobox's index return to default value :(
I read many topics about my problem but I couldn't figure it out.
Upvotes: 2
Views: 2251
Reputation: 3163
I come from the previous question you've asked, seems like you're quite new to Windows Form
. But since you had tried it by yourself, It makes the ones who answer feel better.
Add a global variable LastIndex
storing the last verified index.
Add a global variable InhibitIndexChange
to check if the index change event is fired by the users. For more information about this, see Cancelling ListBox SelectedIndexChange Event
Check InhibitIndexChange
before processing the code you wish to execute during the IndexChangedEvent
Check the message box result, if it's No
, you should rollback the selected index, otherwise, update it to the newest index.
Full Code
int LastIndex = -1;
bool InhibitIndexChange = false;
private void CowTypeSelect_SelectedIndexChanged(object sender, EventArgs e)
{
if(InhibitIndexChange)
return;
DialogResult Result = MessageBox.Show("Type text here", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (Result == DialogResult.No)
{
if(LastIndex != -1)
{
InhibitIndexChange = true;
CowTypeSelect.SelectedIndex = LastIndex;
InhibitIndexChange = false;
}
return;
}
NotGrazingradioButton.Checked = true;
if (CowTypeSelect.SelectedIndex == 0)
{
CowTypeDefaults.LactatingCow(this);
CowTypeVarlbl.Text = "گاو شیری";
}
else if (CowTypeSelect.SelectedIndex == 1)
{
CowTypeDefaults.DryCow(this);
CowTypeVarlbl.Text = "گاو خشک";
}
LastIndex = CowTypeSelect.SelectedIndex;
}
And for it to work on the previous question you asked
if (FirstRun == true)
{
// Codes here execute at the first time only.
...
LastIndex = CowTypeSelect.SelectedIndex;
FirstRun = false;
return;
}
Upvotes: 2
Reputation: 469
I guess that's what you want here:
private int prev_index = -1;
private void CowTypeSelect_SelectedIndexChanged(object sender, EventArgs e)
{
DialogResult Result = MessageBox.Show
("somthing",MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (Result == DialogResult.No)
{
CowTypeSelect.SelectedIndex = prev_index;
return;
}
else if (Result == DialogResult.Yes)
{
NotGrazingradioButton.Checked = true;
if (CowTypeSelect.SelectedIndex == 0)
{
CowTypeDefaults.LactatingCow(this);
CowTypeVarlbl.Text = "گاو شیری";
}
else if (CowTypeSelect.SelectedIndex == 1)
{
CowTypeDefaults.DryCow(this);
CowTypeVarlbl.Text = "گاو خشک";
}
}
prev_index = CowTypeSelect.SelectedIndex;
}
Upvotes: 2