简体   繁体   中英

Bring the front user control with a button which is inside in another user control

I have a Form and a at least 6 user controls (every UC got a button to go to next UC)

  1. I want to bring UC1 to the front first
  2. UC1 got a button which is pops up UC2
  3. etc...

I want to know the schema of how to go from one UC to another.

Form code:

    public partial class TestProgram : Form
{
    public TestProgram()
    {
        InitializeComponent();

        UC21.BringToFront();
    }

    private void TestProgram_Load(object sender, EventArgs e)
    {
        UC21.Hide();
        UC31.Hide();
        UC41.Hide();
        UC51.Hide();
        UC61.Hide();
        UC71.Hide();
        UC81.Hide();
    }
}

UC1 code:

public partial class UC1 : UserControl
{
    public UC2 UC2{ get; set; } 
    
    public UC1()
    {
        InitializeComponent();

    }

    private void NextPageBut_Click(object sender, EventArgs e)
    {
        UC2.BringToFront();
        
    }
}

I think it is easier if you leave it to your main form to decide what is Next instead of in each UserControl. That makes moving them around or add a new one a bit less of a hassle. I'm abusing the mainform as a Controller/Presenter here.

The UserControls get added to the Controls collection of the form. If we don't use BringToFront but simply Hide and Show we can have the mainform find the current showing usercontrol (as it has its Visible property set to true) and then hide that one and Show the next usercontrol. The UserControl has Parent property which we can cast to the mainform so we can invoke methods on it public interface.

UserControl

This the code that goes in the click event of the Next button:

public partial class UC1 : UserControl
{
     public UC1()
    {
        InitializeComponent();
    }

    private void NextPageBut_Click(object sender, EventArgs e)
    {
        // get and cast to the parent form
        var tpf = (TestProgram) this.Parent;
        // tell the mainform we want to go Next
        tpf.Next();
    }
}

Mainform Next method

This methods loops over the Controls while keeping state to decide which action to take.

// our various states
enum NextState {
    Start = 0,
    ShowOne,
    Done,
}

public void Next() 
{
    NextState state = NextState.Start;
    // loop over controls
    // if you want to 
    foreach(var ctl in Controls)
    {
        // find the UserControl ones
        if (ctl is UserControl) 
        {
            var uc = (UserControl) ctl;
            // based on our state ...
            switch(state)
            {
                case NextState.Start:
                    // hide and do the next state
                    if (uc.Visible) 
                    {
                        uc.Hide();
                        state = NextState.ShowOne;
                    }
                break;
                case NextState.ShowOne:
                    // show and do the next state
                    uc.Show();
                    state = NextState.Done;
                break;
                default:
                    // do nothing
                break;
            }
        }
    }
    if (state != NextState.Done) {
        // nothing got set
        // show the first one? 
        // something else?
    }
}

If you really want to use BringToFront instead then you have to be a bit more careful and probably do a SendToBack to maintain order in the Controls collection. Or use a second collection with your usercontrols all together and adapt the Next method accordingly.

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