通过单击其他用户控件中的按钮来显示用户控件。 [英] Show user control by clicking a button from another user control.

查看:87
本文介绍了通过单击其他用户控件中的按钮来显示用户控件。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。



我试图通过点击另一个用户控件的按钮来加载用户控件。基本上,我有1个面板。在这个面板中,我可以点击Order,它将带我到订单用户控件。现在,在这个用户控件中我创建了一个按钮,它应该清除面板的内容,并用一个名为checkout的新用户控件替换它。



我是什么尝试过:



我试过:

Hello.

I was trying to load a user control by clicking a button from another user control. Basically, I have 1 panel. From this panel I can click Order which will take me to a order user control. Now, in this user control I created a button which should clear the content of the panel and replace it with a new user control called checkout.

What I have tried:

I have tried:

private void btnCheckout_Click(object sender, EventArgs e)
              {
              Menu panel = new Menu(); //called the Menu form to allow me to call the panel from that form.

              panel.pnlContent.Controls.Clear(); //I wanted to clear the content of a panel

              panel.pnlContent.Controls.Add(new Checkout()); //add a new user control to the panel
              }





当我点击按钮时没有任何反应。它没有清除内容,也没有加载checkout用户控件。



Whenver I am clicking the button nothing happens. It do not clear the content and it do not load the checkout user control.

推荐答案

如果调试代码,问题应该是非常明显的:< br $>


您创建菜单的新实例,更改其内容,然后扔掉它



您需要显示新的菜单实例,或以某种方式获取现有实例。



我们无法告诉你如何做到这一点,因为我们看不到创建现有实例的代码,你还没有告诉我们什么菜单类是。



例如,如果菜单是一个表单,并且你要显示新实例,你需要调用 panel.Show(); panel.ShowDialog(); 显示它。
If you debug your code, it should be perfectly obvious what the problem is:

You create a new instance of Menu, change its contents, and then throw it away.

You either need to display the new Menu instance, or get hold of an existing instance somehow.

We can't tell you how to do that, because we can't see the code that creates the existing instance, and you haven't told us what the Menu class is.

For example, if Menu is a Form, and you want to show the new instance, you need to call panel.Show(); or panel.ShowDialog(); to show it.


这篇关于通过单击其他用户控件中的按钮来显示用户控件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆