0
votes

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.

2
Use the same event here. You can assign event handler in code. See this msdn example. Or look into your designer generated code to see how it handles events. Or In your method write tabContMain.SelectedIndexChange += and then press Tab key twice.Reza Aghaei
Seems like TabControl.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.adv12
So call hsvc.LoadData() in the SelectedIndexChanged event. Right now I changed my answer.mostafa8026

2 Answers

0
votes

Ok, thanks again for the help. The solution was kind of staring me in the face. I'm not sure it's the best, but this worked very well.

Created interface:

  public interface IControlBase
{
    void LoadData();
}

Had UserControl implement interface:

 HSVControl : UserControl,IControlBase

and having the existing LoadData() method on the usercontrol.

change

private UserControl mControl; 

to

private IControlBase mControl;

Then in SelectedIndex change:

mControl.LoadData();
0
votes

TabControl Has a property called SelectedTab. use that property like:

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);
        tabContMain.SelectedTab = tp1;

    }

}

In that last line, It makes the TabControl to call its SelectedIndexChanged event. Then call LoadData event in this event:

private void TabControl_SelectedIndexChangedEvent(object sender, EventArgs e)
{
    //So Because Hsvc is a public field. I call its method here:
    if(TabControl.SelectedTab.Name = "My Desired Tab";
    {
        hsvc.LoadData();
    }
}