C# winforms dynamically created button controls - c#

I have an add user button which dynamically adds a remove button and a username textbox when the user clicks it. The user can click the button as many times as they like and the controls will continue to add.
I am having trouble with the remove button that is created dynamically. It should removed the itself and the username textbox next to it. Instead it will always remove the top row that was added. Also when you click add a new user after you have clicked remove it doesn't automatically fill the blank space - it moves the new textbox and button to the bottom line.
Here is my code:
private void AddUserbtn_Click_1(object sender, EventArgs e)
{
TextBox[] Username = new TextBox[n];
Button[] Remove = new Button[n];
int UsernameX, UsernameY, RemoveX, RemoveY;
UsernameX = 346;
UsernameY = 45;
RemoveX = 946;
RemoveY = 45;
for (int i = 0; i < n; i++)
{
Username[i] = new TextBox();
Username[i].Size = new Size(233, 26);
Username[i].Location = new Point(UsernameX, UsernameY + space);
Username[i].Font = new Font("Arial", 10);
Username[i].Name = "Username" ;
Remove[i] = new Button();
Remove[i].Location = new Point(RemoveX, RemoveY + space);
Remove[i].Text = "Remove";
Remove[i].Font = new Font("Arial", 10);
Remove[i].Size = new Size(95, 23);
Remove[i].UseVisualStyleBackColor = true;
Remove[i].Click += new EventHandler(Remove_Click);
Remove[i].Name = "Remove";
space += 35;
}
for (int i = 0; i < n; i++)
{
CaeUsersPanel.Controls.Add(Username[i]);
CaeUsersPanel.Controls.Add(Remove[i]);
}
}
private void Remove_Click(object sender, EventArgs e)
{
CaeUsersPanel.Controls.Remove(CaeUsersPanel.Controls[("Username")]);
CaeUsersPanel.Controls.Remove(CaeUsersPanel.Controls[("Remove")]);
}

Put a class variant to store Button - TextBox pair, and delete respectively. Quick tested code below - not fine tuned for memory leak / logic etc, just a sample code that fulfills your requirement.
Dictionary<Button, TextBox> pair = new Dictionary<Button, TextBox>(); // A dictionary to store the Button - TextBox pair
private void button1_Click(object sender, EventArgs e) {
int n = 3; // Added for testing
int space = 0; // Added for testing
int UsernameX, UsernameY, RemoveX, RemoveY;
UsernameX = 100; // Modified for testing
UsernameY = 45;
RemoveX = 400; // Modified for testing
RemoveY = 45;
for (int i = 0; i < n; i++) {
var Username = new TextBox();
Username.Size = new Size(233, 26);
Username.Location = new Point(UsernameX, UsernameY + space);
Username.Font = new Font("Arial", 10);
Username.Name = "Username";
var Remove = new Button();
Remove.Location = new Point(RemoveX, RemoveY + space);
Remove.Text = "Remove";
Remove.Font = new Font("Arial", 10);
Remove.Size = new Size(95, 23);
Remove.UseVisualStyleBackColor = true;
Remove.Click += new EventHandler(Remove_Click);
Remove.Name = "Remove";
Controls.Add(Username);
Controls.Add(Remove);
pair.Add(Remove, Username);
space += 35;
}
}
private void Remove_Click(object sender, EventArgs e) {
Controls.Remove((Button)sender); // Removes the delete button
Controls.Remove(pair[(Button)sender]); // Removes the textbox
pair.Remove((Button)sender); // Removes the entry in dictionary
}
Update: Another version for better memory and logic matters. Also shifts up the rest of controls up if it's the OP's desire.
int n = 3; // Added for testing, probably you already have it somewhere
int space = 0; // Added for testing, probably you already have it somewhere
// Moved for logic
// Value modified for testing
int UsernameX = 100;
int UsernameY = 45;
int RemoveX = 400;
int RemoveY = 45;
int SpaceDelta = 35; // Added for logic
List<Button> RemoveButtons = new List<Button>();
List<TextBox> UsernameTextBoxes = new List<TextBox>();
private void button1_Click(object sender, EventArgs e) {
Random rnd = new Random((int)DateTime.Now.Ticks); // Added for testing
for (int i = 0; i < n; i++) {
var Username = new TextBox();
Username.Size = new Size(233, 26);
Username.Location = new Point(UsernameX, UsernameY + space);
Username.Font = new Font("Arial", 10);
Username.Name = "Username";
Username.Text = $"{(int)(rnd.NextDouble() * 100000)}"; // Added for testing
var Remove = new Button();
Remove.Location = new Point(RemoveX, RemoveY + space);
Remove.Text = $"{(int)(rnd.NextDouble() * 100000)}"; // Modified for testing
Remove.Font = new Font("Arial", 10);
Remove.Size = new Size(95, 23);
Remove.UseVisualStyleBackColor = true;
Remove.Click += new EventHandler(Remove_Click);
Remove.Name = "Remove";
Controls.Add(Username);
Controls.Add(Remove);
RemoveButtons.Add(Remove);
UsernameTextBoxes.Add(Username);
space += SpaceDelta;
}
}
private void Remove_Click(object sender, EventArgs e) {
int idx = RemoveButtons.IndexOf((Button)sender);
// Remove button
RemoveButtons[idx].Dispose();
RemoveButtons.RemoveAt(idx);
// Remove textbox
UsernameTextBoxes[idx].Dispose();
UsernameTextBoxes.RemoveAt(idx);
// Shift controls up
for (int i = idx; i < RemoveButtons.Count; i ++) {
RemoveButtons[i].Top -= SpaceDelta;
UsernameTextBoxes[i].Top -= SpaceDelta;
}
space -= SpaceDelta;
}

Remove the two last created :
private void Remove_Click(object sender, EventArgs e)
{
this.Controls.Remove(this.Controls[this.Controls.Count - 1]);
this.Controls.Remove(this.Controls[this.Controls.Count - 1]);
}

Related

Removing from the list with button click

I'm printing out items from the list each to the label, and a button nearby for removing them from the list. The removal button doesn't seem to work.
private void cart_Click(object sender, EventArgs e)
{
krepselioPanel.Visible = !krepselioPanel.Visible;
krepselioPav.Visible = !krepselioPav.Visible;
int i = 0;
double s = 0;
foreach (Patiekalas preke in prekes)
{
Label prekiulist = new Label();
prekiulist.Location = new Point(0, 26 * i);
prekiulist.Text = preke.GetPatiekalas() + " | " + preke.GetKaina() + "€";
prekiulist.Size = new Size(200, 20);
krepselioPanel.Controls.Add(prekiulist);
s += Convert.ToDouble(preke.GetKaina());
Button removeButton = new Button();
removeButton.Text = "x";
removeButton.Location = new Point(200, 26 * i);
removeButton.Font = new Font(FontFamily.GenericSansSerif, 9);
removeButton.Size = new Size(20, 22);
removeButton.Click += removeButton_Click;
removeButton.Tag = preke;
krepselioPanel.Controls.Add(removeButton);
i++;
}
Label suma = new Label();
suma.Location = new Point(krepselioPanel.Right - 140, 0);
suma.Font = new Font(FontFamily.GenericSansSerif, 13);
suma.Text = "Total: " + s + "€";
suma.Size = new Size(130, 25);
krepselioPanel.Controls.Add(suma);
}
private void removeButton_Click(object sender, EventArgs e)
{
Button b = (Button)sender;
Patiekalas preke = (Patiekalas)b.Tag;
prekes.Remove(preke);
cart_Click(sender, e);
cart_Click(sender, e);
}
}
Just for the interest sake I changed the functions removeButton_Click line
prekes.Remove(preke);
to
prekes.Add(preke);
and this creates a new entry to the list but removal doesn't work however.
The code doesn't seem to remove the previous controls from the panel so each time the controls are added new ones are created. If you have only one item in the cart then the next time nothing seems to happen, nothing added nor removed, but if there's multiple items they will start to multiply.
So remove the controls from the panel and then add new ones and the item will disappear.

How do i access these arrays so that i can multiply inputted values in the diagonal?

If I inputted 5, it will generate a square matrix by 5 (5x5)
How do I multiply 1, 7, 13, 19, 25?
Is there any applicable algorithm for my code so I could multiply the diagonals or do i need to rewrite a new one?
public partial class Form1 : Form {
public int e = 0;
int Row = 0;
int Column = 0;
int YAxisPosition = 0;
int XAxisPosition = 0;
int Counter = 0;
int PositionalValue = 0;
TextBox[] MyTextBoxDimA = new TextBox[999999];
TextBox tbRow = new TextBox();
Button MyButton = new Button();
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
AutoScroll = true;
WindowState = System.Windows.Forms.FormWindowState.Maximized;
//GENERATING THE SIZE BUTTON
tbRow = new TextBox();
tbRow.Text = "5";
tbRow.Size = new Size(100, 10);
tbRow.Location = new Point(0, 0);
Controls.Add(tbRow);
//GENERATE MATRIX BUTTON
MyButton = new Button();
MyButton.Text = "GENERATE MATRIX";
MyButton.Size = new Size(200, 25);
MyButton.Click += new EventHandler(MyButton_Click);
MyButton.Location = new Point(0, 30);
Controls.Add(MyButton);
}
public void MyButton_Click(object sender, EventArgs ee) {
//CODE FOR GENERATING MATRIX A
e = 1;
PositionalValue = 1;
Counter = 1;
//POSITION
YAxisPosition = 60;
XAxisPosition = 0;
Row = Convert.ToInt32(tbRow.Text);
Column = Convert.ToInt32(tbRow.Text);
while (Row >= e) {
while (Column >= Counter) {
MyTextBoxDimA[PositionalValue] = new TextBox();
MyTextBoxDimA[PositionalValue].Location =
new Point(XAxisPosition, YAxisPosition); //coordinates (start)
MyTextBoxDimA[PositionalValue].Size = new Size(70, 10);
MyTextBoxDimA[PositionalValue].Text = Convert.ToString(PositionalValue);
Controls.Add(MyTextBoxDimA[PositionalValue]);
XAxisPosition = XAxisPosition + 100;
PositionalValue++;
Counter++;
}
YAxisPosition = YAxisPosition + 50;
Counter = 1;
e++;
XAxisPosition = 0;
}
}
}
If you are looking for an algorithm:
static void Main(string[] args)
{
int n = 5;
int ans = 1;
int current = 1;
for (int i = 1; i <= n; i++)
{
ans = ans * current;
current += n + 1;
}
Console.WriteLine(ans);
}
Just loop from zero to the size of the matrix. The index will be used to get each diagonal value (this code assumes that each textbox contains an integer and is not blank):
int matrixSize = 5;
int product = 1;
for (int i = 0; i < matrixSize; i++)
{
product *= int.Parse(MyTextBoxDimA[i,i].Text);
}

access labels created on button click outside event

I am new to C# and programming in general. I was trying to make a simple shopping list app using a windows form application and Visual studio. This is how I am adding items to the list.
public Form1()
{
InitializeComponent();
}
int x = 50;
int y = 58;
private void addButton_Click(object sender, EventArgs e)
{
Label itemName = new Label();
itemName.Text = itemInput.Text;
itemInput.Text = "";
this.Controls.Add(itemName);
itemName.Location = new Point(x, y);
itemName.Width = 260;
CheckBox coupon = new CheckBox();
coupon.Location = new Point(x - 30, y);
this.Controls.Add(coupon);
y = y + 25;
}
The main issue I have is that I can't have another event change the properties of the label. EX:
public Form1()
{
InitializeComponent();
}
int x = 50;
int y = 58;
private void addButton_Click(object sender, EventArgs e)
{
Label itemName = new Label();
itemName.Text = itemInput.Text;
itemInput.Text = "";
this.Controls.Add(itemName);
itemName.Location = new Point(x, y);
itemName.Width = 260;
CheckBox coupon = new CheckBox();
coupon.Location = new Point(x - 30, y);
this.Controls.Add(coupon);
Button deletButton = new Button();
deletButton.Text = "delete";
this.Controls.Add(deletButton);
deletButton.Location = new Point(x + 260, y);
deletButton.Width = 50;
y = y + 25;
}
private void deletButton_Click(object sender, EventArgs e)
{
itemName.Text = "";
}
It says
the name itemName does not exist in the current context
which make sense because it is in a different method.
My main question is, can I make itemName available outside of that method? Or am I totally going about this wrong and have to redesign the program for the ground up?
Let's say that you want to stick with the dynamic addition of controls, as you're doing now, then a simple way is to give it a name and the find it by that name:
// When you're creating it.
itemName.Name = "itemName";
// Finding it.
var itemName = (Label)this.Controls["itemName"];
// Another way to find it.
var itemName = (Label)this.Controls.Find("itemName", true);

Multiple CheckedChanged event on programmatically added checkboxes

I want to be able to assign each checkbox to it's own richtextbox
I'm making the richtextboxes, then I'm making the checkboxes but how can I "link" them together?
for example :
// richtextbox 1 - > checkbox 1 = false
// richtextbox 2 - > checkbox 2 = true
// richtextbox 3 - > checkbox 3 = true
// richtextbox 4 - > checkbox 4 = false
this is my code:
int n = TodoItems.Count;
RichTextBox[] RichtextBoxes = new RichTextBox[n];
CheckBox[] Checkboxes = new CheckBox[n];
for (int i = 0; i < n; i++)
{
//creating the richtextbox
RichtextBoxes[i] = new RichTextBox();
RichtextBoxes[i].Name = "TB" + i.ToString();
RichtextBoxes[i].Text = TodoItems[i].ToString();
RichtextBoxes[i].Location = new System.Drawing.Point(130, (10 + (60 * i)));
RichtextBoxes[i].Size = new System.Drawing.Size(300, 50);
RichtextBoxes[i].Visible = true;
RichtextBoxes[i].ReadOnly = true;
RichtextBoxes[i].SelectionAlignment = HorizontalAlignment.Center;
RichtextBoxes[i].BackColor = Color.White;
TodoList.Controls.Add(RichtextBoxes[i]);
//creating the checkboxes
Checkboxes[i] = new CheckBox();
Checkboxes[i].Name = "CB" + i.ToString();
Checkboxes[i].Text = "";
Checkboxes[i].Location = new System.Drawing.Point(440, (30 + (60 * i)));
Checkboxes[i].Size = new System.Drawing.Size(18, 17);
Checkboxes[i].Visible = true;
Checkboxes[i].CheckedChanged += new EventHandler(this.CheckedChange);
TodoList.Controls.Add(Checkboxes[i]);
}
I have modified your code please see below, you can access your desired rich text box on checkbox click.
RichTextBox[] RichtextBoxes { get; set; }
CheckBox[] Checkboxes { get; set; }
private void Form1_Load(object sender, EventArgs e)
{
int n = TodoItems.Count;
RichtextBoxes = new RichTextBox[n];
Checkboxes = new CheckBox[n];
for (int i = 0; i < n; i++)
{
//creating the richtextbox
RichtextBoxes[i] = new RichTextBox();
RichtextBoxes[i].Name = "TB-" + i.ToString();
RichtextBoxes[i].Text = TodoItems[i].ToString();
RichtextBoxes[i].Location = new System.Drawing.Point(130, (10 + (60 * i)));
RichtextBoxes[i].Size = new System.Drawing.Size(300, 50);
RichtextBoxes[i].Visible = false;
RichtextBoxes[i].ReadOnly = true;
RichtextBoxes[i].SelectionAlignment = HorizontalAlignment.Center;
RichtextBoxes[i].BackColor = Color.White;
TodoList.Controls.Add(RichtextBoxes[i]);
//creating the checkboxes
Checkboxes[i] = new CheckBox();
Checkboxes[i].Name = "CB-" + i.ToString();
Checkboxes[i].Text = "";
Checkboxes[i].Location = new System.Drawing.Point(440, (30 + (60 * i)));
Checkboxes[i].Size = new System.Drawing.Size(18, 17);
Checkboxes[i].Visible = true;
Checkboxes[i].CheckedChanged += checkBox1_CheckedChanged;
TodoList.Controls.Add(Checkboxes[i]);
}
}
void checkBox1_CheckedChanged(object sender, EventArgs e)
{
CheckBox cb = sender as CheckBox;
string cbName = cb.Name;
int sbNumber = int.Parse(cbName.Split('-')[1]);
RichtextBoxes[sbNumber].Visible = true; // you can get desired richtextbox here and can any thing with it :)
}
I have found the answere, and I'll post it to make your life easyer
when I made the richtextboxes and the checkboxes I set there names to a number
Checkboxes[i].Name = i.ToString();
then I used the event that was provided for me by Mirza Danish Baig
Create and event void checkBox_CheckedChanged(object sender, EventArgs e) { } and then assign this event name Checkboxes[i].CheckedChanged += checkBox_CheckedChanged; –
and after that I started to try things eventualy I came to this :
private void checkBox_CheckedChanged(object sender, EventArgs e)
{
CheckBox ThisCheckbox = (CheckBox)sender;
if (ThisCheckbox.Checked == true)
{
//finding the richtextbox by id...
RichTextBox ThisRichtextbox = this.Controls.Find("TB" + ThisCheckbox.Name, true).FirstOrDefault() as RichTextBox;
try//try and catch for testing, this can be removed later.
{
MessageBox.Show(ThisRichtextbox.Text);
}
catch (Exception Exc)
{
MessageBox.Show(Exc.Message);
}
}
}

How Can I Add multipe buttons on c# dynamically using point class?

Hello I try to add multiple Buttons on a panel on form and next to each other but instead it put them above each other.
I am using the following function.
the Code:
private void CreatBtn()
{
Point[] p = new Point[6];
string log = "";
Form2 frm2 = new Form2();
Button[] btn = new Button[6];
for (int i = 0; i < btn.GetLength(0); i++)
{
btn[i] = new Button();
btn[i].Height = 65;
btn[i].Width = 80;
p[i] = new Point();
p[i].X = i * 83;
p[i].Y =0;
log +=p.ToString() +"\n";
btn[i].PointToClient(p[i]);
btn[i].Show();
}
panel1.Controls.AddRange(btn);
}
Add a Left value to your buttons
btn[i] = new Button();
btn[i].Height = 65;
btn[i].Width = 80;
btn[i].Left = i * 83; //Now they'll be next to each other.
You may also consider using the FlowLayoutPanel.
Use flowLayoutPanel in the panel of it'll but the next to each other until the button reach the end of it, then it will make an new raw
Point[] p = new Point[6];
string log = "";
Button[] btn = new Button[6];
for (int i = 0; i < btn.GetLength(0); i++)
{
btn[i] = new Button();
btn[i].Height = 65;
btn[i].Width = 80;
p[i] = new Point();
p[i].X = i * 83;
p[i].Y = 0;
log += p.ToString() + "\n";
btn[i].PointToClient(p[i]);
btn[i].Show();
}
FlowLayoutPanel pan = new FlowLayoutPanel();
pan.Width=500;//width of all buttons
pan.Height = 100;
pan.Controls.AddRange(btn);
panel1.Controls.Add(pan);

Categories

Resources