Reputation: 45
my c# winforms program has one tabcontrol with a few tabs displaying listviews, labels, buttons etc. the tabpages are not shown on load. the code simplified looks like this:
public main()
{
InitializeComponent();
removeTabPages(); //removes all but one; deleting this line doesn't change anything
main_tabcontrol.SelectedIndex = 0; //doesn't change anything no matter where i put it
loadData();
doSomeCalculations();
addTabPages();
main_tabcontrol.SelectedIndex = 2; //same issue if i pick any other tab here
}
private void Tabs_SelectedIndexChanged(object sender, EventArgs e)
{
UpdateDataInTab();
}
this loads some data, does some calculations and then switches to tabpage 2. i would expect to see the data processed by the loadData() and doSomeCalculations() functions displayed. instead it displays the default values (mostly nothing) until i switch to another tab and then back. that also verifies Tabs_SelectedIndexChanged() works as intended.
i'd like to understand why this happens and how i can make it work as planned.
running loadData() and doSomeCalculations() as async tasks, and awaiting them, does solve this, but it opens so many other problems that i'd like to avoid it (i don't need this async). since my issue is the exact opposite (i need the code to run synchronous), this shouldn't be the solution anyways.
Upvotes: 0
Views: 194
Reputation: 4733
To use Load Event double click empty space on the form or use Properties Explorer and switch to events.
Upvotes: 0
Reputation: 131
It is because you have it in your constructor, the best place to put it is on the load:
private void main_Load(object sender, EventArgs e)
{
main_tabcontrol.SelectedIndex = 0;
loadData();
doSomeCalculations();
main_tabcontrol.SelectedIndex = 2;
}
Upvotes: 1