Problem Setting Tab Control selectedIndex
I'm having trouble setting a Tab Control Index under a specific circumstance and feel like I'm fighting with VS, as usual. Here's the scenario:
I have a Windows form with a tab control on it. Inside one of the tabs is a grid. When the user clicks on a row on the grid, the current window is closed and a new one opens where the user can edit data in the selected row. When done, the user clicks Update, and they should be taken back to the first screen, where the tab they were on should be redisplayed so the user appears to be back where they started.
To do this, I put code behind the TabControl.TabIndexChanged event to store the selected tab index in a global (this was a trick unto itself - tabpage.tabindexchanged and tabpage.enter don't seem to work at all, and tabpage.click only works if the user clicks inside the tab container and not on the tab selector as would be expected, but that's a different issue). Then when the user returns to the page, I should be able to simply set the tabcontrol.selectedindex and have the right tab displayed (and, btw, tabcontrol.tabpage(index).focus doesn't appear to work either).
Here's the rub. In the Windows Form Designer Generated Code part of the form code, the definition for the tabcontrol includes the statement:
tabcontrol.selectedindex = 0
That would be fine if it was just establishing a default value that could then be overridden in the form load as I intended, but this statement causes the tabcontrol.TabIndexChanged event to fire which overrides the global variable as described above, and I end up with the default value of 0 now in my global and when it hits the Form.Load it selects the wrong tab. I don't nuderstand why events are fired during Initialization, but I suppose MS thought they were doing me a favor somehow. They aren't!
Having found this, it seemed like the simplest solution was to change the Windows Form Generated code to reference my global variable instead of 0. This works great in the designer, but as soon as I close the form, VS appears to REBUILD the entire Windows Form Generated section of the code and my change is overwritten, with no warning whatsoever. I've also tried commenting the darn line out and it keeps coming back every time I save the form. It's maddening!
Is there any way to override this unauthorized change to my code, or prevent events from firing during form initialization? Or will I have to develop yet another workaround?
|