I have searched but cannot locate this problem.
On form 1 in code, I create a TabPage with a usercontrol in it and then add the TabPage to form1.TabControl and call public method LoadData on the usercontrol. Problem: I need to reload the data when the new tabpage is activated or gains focus. If I did not create the tabpage in code, I could simply use TabControl's selectedIndex change event, but it needs to be created in code. How can I do this? Form 1:
private void CreateNewTab()
{
TabPage tp1 = new TabPage();
tp1.Text = "HSV";
tp1.Name = "tpHSV";
if (tabContMain.TabPages.ContainsKey(tp1.Name) == false)
{
HSVControl hsvc = new HSVControl();
hsvc.Dock = DockStyle.Fill;
hsvc.LoadData();
tp1.Controls.Add(hsvc);
tabContMain.TabPages.Add(tp1);
}
}
====EDIT=============== Thanks for the comments. Let me try to explain my problem better. The selectedIndex change event works fine. I can access the tab by it's text or name. The problem is calling the hsvc.LoadData() method. I need to recall this method when the tab containing hsvc user control is clicked. The LoadData() is public, but I cannot find a way to access it in Form1 (which holds the selectedIndex change event). I need a reference to hsvc control.
I added a property to the Form1 class like this:
private UserControl mControl;
then assigning it:
HSVControl hsvc = new HSVControl();
hsvc.Dock = DockStyle.Fill;
hsvc.LoadData();
mControl=hsvc;
Then calling it in SelectedIndex change event, but it is still not visible there.
tabContMain.SelectedIndexChange +=
and then pressTab
key twice. – Reza AghaeiTabControl.SelectedIndexChanged
should work fine. What trouble are you having? Is it just that you have no way to refer to hsvc in that event handler? If so, simply add a field to your form class to hold a reference to it. – adv12hsvc.LoadData()
in theSelectedIndexChanged
event. Right now I changed my answer. – mostafa8026