Home > Back-end >  c# I cannot figure out how to move User Control within panels
c# I cannot figure out how to move User Control within panels

Time:05-06

I have 6 panels on my screen. I have 1 user control with a button.

I am having two issues.

When I load up my user control(only loads when called) into panel 1, the user control loads at the top. I cannot figure out how when it opens, how to have the user control drop to the bottom of the panel.

Second issue - the bigger problem

Each time I click the button on the user control, I want the user control to move to the next panel. I cannot figure that out because the button is not attached to form 1. Right now, the only coding I have is the Button changed text when clicked.

// This is FORM1

public partial class MainForm : Form
{
public MainForm()
{
    
InitializeComponent();       
}
    
private void btnOpen_Click(object sender, EventArgs e)
{
Panel1.Controls.Clear();
Control1 frm = new Control1();
Panel1.Controls.Add(frm);
}
}

// The is the User Control

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

public void btnStatus_Click(object sender, EventArgs e)
{ 
if (btnStatus.Text == "Panel1")
{
btnStatus.Text = "Panel2";
}
else if (btnStatus.Text == "Panel2")
{
btnStatus.Text = "Panel3";  
}
else if (btnStatus.Text == "Panel3")
{
btnStatus.Text = "Panel4";
}
else if (btnStatus.Text == "Panel4")
{
btnStatus.Text = "Panel5";
}
else if (btnStatus.Text == "Panel5")
{
btnStatus.Text = "Panel6";
{
}

CodePudding user response:

For your first problem, you need to set Left and Top properties of your control. If you want center in the panel, you can use Width and Height of panel to set Left/Top of your control.

In the second problem, you only change the text of your control. To move the control to other Panel you need set the Parent property of the control to the Panel, or add the control to yourPanel.Controls collection. Your control hasn't know the panels, so I think it's better do the movement outside your control. But you need a way to inform outside about the button click. So, add an event to your control:

public event EventHandler ButtonClick;

Fire the event in the button click:

public void btnStatus_Click(object sender, EventArgs e)
{ 
    this.ButtonClick?.Invoke(this, EventArgs.Empty);
}

And a way to set the button Text:

public string ButtonText
{
   get { return button1.Text; }
   set { button1.Text = value; }
}

In the form, add an array with all Panels:

private readonly Panel[] _panels;

In the contructor:

frm.ButtonClick  = this.OnControl_ButtonClick;
_panels = new Panel[] { panel1, panel2, panel3, panel4, panel5, panel6 };

And manage your control button click:

private void OnControl_ButtonClick(object sender, EventArgs e)
{
    var control = (Control1)sender;

    for (int i = 0; i < this._panels.Length; i  )
    {
        if (control.Parent == this._panels[i])
        {
            var nextPanelIndex = i == this._panels.Length - 1 ? 0 : i   1;
            var nextPanel = this._panels[nextPanelIndex];
            control.Parent = nextPanel;
            control.ButtonText = nextPanel.Name;
        }
    }
}

You search the parent of your control (which is a Panel of the list) and when you find it, you select next panel (or first when you are in the last panel).

CodePudding user response:

I cannot figure out how when it opens, how to have the user control drop to the bottom of the panel.

Set up a location of control:

Control1 frm = new Control1();
frm.Location = new Point(x,y);

OR also you can use the dock property, also set the control size.

frm.Dock = DockStyle.Botton;

What is btnStatus? You can set the text of your control as a property or in the constructor. And I think that you can try something like to send a List<Panel> to your control constructor and change the property Parent to this panel every time you click the button.

  •  Tags:  
  • c#
  • Related