Clear panel from usercontrol C# - c#

I have a panel in a form and 2 UserControl, When the form loads, in the load () method, loads the first usercontrol using this code:
private void OlvidadaContraseña_Load(object sender, EventArgs e)
{
panel1.Controls.Clear();
UserControl1 Env = new UserControl1();
panel1.Controls.Add(Env);
}
How can I clean the panel (form panel) and load the second usercontrol from the first usercontrol?(Access form control from the usercontrol?)
Thanx

if you want change clean panel and load other usercontrol with first usercontrol you must use delegate-event.
you mast add on control (button,..) in first usercontrol
in code behind first userControl :
public partial class EnvioContraseña: UserControl
{
public delegate void LoadOtherUserControl(EnvioContraseña sender);
public event LoadOtherUserControl On_SelectButton;
public EnvioContraseña()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (On_SelectButton != null)
On_SelectButton(this);
}
}
then in code behind mainform
private void Form1_Load(object sender, EventArgs e)
{
panel1.Controls.Clear();
EnvioContraseña Env = new EnvioContraseña ();
Env.On_SelectButton += Env_On_SelectButton;
panel1.Controls.Add(Env);
}
void Env_On_SelectButton(EnvioContraseña sender)
{
panel1.Controls.Clear();
UserControl1 uc1 = new UserControl1();
panel1.Controls.Add(uc1);
}

Related

how to use textbox content from a form to a usercontrol?

i have a form with a panel in it and when a button is presed a usercontrol showes and the panel hides, now i need to hide a button if the textbox in the form1 contains "admin" in it.
this is the form1 code
public partial class Form1 : Form
{
public string a;
public Form1()
{
InitializeComponent();
a = textBox1.Text;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
panel1.Controls.Clear();
afterlogin v = new afterlogin();
v.Dock = DockStyle.Fill;
panel1.Controls.Add(v);
v.BringToFront();
}
}
and this is the usercontrol code
public partial class afterlogin : UserControl
{
public afterlogin()
{
InitializeComponent();
Form1 f = new Form1();
if (f.a.Contains("admin"))
{
button1.Hide();
}
}
}
You're creating a new form in the user control, it will not have the same values as the original form you created your user control in.
If you wish to take in values from the form, add a constructor parameter to the "afterlogin" class with text of the textbox, such as:
public afterlogin(string text)
{
InitializeComponent();
if (text.Contains("admin"))
{
button1.Hide();
}
}
and pass the text value to the constructor of the "afterLogin" class:
afterlogin v = new afterlogin(a);
Since Form1 creates the UserControl, just have the Form itself turn on or off the Button?
You can make a method in your UserControl that allows you to change the visibility of the control:
public partial class afterlogin : UserControl
{
public void setButton(bool state)
{
button1.Visible = state;
}
}
Now you can call setButton when you create the UserControl:
private void button1_Click(object sender, EventArgs e)
{
panel1.Controls.Clear();
afterlogin v = new afterlogin();
v.Dock = DockStyle.Fill;
panel1.Controls.Add(v);
v.BringToFront();
v.setButton(!textBox1.Text.Contains("admin"));
}

Why is my Event Null?

I am using windows forms C#.
I have main Form and child form. The event is in the child Form and I subscribed to this event in the main Form. The problem is that when I click button1 in child form it does nothing (it should fire the event so textBox1 prints the text) because the event is still null although the main form has already subscribed to the event. Am I doing something wrong? Please help how can I fire the event once button1 is clicked.
Child Form:
public partial class ChildForm : Form
{
public event EventHandler MyEvent;
private void button1_Click(object sender, EventArgs e)
{
if (myEvent != null)
{
MyEvent(this, e);
}
}
Main Form:
public partial class MainForm : Form
{
ChildForm ChildFrm= new ChildForm ();
ChildFrm.MyEvent += new EventHandler(HandleTheEvent);
private void button1_Click(object sender, EventArgs e)
{
ChildForm childfrm = new ChildForm ();
childfrm.ShowDialog()
}
public void HandleTheEvent(object sender, EventArgs e)
{
textBox1.Text = "event is fired";
}
You are adding the event handler to an another instance of ChildForm. Change MainForm's button1_click to look like this:
private void button1_Click(object sender, EventArgs e)
{
ChildFrm.ShowDialog()
}
And your application should work OK.
Here's the working MainForm.cs:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
ChildFrm.MyEvent += new EventHandler(HandleTheEvent);
}
ChildForm ChildFrm = new ChildForm();
private void button1_Click(object sender, EventArgs e)
{
ChildFrm.ShowDialog();
}
public void HandleTheEvent(object sender, EventArgs e)
{
textBox1.Text = "event is fired";
}
}
Subscribe to the event right after creating the form :)
private void button1_Click(object sender, EventArgs e)
{
ChildForm childfrm = new ChildForm();
childfrm.MyEvent += new EventHandler(HandleTheEvent);
childfrm.ShowDialog()
}

Change background image of form as soon as button on another form is clicked

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.

Is this the right way to show/hide user controls?

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();
}

accessing form controls from the class file

I have one mdi form and many child forms all the child form contains panel control named as panel1 now i want to access panel1 control from my class file for that i write the following code but it is not working.
Class1.cs file :
public partial class FormBase : Form
{
public FormBase()
{
this.Load += new System.EventHandler(this.FormLoad);
//this.Resize += new System.EventHandler(this.FormResize);//error
this.panel1.Resize += new System.EventHandler(this.FormResize);//error as a panel1 is not access
}
protected virtual void FormLoad(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Maximized;
}
protected virtual void FormResize(object sender, EventArgs e)
{
//error : panel1 control has a red underline
panel1.Left = (this.ClientSize.Width - panel1.Width) / 2;//error as a panel1 is not access
panel1.Top = (this.ClientSize.Height - panel1.Height) / 2;//error as a panel1 is not access
}
}
now my one of the child form
frmAddNewEmployee.cs
public partial class frmAddNewEmployee : FormBase
{
public frmAddNewEmployee()
{ InitializeComponent();
}
protected override void FormLoad(object sender, EventArgs e)
{
base.FormLoad(sender, e);
}
protected override void FormResize(object sender, EventArgs e)
{
base.FormResize(((Panel)sender).Name, e);//error
base.FormLoad(sender, e);//error
}
}
so my question is how to access this panel1 control of the child forms from my Class1.cs file

Categories

Resources