I'm a new c# programmer and having trouble updated my programmatically added label based on the textbox value. It works fine if the textboxes and labels are added using the form designer but the quantity textboxes and labels will vary based on my user's data so I will be adding them using code. The label added in code is unfortunately not accessible in the Text_Changed event and my searches on the internet haven't made it clear how to accomplish this. Below is my code.
namespace Test_Form_Controls
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
TextBox txtBox1 = new TextBox();
txtBox1.Text = "0";
txtBox1.Location = new Point(100, 25);
Controls.Add(txtBox1);
txtBox1.TextChanged += txtBox1_TextChanged;
Label label1 = new Label();
label1.Text = "0";
label1.Location = new Point(25, 25);
Controls.Add(label1);
}
private void txtBox1_TextChanged(object sender, EventArgs e)
{
TextBox objTextBox = (TextBox)sender;
label1.Text = objTextBox.Text;
}
}
}
Move your label to be a private field
namespace Test_Form_Controls
{
public partial class Form1 : Form
{
Label label1;
public Form1()
{
InitializeComponent();
TextBox txtBox1 = new TextBox();
txtBox1.Text = "0";
txtBox1.Location = new Point(100, 25);
Controls.Add(txtBox1);
txtBox1.TextChanged += txtBox1_TextChanged;
label1 = new Label();
label1.Text = "0";
label1.Location = new Point(25, 25);
Controls.Add(label1);
}
private void txtBox1_TextChanged(object sender, EventArgs e)
{
TextBox objTextBox = (TextBox)sender;
label1.Text = objTextBox.Text;
}
}
}
Related
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:
I have code for creating textboxes. I'm repeating this code three times in different areas. I'm trying to learn more about methods and classes, so I would like to know if there is any possibility of creating textboxes using methods or classes without repeating code.
private void incomes_Click(object sender, EventArgs e)
{
panel1.Controls.Clear();
TextBox textbox1 = new TextBox();
textbox1.Size = new Size(75,23);
textbox1.Text = "Name";
textbox1.Location = new Point(0, 0);
panel1.Controls.Add(textbox1);
TextBox textbox2 = new TextBox();
textbox2.Size = new Size(75, 23);
textbox2.Text = "Sum";
textbox2.Location = new Point(80, 0);
panel1.Controls.Add(textbox2);
}
Absolutely! You can create a method that accepts the differing parameters and creates the textbox with those specifications:
private void incomes_Click(object sender, EventArgs e)
{
panel1.Controls.Clear();
TextBox textbox1 = CreateTextBox("Name", 0);
TextBox textbox2 = CreateTextBox("Sum", 80);
// Now you can use the textbox values...
}
private TextBox CreateTextBox(string text, int x)
{
TextBox textbox = new TextBox();
textbox.Size = new Size(75, 23);
textbox.Text = text;
textBox.Location = new Point(x, 0);
panel1.Controls.Add(textbox);
return textbox;
}
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!
It says createPassword and repeatPassword are not in the current context. Why and what happens ?
Code;
public MainPage()
{
InitializeComponent();
TextBox createPassword = new TextBox();
createPassword.Width = 400;
TextBox repeatPassword = new TextBox();
repeatPassword.Width = 400;
Button createButton = new Button();
createButton.Content = "Create New Password";
createButton.Click += new RoutedEventHandler(savePassword);
StackPanel content = new StackPanel();
content.HorizontalAlignment = HorizontalAlignment.Center;
content.Children.Add(createPassword);
content.Children.Add(repeatPassword);
content.Children.Add(createButton);
LayoutRoot.Children.Add(content);
}
void savePassword(object sender, RoutedEventArgs e)
{
string password1 = createPassword.Text;
string password2 = repeatPassword.Text;
}
createPassword and repeatPassword must be class members to use them in different class methods:
TextBox createPassword;
TextBox repeatPassword;
public MainPage()
{
InitializeComponent();
createPassword = new TextBox();
createPassword.Width = 400;
repeatPassword = new TextBox();
repeatPassword.Width = 400;
Button createButton = new Button();
createButton.Content = "Create New Password";
createButton.Click += new RoutedEventHandler(savePassword);
StackPanel content = new StackPanel();
content.HorizontalAlignment = HorizontalAlignment.Center;
content.Children.Add(createPassword);
content.Children.Add(repeatPassword);
content.Children.Add(createButton);
LayoutRoot.Children.Add(content);
}
void savePassword(object sender, RoutedEventArgs e)
{
string password1 = createPassword.Text;
string password2 = repeatPassword.Text;
}
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.