Button creates a button then that button show/hide form - c#

I already know how to create a button when a button is clicked. What code should I write next to this lines for me to be able to show/hide forms?
Button b1 = new Button();
b1.Location = new Point (21, 0);
b1.Name = "";
b1.Size = new Size(120, 100);
b1.FlatStyle = FlatStyle.Flat;
b1.Image = TITOMS_LOGIN.Properties.Resources.icon1_1_;
b1.BackColor = Color.Transparent;
Button b2 = new Button();
b2.Location = new Point(21, 99);
b2.Name = "";
b2.Size = new Size(120, 100);
b2.FlatStyle = FlatStyle.Flat;
b2.Image = TITOMS_LOGIN.Properties.Resources.icon2_1_;
b2.BackColor = Color.Transparent;
Button b3 = new Button();
b3.Location = new Point(21, 198);
b3.Name = "";
b3.Size = new Size(120, 100);
b3.FlatStyle = FlatStyle.Flat;
b3.Image = TITOMS_LOGIN.Properties.Resources.icon3_1_;
b3.BackColor = Color.Transparent;
Button b4 = new Button();
b4.Location = new Point(21, 297);
b4.Name = "";
b4.Size = new Size(120, 100);
b4.FlatStyle = FlatStyle.Flat;
b4.Image = TITOMS_LOGIN.Properties.Resources.icon2_1_;
b4.BackColor = Color.Transparent;
for each button they show different form
For example: Button 1 shows Form 1 and Hide others
Button 2 shows Form 2 and hide others

You should handle button click events and inside each of them you have to instantiate the desired form and show it with Show() method.

You have the buttons.
You need Events for the Buttons to perform actions.
b1.Click += new System.EventHandler(button1_Click);
b2.Click += new System.EventHandler(button2_Click);
b3.Click += new System.EventHandler(button3_Click);
b4.Click += new System.EventHandler(button4_Click);
button1_Click is a method which is called, whenever the button is clicked.
If you want do show an new Form:
private void button1_Click(object sender, EventArgs e)
{
Form form = new Form();
form.Show();
}
If you want to close other Forms, the forms must be global.
Form2 form1;
Form2 form2;
private void button1_Click(object sender, EventArgs e)
{
//Create new Form
form2 = new Form2();
form2.Show();
//Check if other Form1 is not null -> it was initialized
if (form1 != null )
{
form1.Close();
form1.Dispose();
form1 = null;
}
}

Related

How can I use a textbox, label and button that was added to a panel to input and show information

Okay so basically I have a calendar display and when you click on anyone of the dates on it, it creates a new panel with a label displaying the date selected. I also made it so when you click on a date and a new panel is made, a label, textbox and button is created and placed onto that new panel as well.
So what I want and have been struggling with is for me to enter something into that textbox then to press the button to submit it and then for it to show on the label.
I think I know what the issue is but I've been stuck at this for hours.
Here is my code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void monthCalendar1_DateSelected_1(object sender, DateRangeEventArgs e)
{
Panel newPanel = new Panel();
this.Controls.Add(newPanel);
newPanel.Visible = true;
newPanel.Size = new Size(564, 831);
newPanel.Location = new Point(0, 190);
newPanel.BringToFront();
Label textLabel = new Label();
textLabel.Size = new Size(500, 500);
textLabel.Font = new Font(textLabel.Font.Name, 25, textLabel.Font.Style);
textLabel.Location = new Point(3, 3);
Label dateLabel = new Label();
dateLabel.Size = new Size(500, 500);
dateLabel.Font = new Font(dateLabel.Font.Name, 25, dateLabel.Font.Style);
dateLabel.Location = new Point(128, 3);
Button Submitbutton = new Button();
Submitbutton.Location = new Point(100, 500);
Submitbutton.Text = "Add Food";
Submitbutton.Size = new Size(400, 100);
Submitbutton.BackColor = Color.Aqua;
Submitbutton.BringToFront();
Submitbutton.Click += Button_Click;
TextBox textBox = new TextBox();
textBox.Location = new Point(100, 650);
textBox.Size = new Size(500, 500);
textBox.BackColor = Color.Aqua;
textBox.Visible = true;
textBox.Text = "Enter food here...";
textBox.BringToFront();
Label inputtedFood = new Label();
inputtedFood.Size = new Size(500, 500);
inputtedFood.Font = new Font(inputtedFood.Font.Name, 25, inputtedFood.Font.Style);
inputtedFood.Location = new Point(100, 600);
inputtedFood.Text = "placeholder";
newPanel.Controls.Add(dateLabel);
newPanel.Controls.Add(textLabel);
newPanel.Controls.Add(Submitbutton);
newPanel.Controls.Add(textBox);
newPanel.Controls.Add(inputtedFood);
String myCalendar = monthCalendar1.SelectionRange.Start.ToShortDateString();
textLabel.Text = "Date:";
dateLabel.Text = myCalendar;
}
private void Button_Click(object sender, EventArgs e)
{
inputtedFood.Text = textBox.Text;
}
private void monthCalendar1_DateChanged_1(object sender, DateRangeEventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
}
}
I tried the above code and was met with errors that are shown in the post.
Totally agree with both LarsTech and Ňɏssa Pøngjǣrdenlarp, you should be building a UserControl in place of the Panel and placing the TextBox, Button, and Label inside of that.
Your immediate question, though:
So what I want and have been struggling with is for me to enter
something into that textbox then to press the button to submit it and
then for it to show on the label.
Can be accomplished with this simple code:
Button Submitbutton = new Button();
// ... more code ...
Submitbutton.Click += (s2, e2) =>
{
inputtedFood.Text = textBox.Text;
};
Here's a little example showing it in action:
private void button1_Click(object sender, EventArgs e)
{
FlowLayoutPanel flp = new FlowLayoutPanel();
TextBox textBox = new TextBox();
// ... more code ...
Label inputtedFood = new Label();
inputtedFood.Text = "placeholder";
// ... more code ...
Button Submitbutton = new Button();
// ... more code ...
Submitbutton.Click += (s2, e2) =>
{
inputtedFood.Text = textBox.Text;
};
flp.Controls.Add(textBox);
flp.Controls.Add(Submitbutton);
flp.Controls.Add(inputtedFood);
flowLayoutPanel1.Controls.Add(flp);
}
The output:

How to add button to a previous, manual-created form?

What I'm trying to do is: create a new form with a click (DONE and working, see code below), and then add some buttons to that new form. In this case, it's just one button because I need to make this work before adding more buttons. Should be pretty simple, but after following some Stackoverflow answers/YouTube tutorials/Internet tutorials I'm still not able to do it.
Actually, [the application] It's meant to be like a personal schedule, where I could track every activity or work to do, dispersed over several days (from monday to friday) and in each day you should find the different times of the day (morning/midday/evening).
My code is shown below (this code belongs to the button1_Click method of the first form, as you may notice).
private void button1_Click(object sender, EventArgs e)
{
// día lunes
Form SubLunes = new Form(); // new form
SubLunes.Text = "Día lunes";
SubLunes.Size = new Size(800, 400);
SubLunes.StartPosition = FormStartPosition.CenterScreen;
SubLunes.FormBorderStyle = FormBorderStyle.FixedSingle;
SubLunes.ShowIcon = false;
SubLunes.CreateControl();
SubLunes.ShowDialog();
// botones
Button Mañana = new Button(); // new button
Mañana.Location = new System.Drawing.Point(100, 150);
Mañana.Size = new Size(100, 100);
Mañana.Text = "Mañana";
Mañana.Click += new EventHandler(Mañana_Click);
SubLunes.Controls.Add(Mañana); // should add button to SubLunes
}
private void Mañana_Click(object sender, EventArgs e)
{
MessageBox.Show("hello, i'm new button"); // displayed when clicking new button
}
This is how currently looks:
Main form ///////
After clicking Lunes button (a new Button called 'Mañana' should be in there)
THANKS YOU in advance. See you later.
You're showing the form before creating the button :
Form SubLunes = new Form();
SubLunes.Text = "Día lunes";
SubLunes.Size = new Size(800, 400);
SubLunes.StartPosition = FormStartPosition.CenterScreen;
SubLunes.FormBorderStyle = FormBorderStyle.FixedSingle;
SubLunes.ShowIcon = false;
SubLunes.CreateControl();
SubLunes.ShowDialog();
Button Mañana = new Button();
Mañana.Location = new System.Drawing.Point(100, 150);
Mañana.Size = new Size(100, 100);
Mañana.Text = "Mañana";
Mañana.Click += new EventHandler(Mañana_Click);
SubLunes.Controls.Add(Mañana);
You should create the button before showing the form, like this :
Form SubLunes = new Form();
SubLunes.Text = "Día lunes";
SubLunes.Size = new Size(800, 400);
SubLunes.StartPosition = FormStartPosition.CenterScreen;
SubLunes.FormBorderStyle = FormBorderStyle.FixedSingle;
SubLunes.ShowIcon = false;
SubLunes.CreateControl();
Button Mañana = new Button(); // new button
Mañana.Location = new System.Drawing.Point(100, 150);
Mañana.Size = new Size(100, 100);
Mañana.Text = "Mañana";
Mañana.Click += new EventHandler(Mañana_Click);
SubLunes.Controls.Add(Mañana);
SubLunes.ShowDialog();

TextBox not activating Form accept button

I have a Form which has a TextBox and a Button. I have set the Form's AcceptButton property to my Button, and set the TextBox's AcceptsReturn property to false:
class Window : Form
{
private TextBox textBox1;
private Button btn;
public Window()
{
this.Size = new Size(200, 200);
this.AcceptButton = this.btn;
textBox1 = new TextBox();
textBox1.Location = new Point(10, 10);
textBox1.Width = 50;
textBox1.AcceptsReturn = false;
this.Controls.Add(textBox1);
btn = new Button();
btn.Text = "Test";
btn.Location = new Point(textBox1.Right + 10, 10);
btn.Click += btn_Click;
this.Controls.Add(btn);
}
void btn_Click(object sender, EventArgs e)
{
MessageBox.Show("Works");
}
}
class Program
{
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.Run(new Window());
}
}
However, when pressing the Enter key while the TextBox has focus, the AcceptButton of the Form is not activated. I worked around it using the KeyDown event of the TextBox like so:
textBox1.KeyDown += (s,e) => { if (e.KeyCode == Keys.Enter) btn.PerformClick(); } ;
And although it works I am curious as to why the earlier method using the AcceptButton property failed.
The problem is that you set the Form's AcceptButton to btn before you instantiate it. Move this.AcceptButton = this.btn; to any line AFTER btn = new Button();.. btn is pointing to a null reference up until new Button(). Once you instantiate btn you can use it to set AcceptButton.
You just wrote one line in the wrong place.That's the answer:
public Form1()
{
InitializeComponent();
this.Size = new Size(200, 200);
textBox1 = new TextBox();
textBox1.Location = new Point(10, 10);
textBox1.Width = 50;
textBox1.AcceptsReturn = true;
this.Controls.Add(textBox1);
btn = new Button();
btn.Text = "Test";
btn.Location = new Point(textBox1.Right + 10, 10);
btn.Click += btn_Click;
this.Controls.Add(btn);
this.AcceptButton = this.btn;
}
I hope this helps you!

How to control form load from another form

I want to control my form load event from another form.
my problem I create some winform control in form1 runtime, but the creation will controlled by form2.
I will read some data from user in form2 and when user enter specific text I will create winform control in form1.
I make some code to do that using from1 to create winform control in runtime.
private TextBox txtBox = new TextBox();
private Button btnAdd = new Button();
private ListBox lstBox = new ListBox();
private CheckBox chkBox = new CheckBox();
private Label lblCount = new Label();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.MaximizeBox = false;
this.MinimizeBox = false;
this.BackColor = Color.White;
this.ForeColor = Color.Black;
this.Size = new System.Drawing.Size(550, 550);
this.Text = "Test Create form in runtime ";
this.FormBorderStyle = FormBorderStyle.FixedDialog;
this.StartPosition = FormStartPosition.CenterScreen;
this.btnAdd.BackColor = Color.Gray;
this.btnAdd.Text = "Add";
this.btnAdd.Location = new System.Drawing.Point(90, 25);
this.btnAdd.Size = new System.Drawing.Size(50, 25);
this.txtBox.Text = "Text";
this.txtBox.Location = new System.Drawing.Point(10, 25);
this.txtBox.Size = new System.Drawing.Size(70, 20);
this.lstBox.Items.Add("One");
this.lstBox.Sorted = true;
this.lstBox.Location = new System.Drawing.Point(10, 55);
this.lstBox.Size = new System.Drawing.Size(130, 95);
this.chkBox.Text = "Disable";
this.chkBox.Location = new System.Drawing.Point(15, 190);
this.chkBox.Size = new System.Drawing.Size(110, 30);
this.lblCount.Text = lstBox.Items.Count.ToString() + " items";
this.lblCount.Location = new System.Drawing.Point(55, 160);
this.lblCount.Size = new System.Drawing.Size(65, 15);
this.Controls.Add(btnAdd);
this.Controls.Add(txtBox);
this.Controls.Add(lstBox);
this.Controls.Add(chkBox);
this.Controls.Add(lblCount);
}
How make the same thing from form2 ?
I don't know which kind of 'Control' you need. However in multiple forms environment, communication between forms is trivial. There are many ways to do communicate, like one can be as
Create public properties of type Form in your Parent form,
public Form propForm1 {get;set;}
When on menu item click you open form1, store it's object to that public property.
var form1 = New yourchildformname();
form1.MdiParent = this;
propForm1 = form1; // Add this line.
form1.Show();
Now every time when you will click an other button to open the form2, you will have propForm1 object, which you can use to set some data on that form.
EDIT:
On form2, you can access controls of form1 as
private void button1_Click(object sender, EventArgs e)
{
this.parent.propForm1.txtUserName = "Yokohama";
}
Remember the above code is on form2. Also set 'access modifier' property of txtUserName from private to public.

Iterating controls on button click in c# windows application

I am trying to create a windows application where I want to display a group of controls (Combo Box, Text Box and a button) on button click inside a panel.
I have created a code to create controls once but I want to create them again and again on button click one below another.
The code I am using is
public partial class Employee_PayHeads_add : Form
{
private TextBox txtBox = new TextBox();
private Button btnAdd = new Button();
private ComboBox combohead = new ComboBox();
public Employee_PayHeads_add()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.btnAdd.BackColor = Color.Gray;
this.btnAdd.Text = "Remove";
this.btnAdd.Location = new System.Drawing.Point(240, 25);
this.btnAdd.Size = new System.Drawing.Size(70, 25);
this.txtBox.Text = "";
this.txtBox.Location = new System.Drawing.Point(150, 25);
this.txtBox.Size = new System.Drawing.Size(70, 40);
this.combohead.Location = new System.Drawing.Point(10, 25);
panel1.Controls.Add(btnAdd);
panel1.Controls.Add(txtBox);
panel1.Controls.Add(combohead);
}
Also I want a vertical scroller in the panel if number controls overlap the space.
Thanks in advance
Inside the button click event create new objects, instead of using the one you declared before.
Try something like that:
public partial class Employee_PayHeads_add : Form
{
private TextBox txtBox = new TextBox();
private Button btnAdd = new Button();
private ComboBox combohead = new ComboBox();
private int txtBoxStartPosition = 150;
private int btnAddStartPosition = 240;
private int comboheadStartPosition = 10;
}
public Employee_PayHeads_add()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
TextBox newTxtBox = new TextBox();
Button newBtnAdd = new Button();
ComboBox newCombohead = new ComboBox();
newBtnAdd.BackColor = Color.Gray;
newBtnAdd.Text = "Remove";
newBtnAdd.Location = new System.Drawing.Point(btnAddStartPosition, 25);
newBtnAdd.Size = new System.Drawing.Size(70, 25);
newTxtBox.Text = "";
newTxtBox.Location = new System.Drawing.Point(txtBoxStartPosition, 25);
newTxtBox.Size = new System.Drawing.Size(70, 40);
newCombohead.Location = new System.Drawing.Point(comboheadStartPosition, 25);
panel1.Controls.Add(newBtnAdd);
panel1.Controls.Add(newTxtBox);
panel1.Controls.Add(newCombohead);
txtBoxStartPosition += 50;
btnAddStartPosition += 50;
comboheadStartPosition += 50;
}
I havent tried your code yet, but it shows that it is always creating the new controls on every click event, but as youo have specified the hardcoaded location for buttons, so it must be creating new controls overlapping each other. so you can change the location dynamically and hopefully it will work
If you want to add the controls again and again you have to create new ones. So rather than defining them in your form like that you have to:
private void button1_Click(object sender, EventArgs e)
{
Button btnAdd = new Button();
btnAdd.BackColor = Color.Gray;
btnAdd.Text = "Remove";
btnAdd.Location = new System.Drawing.Point(240, 25);
btnAdd.Size = new System.Drawing.Size(70, 25);
TextBox txtBox = new TextBox();
txtBox.Text = "";
txtBox.Location = new System.Drawing.Point(150, 25);
txtBox.Size = new System.Drawing.Size(70, 40);
ComboBox combohead = new ComboBox();
combohead.Location = new System.Drawing.Point(10, 25);
panel1.Controls.Add(btnAdd);
panel1.Controls.Add(txtBox);
panel1.Controls.Add(combohead);
}
Now you can remove those private declarations on top of your class.

Categories

Resources