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.