How to open usercontrol in a click of a button C# - c#

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

Related

Changing button's visibility after opening a new form

I have 2 forms. Form1 and Form5. Form1 has 2 buttons which open Form5. I want to change the visibility of Form5's button after it is shown. I have set Form5 button's modifiers to public and I've also tried this code below but it's not working:
public void button1_Click(object sender, EventArgs e)
{
Form5 fr5 = new Form5();
fr5.button1.Visible = true;
fr5.ShowDialog();
}
public void button2_Click(object sender, EventArgs e)
{
Form5 fr5 = new Form5();
fr5.button1.Visible = false;
fr5.ShowDialog();
}
Edit: I've set button1.Visible = true; in Form5 Load event.
Thank you for your suggestions. :) I have solved the problem by changing the code like this:
public void button1_Click(object sender, EventArgs e)
{
Form5 fr5 = new Form5();
fr5.Show(this);
fr5.button1.Visible = true;
}
public void button1_Click(object sender, EventArgs e)
{
Form5 fr5 = new Form5();
fr5.Show(this);
fr5.button1.Visible = false;
}

Visual Studio Windows.Forms Button Background Color selected when Click

//Hi,
I want to write a Software and am confused about "keeping the buttom selected (i mean the background color) when button is clicked, until i click on another one"
If someone could help me, i would appreciate that so much.
Thanks in advance//
namespace SoftwareUI
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
button1.ForeColor = Color.LightGray;
}
private void button1_Leave(object sender, EventArgs e)
{
button1.ForeColor = Color.GhostWhite;
}
private void button1_MouseEnter(object sender, EventArgs e)
{
button1.ForeColor = Color.LightSlateGray;
}
}
}
Add this code for click event of all buttons:
Button b = (Button)sender;
b.BackColor = Color.LightGray;
foreach (Button bt in b.Parent.Controls.OfType<Button>())
{
if (bt != b)
bt.BackColor = Color.White;
}

how to pass string or value from usercontrol to main form in C#

I created a usercontrol that contains many buttons and in the main form I have a textbox.
I add the usercontrol to the main form and I want to click any button on the usercontrol and have the textbox in the main form shows the button text.
The question is how to pass the string of the button in usercontrol to the textbox in the main form? This is what I'm trying to do
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
public string a ;
private void button1_Click(object sender, EventArgs e)
{
a = button1.Text;
}
private void button2_Click(object sender, EventArgs e)
{
a = button2.Text;
}
private void button3_Click(object sender, EventArgs e)
{
a = button3.Text;
}
and the main form code is :
private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox1.Text = usrCtrl.a;
// usrCtrl come from : Usercontrol1 usrCtrl = new Usercontrol1();
}
and it shows nothing in the textbox.
refer to this answer, you need to create a property changed event.
UserControl.cs class;
public partial class UserControl1 : UserControl
{
public event PropertyChangedEventHandler PropertyChanged;
public UserControl1()
{
InitializeComponent();
}
private string stringA;
public string a
{
get { return stringA; }
set
{
if (value != stringA)
{
stringA = value;
if (PropertyChanged!= null)
{
PropertyChanged(this, new PropertyChangedEventArgs(a));
}
}
}
}
private void button1_Click(object sender, EventArgs e)
{
a = button1.Text;
}
private void button2_Click(object sender, EventArgs e)
{
a = button2.Text;
}
private void button3_Click(object sender, EventArgs e)
{
a = button3.Text;
}
private void button4_Click(object sender, EventArgs e)
{
a = button4.Text;
}
}
On Form's Load we need to define the event,
private void Form1_Load(object sender, EventArgs e)
{
cntr.PropertyChanged += Cntr_PropertyChanged; // press tab + tab after += and it will generate the following method automatically.
}
Here is Event;
private void Cntr_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
textBox1.Text = cntr.a.ToString(); //cntr is the instance of UserControl1
}
Hope helps,
Your code to change the textBox1.Text value is in the wrong event handler.
The textBox1_TextChanged event handler only fires when text in that field changes.
What you need to do is put the line:
textBox1.Text = a;
in the click event handlers.

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

how to do label.text = readKey in WFA

How can I ensure that when you click on label, application expect to press any key, and when user press key, label text change to this key char?
Ok, now i have:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
bool isLabelClicked = false;
private void label1_Click(object sender, EventArgs e)
{
isLabelClicked = true;
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (isLabelClicked)
{
label1.Text = ((char)e.KeyValue).ToString();
isLabelClicked = false;
}
}
}
And if I press Up, Down, Left, Right, Tab or Enter, application not responding and still expects press key. Only when I press any other key, application works well.
If I create new project and paste the same code, all works well, for Up, Down, Left and Right also, but I need this keys for my application which I do.
This example a textBox class : when press any key show it
using System;
using System.Windows.Forms;
using System.Drawing;
namespace KeyPressDisplayTextBox {
public partial class Form1 : Form {
private TextBox textBox1;
private Label label1;
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
textBox1 = new TextBox();
textBox1.Location = new Point(10,10);
textBox1.KeyPress += textBox1_KeyPress;
Controls.Add(textBox1);
label1 = new Label();
label1.Location = new Point(10, 40);
label1.BorderStyle = BorderStyle.FixedSingle;
label1.Font = new Font("Arial", 14);
Controls.Add(label1);
}
void textBox1_KeyPress(object sender, KeyPressEventArgs e) {
label1.Text = e.KeyChar.ToString();
}
}
}
Best regards
You can handle KeyDown event on the Form and get the KeyValue as below
Try This:
You need to set the Form KeyPreview property to True to receive the key events from the Form
bool isLabelClicked = false;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (isLabelClicked)
{
label1.Text = ((char)e.KeyValue).ToString();
isLabelClicked = false;
}
}
private void label1_Click(object sender, EventArgs e)
{
isLabelClicked = true;
}

Categories

Resources