I have this code to create a button:
private void CreateButton(){
Button createButton = new Button();
// Set Button properties
createButton.Height = 40;
createButton.Width = 300;
createButton.BackColor = Color.Red;
createButton.ForeColor = Color.Blue;
createButton.Location = new Point(20, 150);
createButton.Text = "I am Button";
createButton.Name = "Button";
createButton.Font = new Font("Georgia", 16);
createButton.Click += new EventHandler(create_Button_Click);
this.Controls.Add(createButton);
}
private void create_Button_Click(object sender, EventArgs e)
{
MessageBox.Show("button is clicked");
}
and i want to save it permanently in the project. what is the best way to do this?
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:
How to create a validator from the textbox class or create new textbox, rather than dragging the textbox from the toolbox
private void Form1_Load(object sender, EventArgs e)
{
createTextpass();
// Creating and setting the properties of TextBox1
TextBox textboxUsername = new TextBox();
textboxUsername.Location = new Point(420, 50);
textboxUsername.Size = new Size (300,30);
textboxUsername.Name = "text_user";
this.Controls.Add(textboxUsername);
TextBox textboxPassword = new TextBox();
textboxPassword.Location = new Point(420, 80);
textboxPassword.Size = new Size(300, 30);
textboxPassword.Name = "text_pass";
this.Controls.Add(textboxPassword);
TextBox textboxMail = new TextBox();
textboxMail.Location = new Point(420, 110);
textboxMail.Size = new Size(300, 30);
textboxMail.Name = "text_mail";
this.Controls.Add(textboxMail);
}
Add the texbox width and height
If I understand your question correctly. You want to create a textbox and check whether valid text is entered in this textbox or not. For example, the following code can be used for textboxUsername:
public TextBox textboxUsername;
private void Form1_Load(object sender, EventArgs e)
{
createTextpass();
// Creating and setting the properties of TextBox1
textboxUsername = new TextBox();
textboxUsername.Location = new Point(420, 50);
textboxUsername.Size = new Size(300, 30);
textboxUsername.Name = "text_user";
this.Controls.Add(textboxUsername);
textboxUsername.TextChanged += new EventHandler(usernameTextbox_TextChanged);
....
}
//If textboxUsername contains the # character, an alarm will be displayed
protected void usernameTextbox_TextChanged(object sender, EventArgs e)
{
if (textboxUsername.Text.Contains('#'))
MessageBox.Show("inavlid char in userName");
}
In the Form1_Load method what code should I write to create a simple button?
private void Form1_Load(object sender, System.EventArgs e)
{
}
So that on Load the button would show.
As you said it is Winforms, you can do the following...
First create a new Button object.
Button newButton = new Button();
Then add it to the form inside that function using:
this.Controls.Add(newButton);
Extra properties you can set...
newButton.Text = "Created Button";
newButton.Location = new Point(70,70);
newButton.Size = new Size(50, 100);
Your issue you're running to is that you're trying to set it on Form_Load event, at that stage the form does not exist yet and your buttons are overwritten. You need a delegate for the Shown or Activated events in order to show the button.
For example inside your Form1 constructor,
public Form1()
{
InitializeComponent();
this.Shown += CreateButtonDelegate;
}
Your actual delegate is where you create your button and add it to the form, something like this will work.
private void CreateButtonDelegate(object sender, EventArgs e)
{
Button newButton= new Button();
this.Controls.Add(newButton);
newButton.Text = "Created Button";
newButton.Location = new Point(70,70);
newButton.Size = new Size(50, 100);
newButton.Location = new Point(20, 50);
}
on your eventload form put this code
private void Form1_Load(object sender, EventArgs e)
{
Button testbutton = new Button();
testbutton.Text = "button1";
testbutton.Location = new Point(70, 70);
testbutton.Size = new Size(100, 100);
testbutton.Visible = true;
testbutton.BringToFront();
this.Controls.Add(testbutton);
}
It's simple :
private void Form1_Load(object sender, System.EventArgs e)
{
Button btn1 = new Button();
this.Controls.add(btn1);
btn1.Top=100;
btn1.Left=100;
btn1.Text="My Button";
}
I've got a form with a combo to pick up a customer and a button.
When a customer is selected and you move the mouse over the button a ToolTip is shown with information about this customer.
I have customized the tooltip using ToolTip_Draw.
All of these works fine.
The problem is that when I change de selected customer and then move the mouse over the button several ToolTip texts are shown. One for each customer I've previously selected.
I've tried to somehow empty de ToolTip but nothing seems to work.
private void bttCitas_MouseHover(object sender, EventArgs e)
{
string mSQL = #" SELECT one, two, three
FROM customers
WHERE id = " + comboCliente.SelectedValue + ";";
DataTable tablaTemp = retrieveData(mSQL);
string customerText = ConvertDataTableToString(tablaTemp);
System.Windows.Forms.ToolTip Emergente = new System.Windows.Forms.ToolTip();
Emergente.OwnerDraw = true;
Emergente.Draw += new DrawToolTipEventHandler(ToolTip_Draw);
Emergente.AutoPopDelay = 150000;
Emergente.InitialDelay = 500;
Emergente.ReshowDelay = 500;
Emergente.SetToolTip(this.bttCitas, customerText);
}
void ToolTip_Draw(object sender, DrawToolTipEventArgs e)
{
using (e.Graphics)
{
Font f = new Font("Courier New", 9.0f);
e.DrawBackground();
e.DrawBorder();
SolidBrush myBrush = new SolidBrush(GLOBALToolTipFontColor);
e.Graphics.DrawString(e.ToolTipText, f, myBrush, new PointF(2, 2));
}
}
I've finally figured out what happens.
I need to declare Emergente out of the MouseOver event and then dispose it in the MouseLeave:
private ToolTip Emergente;
private void bttCitas_MouseHover(object sender, EventArgs e)
{
string mSQL = #" SELECT one, two, three
FROM customers
WHERE id = " + comboCliente.SelectedValue + ";";
DataTable tablaTemp = retrieveData(mSQL);
string customerText = ConvertDataTableToString(tablaTemp);
Emergente = new System.Windows.Forms.ToolTip();
Emergente.OwnerDraw = true;
Emergente.Draw += new DrawToolTipEventHandler(ToolTip_Draw);
Emergente.AutoPopDelay = 150000;
Emergente.InitialDelay = 500;
Emergente.ReshowDelay = 500;
Emergente.SetToolTip(this.bttCitas, customerText);
}
void ToolTip_Draw(object sender, DrawToolTipEventArgs e)
{
using (e.Graphics)
{
Font f = new Font("Courier New", 9.0f);
e.DrawBackground();
e.DrawBorder();
SolidBrush myBrush = new SolidBrush(GLOBALToolTipFontColor);
e.Graphics.DrawString(e.ToolTipText, f, myBrush, new PointF(2, 2));
}
}
private void bttCitas_MouseLeave(object sender, EventArgs e)
{
Emergente.Dispose();
}
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.