开发者

tabcontrol selectedindex changed event not getting fired C#

开发者 https://www.devze.com 2023-01-26 01:51 出处:网络
hey guys, i have very small issue please see the code below // this is main load private void Form1_Load(object sender, EventArgs e)

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 (tabAdd开发者_Python百科RemoveOperator.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.


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:

  1. 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.

  2. 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.


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            
    }


Have you registered the event? Something like:

tabAddRemoveOperator.SelectedIndexChanged += new EventHandler(tabAddRemoveOperator_SelectedIndexChanged);


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消