从父级访问用户控件元素

时间:2014-08-19 04:38:08

标签: c# winforms user-controls parent

我正在尝试通过检查表单中的一些复选框(父级)来更改用户控件中可见的内容。我试过直接在父表单和孩子一侧的函数中更改它。从调试中,属性正在被更改,但是在调用之后,属性似乎重置为默认值。

我在父方的代码

public partial class Program: DevExpress.XtraBars.Ribbon.RibbonForm
{
    public Program()
    {
        InitializeComponent();
    }
    public class GlobalVariables
    {
        public static string categoryID = "1";
        public static string currentPage = "Campus";
    }

    private void ribbonControl1_MouseDown(object sender, MouseEventArgs e)
    {
        RibbonControl ribbon = sender as RibbonControl;
        RibbonHitInfo hitInfo = ribbon.CalcHitInfo(e.Location);
        if (hitInfo.HitTest == RibbonHitTest.PageHeader)
        {
            System.Diagnostics.Debug.WriteLine(hitInfo.Page.Name + "");
            pnlPanel.Panel2.Controls.Clear();
            if(hitInfo.Page.Name == "Campus")
            {
                var Campus = new WindowsFormsApplication1.Campus();
                pnlPanel.Panel2.Controls.Add(Campus);
                GlobalVariables.currentPage = "Campus";
            }
            else if (hitInfo.Page.Name == "Report")
            {
                var Report = new WindowsFormsApplication1.Report();
                pnlPanel.Panel2.Controls.Add(Report);
                GlobalVariables.currentPage = "Report";
            }
            else if (hitInfo.Page.Name == "Admin")
            {
                var Admin = new WindowsFormsApplication1.Admin();
                pnlPanel.Panel2.Controls.Add(Admin);
                GlobalVariables.currentPage = "Admin";
            }

        }
    }

    private void Program_Load(object sender, EventArgs e)
    {
        var Campus = new WindowsFormsApplication1.Campus();
        pnlPanel.Panel2.Controls.Add(Campus);

        List<string> Category = GenerateCategory();

        for(int j = 0; j<= Category.Count(); j++)
        {
            krpList.Items.Add(Category[j + 1]);
            krpValue.Items.Add(Category[j]);
            j++;
        }
    }

public void krpList_SelectedIndexChanged(object sender, EventArgs e)
        {
            int selectedIndex = krpList.SelectedIndex;
            krpValue.SelectedIndex = selectedIndex;
            GlobalVariables.categoryID = krpValue.SelectedItem + "";
            System.Diagnostics.Debug.WriteLine(krpValue.SelectedItem);
            Reset();  
        }

        public void Reset()
        {
            var Campus = new WindowsFormsApplication1.Campus();
            pnlPanel.Panel2.Controls.Clear();
            pnlPanel.Panel2.Controls.Add(Campus);
        }

        private void chkDisplay_EditValueChanged(object sender, EventArgs e)
        {
            string displayInfo = "";
            var Campus = new WindowsFormsApplication1.Campus();
            displayInfo = chkDisplay.Text;
            Campus.UpdateAudit(displayInfo);
            Campus.treeList1.ClearNodes();
        }


    }
}

用户控制代码

public void UpdateAudit(string displayInfo) //Child Method
{
    treeList1.ClearNodes(); //Adjusting property on user control element
    treeList1.Columns[1].VisibleIndex = 1;
    treeList1.Columns[2].VisibleIndex = 2;
    //...more code similar to above 
}

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:1)

嗯,这是问题所在,您正在使用相同表单的多个实例。 你应该创建一个单独的实例并使用它。

public partial class Program: DevExpress.XtraBars.Ribbon.RibbonForm {

UserControl Campus = null;

 public Program()
    {
        InitializeComponent();
         Campus  = new WindowsFormsApplication1.Campus();
    }

 private void Program_Load(object sender, EventArgs e) {
  // remove this object creation
 //var Campus = new WindowsFormsApplication1.Campus();

        pnlPanel.Panel2.Controls.Add(Campus);
   ... 
}

 public void Reset() {
  // remove this object creation
 //  var Campus = new WindowsFormsApplication1.Campus();
}


private void chkDisplay_EditValueChanged(object sender, EventArgs e) {

 string displayInfo = "";
  // remove this object creation
 // var Campus = new WindowsFormsApplication1.Campus();
}

private void ribbonControl1_MouseDown(object sender, MouseEventArgs e)
    {
     ...
   if(hitInfo.Page.Name == "Campus")
            {
                // remove this object creation
                //var Campus = new WindowsFormsApplication1.Campus();

    }
    ...
}
}