Reputation: 3911
hey guys, i have very small issue please see the code below
// this is main load
private void Form1_Load(object sender, EventArgs e)
{
tabAddRemoveOperator.SelectedIndex = 0;
}
//this is selected index changed event method
private void tabAddRemoveOperator_SelectedIndexChanged(object sender, EventArgs e)
{
if (tabAddRemoveOperator.SelectedIndex == 0)
//someCode;
else if (tabAddRemoveOperator.SelectedIndex == 1)
//someCode;
}
My problem is, i'm changing the tab selectedindex
on form_load
method so tab_selectedindexchanged
Event should get fired right ? but it isn't..
I have googled about this issue, so i found a thread saying that untill your controls are loaded their event fill not get fired, but i dn't think it is correct, coz all controls are get loaded n initialized in constructor only.. so i'm confused here with this issue.
Upvotes: 3
Views: 12651
Reputation: 21
I came up with another way to force a SelectedIndexChanged event to fire in the form_load. Set DeselectTab() in the form_load to the last tab on the control. Deselecting the last tab will cause the index to move to tab 0 and generate the SelectedIndexChanged event.
Upvotes: 0
Reputation:
I had almost the same issue (in my case I was setting the SelectedIndex in the form's constructor, but I was using a non-zero value - and the SelectedIndexChanged event did not fire).
I put a Timer on the form with a short delay, enabled it in the constructor, after the InitializeComponent call. In the timer's Tick event, I disabled the timer (so that it only happened once) and set the tab control's SelectedIndex there (and the SelectedIndexChanged event did fire).
public MainForm(string[] args)
{
InitializeComponent();
// tabControl1.SelectedIndex = 2; // Did not fire
OnceAtStartupTimer.Enabled = true;
}
private void OnceAtStartupTimer_Tick(object sender, EventArgs e)
{
OnceAtStartupTimer.Enabled = false;
tabControl1.SelectedIndex = 2;
}
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
// Code to run when SelectedIndex changes
}
Upvotes: 3
Reputation: 244772
The SelectedIndexChanged
event is never raised because the default SelectedIndex
is 0. When you set the SelectedIndex
to 0 in the form's Load
event handler, the selected index doesn't actually change to a new value, so the event is never getting raised.
There are two possible workarounds for this that come to mind:
You could refactor the initialization code for the case where SelectedIndex == 0
out to another method, and then call that method both from the SelectedIndexChanged
event handler and from the form's Load
event handler.
You could set the SelectedIndex
to an index other than 0 in the Load
event handler, and then immediately set it back to 0. This will cause the SelectedIndex
value to change twice, but will have the expected result.
Upvotes: 5
Reputation: 43523
Have you registered the event? Something like:
tabAddRemoveOperator.SelectedIndexChanged += new EventHandler(tabAddRemoveOperator_SelectedIndexChanged);
Upvotes: 0