'c# I cannot figure out how to move User Control within panels
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";
{
}
Solution 1:[1]
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.
Solution 2:[2]
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).
UPDATE
public partial class Control1 : UserControl
{
public Control1()
{
InitializeComponent();
}
// NOTE: button1 is the name of your button in the control. Change if you are using another name
// This property allow get/set the text of the button from a Control1 instance
public string ButtonText
{
get { return button1.Text; }
set { button1.Text = value; }
}
public event EventHandler ButtonClick;
public void btnStatus_Click(object sender, EventArgs e)
{
// When you click in the button, you do nothing with panels. Panels are managed from
// Form and your control doesn't know the form. Here, you simply fire the event.
// Your form will attach the event to manage the Panels
this.ButtonClick?.Invoke(this, EventArgs.Empty);
}
}
public partial class MainForm : Form
{
// We can use an array to save into it all your Panels. This allow us works with all
// panels using a for instead multiple if/else on each panel
private readonly Panel[] _panels;
public MainForm()
{
InitializeComponent();
// You have a variable frm of your Control1 class. Here, the form attach the button
// click of your control. When you click in the control, OnControl_ButtonClick of
// the form will be executed
frm.ButtonClick += this.OnControl_ButtonClick;
// Put here all your panels
_panels = new Panel[] { Panel1, Panel2, Panel3, Panel4, Panel5, Panel6 };
// Maybe you want start with your control in the first panel
Control1 frm = new Control1();
Panel1.Controls.Add(frm);
}
private void OnControl_ButtonClick(object sender, EventArgs e)
{
// sender is "this" in the ButtonClick?.Invoke of your control. We can do a cast
var control = (Control1)sender;
// We iterate all panels searching the one that contains the control
for (int i = 0; i < this._panels.Length; i++)
{
// When we find the control...
if (control.Parent == this._panels[i])
{
// We get the next panel (or first one if Control1 is in the last panel)
var nextPanelIndex = i == this._panels.Length - 1 ? 0 : i + 1;
var nextPanel = this._panels[nextPanelIndex];
// Setting the parent we move the control to the new panel
control.Parent = nextPanel;
// And set the button text
control.ButtonText = nextPanel.Name;
}
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Dúver Cruz |
Solution 2 |