如何仅使用用户控件时如何在主窗体面板中显示用户控件?

时间:2013-04-25 00:34:12

标签: c# winforms

@Mark,是的我先生在usercontrol中创建了一个事件。 这是我的代码:

用户控件

namespace Purchase_Order
{
    public partial class Static : UserControl
    {
        public event EventHandler ClassificationClicked;
        public Static()
        {
            InitializeComponent();
        }

        private void btnClassification_Click(object sender, EventArgs e)
        {
            ClassificationClicked(sender, e);
        }
    }
}

的MainForm

public partial class formMain : Form
{

    public formMain()
    {
        InitializeComponent();
        Static.ClassificationClicked += new EventHandler(Static_ClassificationClicked); 
    }

    private void formMain_Load(object sender, EventArgs e)
    {
        Static control = new Static();
        panelSide.Controls.Clear();
        panelSide.Controls.Add(control);   
    }

    void  Static_ClassificationClicked(object sender, EventArgs e)
    {
        classification control = new classification();
        panelMain.Controls.Clear();
        panelMain.Controls.Add(control);
    }

}

这是错误 - > Static.ClassificationClicked + = new EventHandler(Static_ClassificationClicked);

3 个答案:

答案 0 :(得分:1)

我假设btnClassification_Clickstatic用户控件上的一种方法,该方法位于formMain的实例上。

您的问题是,在btnClassification_Click方法中,您创建了formMain类的新实例,而不是访问您的static用户控件所在的实例。看起来您已经在mainPanel类中公开了formMain,因此您只需要找到formMain类的正确实例来添加您的用户控件。为此,请将此方法放在static用户控件中:

private Form GetParentForm()
{
    Control current = this.Parent;
    while (current != null)
    {
        Form form = current as Form;
        if (form != null)
        {
            return form;
        }

        current = current.Parent;
    }

    return null;
}

然后从事件处理程序中调用此方法:

private void btnClassification_Click(object sender, EventArgs e)
{
    classification control = new classification();
    formMain main = (formMain)this.GetParentForm();
    main.panelMain.Controls.Clear();
    main.panelMain.Controls.Add(control);
}

答案 1 :(得分:1)

我个人会在你的UserControl上创建一个Event并在你的MainForm中订阅它。在我看来,UserControl知道父表格是不恰当的。像这样:

将事件添加到您的UserControl:

public partial class UserControl1 : UserControl
{
    public event EventHandler ClassificationClicked;
    public UserControl1()
    {
        InitializeComponent();
    }

    private void btnClassification_Click(object sender, EventArgs e)
    {
        ClassificationClicked(sender, e);
    }
}

在MainForm中订阅

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        userControl11.ClassificationClicked += new EventHandler(userControl11_ClassificationClicked);
    }

    void userControl11_ClassificationClicked(object sender, EventArgs e)
    {
        classification control = new classification();
        panelMain.Controls.Clear();
        panelMain.Controls.Add(control);

    }
}

根据评论流进行编辑。我还要确保您的Usercontrols具有唯一的名称,而不仅仅是控制,这样您就可以区分它们。

public partial class formMain : Form
{
    Static staticControl;
    Classification classificationControl;

    public formMain()
    {
        InitializeComponent();
        staticControl= new Static();
        panelSide.Controls.Clear();
        panelSide.Controls.Add(staticControl);
        staticControl.ClassificationClicked += new EventHandler(Static_ClassificationClicked); 
    }

    void  Static_ClassificationClicked(object sender, EventArgs e)
    {
        classificationControl = new classification();
        panelMain.Controls.Clear();
        panelMain.Controls.Add(classificationControl);
    }

}

答案 2 :(得分:0)

Mark Hall's答案是最好的,但是我想补充一点,你不应该直接提出事件,而是首先检查是否有订阅者然后举起事件。如果还没有人订阅该事件,这将停止空引用异常。

public partial class UserControl1 : UserControl
{
    public event EventHandler ClassificationClicked;
    public UserControl1()
    {
        InitializeComponent();
    }

    private void btnClassification_Click(object sender, EventArgs e)
    {
        RaiseClassifactionClicked();
    }

    private RaiseClassifactionClicked()
    {
        var tmp = ClassificationClicked; //This is added for thread safty
        if(tmp != null) //check to see if there are subscribers, if there are tmp will not be null
           tmp(this, EventArgs.Empty); //You don't need to pass along the sender and e, make your own here.
    }
}

有关线程安全技巧的更多信息,请阅读this post