简体   繁体   中英

How to hide User control from another user control?

I am working in an school project in C# but I have a problem. I made a user control in which i have some buttons, this is like a banner with buttons in it. Those buttons should SHOW and HIDE other User Controls

Here is my code in the User Control that has the button:

 Reports ra = new Reports();
 PurchaseReport rb = new PurchaseReport();

than in one of the button i wrote this code:

 ra.Hide();

 rb.Show();
 this.Controls.Add(rb);
 rb.Location = new Point(130, 153);

But the problem is that it doesn't hide the Reports (ra). And it doesn't show the Purchase Report (rb). What is the problem?

public partial class MenuUserC : UserControl
{

    Reports ra = new Reports();
    PurchaseReport rb = new PurchaseReport();

    public MenuUserC()
    {
        InitializeComponent();
    }

    private void ButtonItem15_Click(object sender, EventArgs e)
    {
        rb.Hide();

        ra.Show();
        this.Controls.Add(ra);
        ra.Location = new Point(130, 153);
    }

    private void ButtonItem1_Click(object sender, EventArgs e)
    {

        ra.Hide();

        rb.Show();
        this.Controls.Add(rb);
        rb.Location = new Point(130, 153);

    }

Generally, you do NOT want one Control to know about the other. Make your custom Controls expose events when something interesting happens and let the application code handle the logic to show/hide other controls.

For example:

MyUserControl ctl = new MyUserControl;
ctl.OnHideReports += OnHideReports;
ctl.OnShowReports += OnShowReports;
...

Then in the event handler for OnHideReports, handle the logic:

void OnHideReports(...)
{
    _reports.Hide();
    _purchaseReports.Show();
    ...
}

Also, try to add your user controls to the Form using the WinForm designer. It is much easier to work with Controls from the designer versus declaring them directly in the code.

Update:

This example has two buttons on a form plus two user Controls. When Button 1 is clicked, it shows User Control 1 and hides User Control 2. When Button 2 is clicked, it does the reverse. Note that the buttons and user Controls are added to the form using the Forms designer, not manually in code. This will take care of positioning, adding them as child controls to the main form etc. Do not manually add them with your own code unless you have a good reason to do so.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void UiButtonOneClick(object sender, EventArgs e)
    {
        myUserControlOne.Show();
        myUserControlTwo.Hide();
    }

    private void UiButtonTwoClick(object sender, EventArgs e)
    {
        myUserControlOne.Hide();
        myUserControlTwo.Show();
    }
}

尝试对其他对象使用hide方法和show方法

you are creating a new instance of the user controls

instead you need something like this

Reports ra = (Reports)Page.FindControl("Reports1");//Reports1 should be the name the controls renders in the browser

that will give you access to the instance on the page my syntax might be off a little, last time i did this was in VB

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM