I just want to ask if this is good way of showing/hiding the user controls on the main form.
I have form1 with 3 buttons (button1, button2,button3) and I have user controls (user control1, user control2,user control3 and they contain nothing).
now click button1 and user control1 shows up, and click button2 and user control2 shows up and user control1 hides.... and so on ( so ever time you click a button a user controll shows up and hides the rest.
I used the following code and it worked perfectly as i wanted but my question is:
the UserControl.BringToFront() function brings the user control to the front and every time you click a button it brings that usercontrol to front, so what happens to other user controls? I mean the BringToFront() kind of places each user control on top of another and does not remove any previous user controls. I feel like something missing, something like "Remove" function to remove the previous UserControl. And what happens if I leave my code like this (without "Remove" function? Please help. Thank you.
Here is the code and it works very well:
user control1 name is UC1
user control2 name is UC2
user control2 name is UC3
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
UC1 uc1 = new UC1();
Controls.Add(uc1);
uc1.BringToFront();
}
private void button2_Click(object sender, EventArgs e)
{
UC2 uc2 = new UC2();
Controls.Add(uc2);
uc2.BringToFront();
}
private void button3_Click(object sender, EventArgs e)
{
UC3 uc3 = new UC3();
Controls.Add(uc3);
uc3.BringToFront();
}
}
Do not recreate the controls each time you press the buttons, just show/hide the controls:
private UC1 uc1 = new UC1() {
Visible = false
};
private UC2 uc2 = new UC2() {
Visible = false
};
private UC3 uc3 = new UC3() {
Visible = false
};
private void VisualizeUC(Control value) {
uc1.Visible = false;
uc2.Visible = false;
uc3.Visible = false;
value.Visible = true;
}
private void Form1_Load(object sender, EventArgs e) {
Controls.Add(uc1);
Controls.Add(uc2);
Controls.Add(uc3);
}
private void button1_Click(object sender, EventArgs e) {
VisualizeUC(uc1);
}
private void button2_Click(object sender, EventArgs e) {
VisualizeUC(uc2);
}
private void button3_Click(object sender, EventArgs e) {
VisualizeUC(uc3);
}
You were previously creating a new instance of each control every time a button was pressed which quickly adds up to lots of unnecessary controls.
Instead create and add only one instance of each control, and then hide/show as necessary:
// Put your controls here so they're accessible
UC1 uc1;
UC2 uc2;
UC3 uc3;
private void Form1_Load(object sender, EventArgs e)
{
// Do this on form load so it only happens once
// Instantiate your controls
uc1 = new UC1();
uc2 = new UC2();
uc3 = new UC3();
// Make them invisible
uc1.Visible = false;
uc2.Visible = false;
uc3.Visible = false;
// Add your controls
Controls.Add(uc1);
Controls.Add(uc2);
Controls.Add(uc3);
}
private void button1_Click(object sender, EventArgs e)
{
// You can keep using bring to front
uc1.BringToFront();
// OR
// Use show/hide
uc1.Show();
uc2.Hide();
uc3.Hide();
}
private void button2_Click(object sender, EventArgs e)
{
uc2.BringToFront();
// OR show hide...
}
private void button3_Click(object sender, EventArgs e)
{
uc3.BringToFront();
}
Related
I attached a TextBox to the first TabPage of a TabControl. I would like to display the same TextBox object on every TabPage. I tried to add the control to the tabControl Collection but unfortunately it's not working.
private void Form1_Load(object sender, EventArgs e)
{
tabControl1.TabPages[tabControl1.SelectedIndex].Controls.Add(textBox);
}
Button b;
public Form1()
{
InitializeComponent();
b = new Button() { Text = "Prueba" };
}
private void Form1_Load(object sender, EventArgs e)
{
AddButtonToTabControl();
}
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
AddButtonToTabControl();
}
public void AddButtonToTabControl()
{
tabControl1.SelectedTab.Controls.Add(b);
}
I missed two methods. It's working now!
tabControl1.Selecting += new TabControlCancelEventHandler(tabControl1_Selecting);
void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
{
tabControl1.TabPages[tabControl1.SelectedIndex].Controls.Add(textBox);
}
I have these codes in my Mainform that when I click on a picturebox, a user control then loads in the a panel.
My problem is the datagridviews inside other controls doesn't get updated. When I make transactions in other user controls that affects other user controls.
What I want now is to automatically update those gridviews by refreshing the user control when their associated button is clicked.
private void pictureBox5_Click(object sender, EventArgs e)
{
panel2.Controls.Add(UCCustomer.Instance);
UCCustomer.Instance.Dock = DockStyle.Fill;
UCCustomer.Instance.BringToFront();
lblMenu.Text = "Customer Management";
}
private void pictureBox2_Click(object sender, EventArgs e)
{
panel2.Controls.Add(UCDelivery.Instance);
UCDelivery.Instance.Dock = DockStyle.Fill;
UCDelivery.Instance.BringToFront();
lblMenu.Text = "Delivery Management";
}
private void pictureBox3_Click(object sender, EventArgs e)
{
panel2.Controls.Add(UCReport.Instance);
UCReport.Instance.Dock = DockStyle.Fill;
UCReport.Instance.BringToFront();
lblMenu.Text = "Reports";
}
Here's an example on how I declared the instance for a user control. This is in the same format as other user controls.
private static UCCustomer _instance;
public static UCCustomer Instance
{
get
{
if (_instance == null)
_instance = new UCCustomer();
return _instance;
}
}
I am having two forms (A and B). In form B there are many buttons having different background image. On clicking any of the button I want to change the background image of form A to the background image of the button which was clicked instantly as it is always open behind the form.
formA mai = new formA();
private void button1_Click(object sender, EventArgs e)
{
mai.BackgroundImage = button1.BackgroundImage;
}
This is the code I am using although it changes the background image it doesn't change instantly but if I will open and close the form the background image will be changed.
I don't need like that I need it to change instantly.
Add a field in formB to refer to the formA instance which you want to change its BackgroundImage; and initialize it when you call formB
formB's code-behind:
public partial class formB : Form
{
public formA owner;
public formB()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (owner != null)
owner.BackgroundImage = button1.BackgroundImage;
}
private void button2_Click(object sender, EventArgs e)
{
if (owner != null)
owner.BackgroundImage = button2.BackgroundImage;
}
private void button3_Click(object sender, EventArgs e)
{
if (owner != null)
owner.BackgroundImage = button3.BackgroundImage;
}
}
formA's code-behind:
public partial class formA : Form
{
public formA()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
formB b = new formB();
b.owner = this;
b.ShowDialog();
}
}
Add this.Refresh()
formA mai = new formA();
private void button1_Click(object sender, EventArgs e)
{
mai.BackgroundImage = button1.BackgroundImage;
mai.BringToFront();
mai.Refresh();
}
Call mai.Invalidate() after setting the new image.
I am trying to access a on my MainForm from another Form. This control is a FlowLayoutPanel, and I have set its Access Modifier to Public. I don't know why I can't access it from another form, because this method has always worked for me in the past.
MainForm.cs:
void button1_Click(object sender, EventArgs e)
{
using(var editor = new Editor())
{
editor.ShowDialog();
}
}
Editor.cs:
void button1_Click(object sender, EventArgs e)
{
int count = MainForm.flow.Count;
}
Why can I not access this control from another form - even though its modifier is set to public?
you are accessing the control/property wrong.
You should do it like this.
MainForm.cs
private void button1_Click(object sender, EventArgs e)
{
var frm = new Editor();
frm.ShowDialog(this);
}
Editor.cs
private void button1_Click(object sender, EventArgs e)
{
var f = (this.Owner as MainForm);
int count = f.flow.Count;
MessageBox.Show(count.ToString());
}
Firstly I had been searching about my problem and can't find \help.
So my question is I've got 3 buttons and three userControl and when I click on one button it displays usercontrol 1 but after I click button 2. I cannot get back to usercontrol 1 im stuck in usercontrol2 and the button 1 does not do anything anymore.
Here's my code:
public partial class Form2 : Form
{
UserControl1 u1;
UserControl2 u2;
UserControl3 u3;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
u1 = new UserControl1();
u1.Dock = DockStyle.Fill;
this.Controls.Add(u1);
}
private void button2_Click(object sender, EventArgs e)
{
u1.Hide();
u2 = new UserControl2();
u2.Dock = DockStyle.Fill;
this.Controls.Add(u2);
}
private void button3_Click(object sender, EventArgs e)
{
u1.Hide();
u2.Hide();
u3 = new UserControl3();
u3.Dock = DockStyle.Fill;
this.Controls.Add(u3);
}
private void button4_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
SOLVED CODE for other's who need :)
--->
enter code here
public partial class Form2 : Form
{
UserControl1 u1;
UserControl2 u2;
UserControl3 u3;
public Form2()
{
u1 = new UserControl1();
u2 = new UserControl2();
u3 = new UserControl3();
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
u2.Hide();
u3.Hide();
u1.Show();
u1.Dock = DockStyle.Fill;
this.Controls.Add(u1);
}
private void button2_Click(object sender, EventArgs e)
{
u1.Hide();
u3.Hide();
u2.Show();
u2.Dock = DockStyle.Fill;
this.Controls.Add(u2);
}
private void button3_Click(object sender, EventArgs e)
{
u1.Hide();
u2.Hide();
u3.Show();
u3.Dock = DockStyle.Fill;
this.Controls.Add(u3);
}
private void button4_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
It seems that you should have:
u1=new UserControl1();
u2=new UserControl2();
u3=new UserControl3();
in the constructor public Form2() rather than in the event handlers. This will allow you to add
u2.Hide();
u3.Hide();
in your button1_Click handler.
You should probably also add u3.Hide() to button2_Click.
Take a look at this. I think it's absolutely clear and didn't need any further explanation.
public partial class Form1 : Form
{
private UserControl1 uc1 = new UserControl1();
private UserControl2 uc2 = new UserControl2();
private UserControl3 uc3 = new UserControl3();
public Form1()
{
InitializeComponent();
AssignedButtonClickEvents();
}
private void Form1_Load(object sender, EventArgs e)
{
}
protected void ButtonClicked(object sender, EventArgs e)
{
Button button = sender as Button;
panel1.Controls.Clear();
if (button != null)
{
switch (button.Name)
{
case "button1":
uc1.Dock = DockStyle.Fill;
panel1.Controls.Add(uc1);
break;
case "button2":
uc2.Dock = DockStyle.Fill;
panel1.Controls.Add(uc2);
break;
case "button3":
uc3.Dock = DockStyle.Fill;
panel1.Controls.Add(uc3);
break;
default:
panel1.Controls.Clear();
break;
}
}
}
public void AssignedButtonClickEvents()
{
foreach (Control ctl in this.Controls)
{
if (ctl is Button)
{
Button button = (Button)ctl;
button.Click += new EventHandler(ButtonClicked);
}
}
}
edit
note that i create a panel to store the usercontrols, but i think it's the same if you show your user controls directly on the windows forms. you only need to hide your controls.
If you use BringToFront() property when clicking the button, the functionality would happen. This method will be effective for response button clicks other than Hide() property.
private void buttonProdutsList_Click(object sender, EventArgs e)
{
productsListView.BringToFront();
}