How can I retrieve the input values of dynamically created textfields? - c#

Here is my code so far where I have a numericupdown item named numericUpDown and button. Once the user selects a number when they press the button it dynamically created the fields.
private void createPerson_Click(object sender, EventArgs e)
{
Label[] person_Name = new Label[(int)this.numericUpDown.Value];
TextBox[] person_txtinput = new TextBox[(int)this.numericUpDown.Value];
for (int i = 0; i < this.numericUpDown.Value; i++)
{
//create person name label
Person_Name[i] = new Label();
Person_Name[i].Location = new System.Drawing.Point(20, 114 + i * 25);
Person_Name[i].Size = new System.Drawing.Size(120, 15);
Person_Name[i].Text = (i + 1).ToString() + #")" + "Person Name:";
this.Controls.Add(Person_Name[i]);
//create person name textbox
PersonNameTxtInput[i] = new TextBox();
PersonNameTxtInput[i].Location = new System.Drawing.Point(140, 114 + i * 25);
PersonNameTxtInput[i].Size = new System.Drawing.Size(125, 20);
this.Controls.Add(PersonNameTxtInput[i]);
}
}
private void save_Click(object sender, EventArgs e)
{
for (int i = 0; j < this.numericUpDown.Value; i++)
{
MessageBox.Show("" + PersonNameTxtInput[i].Text);
}
}
My question is, how can I get all the values from the textboxes depending on how many fields are created by the user when the save button is pressed?
I have tried using the code within the save button listener however how can i make Label[] person_Name = new Label[(int)this.numericUpDown.Value]; a global variable so i can access it within the save button for loop.

Well, I don't know exactly why you are doing this in this particular way and I must admit it doesn't seem very effective, but you could just do what Ryan_L suggested and iterate through this.Controls like this
for(int i = 0; i < this.Controls.Count; i++)
{
if(this.Controls[i] is TextBox) //skip buttons and labels
{
MessageBox.Show("" + this.Controls[i].Text);
}
}
Now, regarding your question how to define a global variable so you can access it within the save button for loop...just define the two arrays outside of the createPerson_Click event like this:
Label[] person_Name;
TextBox[] person_txtinput;
private void button1_Click(object sender, EventArgs e)
{
person_Name = new Label[(int)this.numericUpDown.Value];
person_txtinput = new TextBox[(int)this.numericUpDown.Value];
//the rest of the code
}
Hope this helps. However, you might want to reconsider your entire approach.

Related

Dynamically adding controls and eventhandlers (asp.net c#)

I'm a computing foundation year student and am designing a basic e-commerce site for a project at uni and have run into a problem that has me banging my head against my desk
So..
I am dynamically creating a shopping basket using asp.net controls in the Page_Load method of the basket page based off what is in a session variable (type dictionary - the key is the product id and the value is the qty).
I can add items from the browse page which has more dynamically added controls based off whatever the user puts in the products table of the database, that works fine.
I can populate the basket with all the details but am struggling with a '-' and '+' button to alter the qty in the basket.
I can assign the event handler to run a method in a class that adds (or removes) 1 item at a time but the problem I face is if I place my code in Page_Load the function works but renders the controls before the event handler fires (so while the dictionary updates, it's not showing the new value - you have to refresh or add another and then you're always 1 behind)
If I place the code in PreRender the event handler doesnt fire.
This is my first ever project in ASP.NET so please go easy on me if I'm barking up the wrong tree with my methodology.
Any ideas or a nudge in the right direction would be gratefully received
Many thanks in advance
EDIT To add a bit more detail
//creating my button
Button tdQtyDownButton = new Button();
tdQtyDownButton.ID = "qtyDown"+ row["prod_id"]; tdQtyDownButton.Text = "-";
tdQtyDownButton.Click += delegate (object sender1, EventArgs e1) {
ShoppingBasket.AddItem((int)row["prod_id"]); };
tdQtyDown.Controls.Add(tdQtyDownButton);
//in seperate ShoppingBasket class file
public static void AddItem(int prod_id)
{
if (HttpContext.Current.Session["basket"] != null)
{
if(!((Dictionary<int, int>)HttpContext.Current.Session["basket"]).ContainsKey(prod_id))
{
((Dictionary<int, int>)HttpContext.Current.Session["basket"]).Add(prod_id, 1);
}
else
{
int currentQty = 0;
((Dictionary<int, int>)HttpContext.Current.Session["basket"]).TryGetValue(prod_id, out currentQty);
((Dictionary<int, int>)HttpContext.Current.Session["basket"]).Remove(prod_id);
((Dictionary<int, int>)HttpContext.Current.Session["basket"]).Add(prod_id, currentQty + 1);
}
}
else
{
CreateBasket();
AddItem( prod_id);
}
}
As i said, it sort of works - I think it's a just a lifecycle issue and probably needs a wholly fresh approach
Assuming you want to add a Button dynamically and handle it's click event :
Button button1 = new Button();
button1.Text = "dynamic button";
button1.Left = 10; button1.Top = 10;
textBox1.Click += new EventHandler(btn_click);
this.Controls.Add(button1);
private void btn_click()
{
}
Update : From your comment,it seems like that you want to refresh your page without reloading it...For that,your easiest approach will be SignalR or you can use the UpdatePanel Control
For Windows Form Application
public partial class Form1 : Form
{
int i = 1;
int j = 1;
int rbCount = 0;
public Form1()
{
InitializeComponent();
}
private void buttonAddRadio_Click(object sender, EventArgs e)
{
RadioButton rb = new RadioButton();
rb.Name = "Radio Button" + i.ToString();
rb.Text = "Radio Button" + i;
rb.Left = 8;
rb.Top = 15 + (rbCount * 27);
rb.AutoSize = true;
rb.Click += new EventHandler(radio_click);
groupBox1.Controls.Add(rb);
i++;
rbCount++;
}
void radio_click(object sender, EventArgs e)
{
MessageBox.Show(((RadioButton)sender).Text);
}
private void buttonAddCheck_Click(object sender, EventArgs e)
{
CheckBox cb = new CheckBox();
cb.Name = "CheckBox" + j.ToString();
cb.Text = "CheckBox" + j;
cb.Left = 8;
cb.Top = 15 + (rbCount * 27);
cb.AutoSize = true;
cb.Click += new EventHandler(checkbox_checked);
groupBox1.Controls.Add(cb);
j++;
rbCount++;
}
void checkbox_checked(object sender, EventArgs e)
{
MessageBox.Show(((CheckBox)sender).Text);
}
}
For ASP.NET Web Application
string[] myArray = new string[] { "Alex", "Bob", "John", "Srinivas", "Zamal", "Rahul" }
foreach (string item in myArray)
{
HyperLink myHyp = new HyperLink();
myHyp.Text = Suspect;
myHyp.NavigateUrl = "User Details.aspx?name=" + HttpUtility.UrlEncode(item));
myPanel.Controls.Add(new LiteralControl("<ul>"));
myPanel.Controls.Add(new LiteralControl("<li>"));
myPanel.Controls.Add(hpSuspect);
myPanel.Controls.Add(new LiteralControl("</li>"));
myPanel.Controls.Add(new LiteralControl("</ul>"));
}
N.B. LiteralControl is used for adding general HTML controls.

Dynamically create buttons in c# by user at runtime

How to create buttons dynamically after user input in C# (Visual Studio).
There is a text-box to enter how many buttons user wants?
Then my target is to create buttons below the input field as the user wants
then how can I get id's of that buttons?
private void button1_Click(object sender, EventArgs e)
{
List<Button> buttons = new List<Button>();
for (int i = 0; i < n; i++)
{
this.Controls.Add(buttons[i]);
}
}
Here I first added an event handler to the textbox, which is called whenever the text value is changed. The value is converted to the int value and then is used in a for loop statement. You can set your button's potion to the desired value using location property. Using tag or name property you can assign a unique value to your buttons. I hope the code helps.
Look at the code below :
private void Form1_Load(object sender, EventArgs e)
{
textBox1.TextChanged += textBox1_TextChanged;
}
void textBox1_TextChanged(object sender, EventArgs e)
{
var txtBox = sender as TextBox;
if (txtBox == null) return;
var count = Convert.ToInt16(txtBox.Text);
//
var xPosition = 0;
for (var i = 1; i <= count; i++)
{
var button = new Button
{
Tag = string.Format("Btn{0}", i),
Text = string.Format("Button{0}",i),
Location = new Point(xPosition, 0)
};
xPosition = xPosition + 100;
Controls.Add(button);
}
When you are creating Control(in your case Buttons) you can give them Name property. It will be very good if that name will be unique.
var btn = new Button();
btn.Name = "MyBtn";
btn.Text = "Our Button";
this.Controls.Add(btn);
For creation of N buttons you just need to put this in a Loop with N iterations and set btn.Name to something like "Name"+SomeNumber.
To set the Position of the Buttons to below the input you should set btn.Left and btn.Top to the corresponding coordinates.
Then when you need to work with generated Control/Button you can do search by that Name in the following way:
var btn = (Button)this.Controls.Find("MyBtn", true).First();
and do whatever you want with that Control/Button.
But in this case there is some danger as I am not checking if there was found any control with that name. If you write incorrect Name this will throw exception on .First().

Removing TextBoxes created Dynamically on Button click

I have tried creating textboxes dynamically using lists. All i need now is, how can i reset all text boxes that i have created by hitting a reset button.
The following is my code:
public void button2_Click_1(object sender, EventArgs e)
{
int number = Convert.ToInt32(textBox2.Text);
List<TextBox> inputTextBoxes;
inputTextBoxes = new List<TextBox>();
for (int i = 1; i <= number; i++)
{
Label labelInput = new Label();
TextBox textBoxNewInput = new TextBox();
labelInput.Text = "Activity No: " + i;
labelInput.Location = new System.Drawing.Point(30, textBox2.Bottom + (i * 40));
labelInput.AutoSize = true;
textBoxNewInput.Location = new System.Drawing.Point(labelInput.Width+60, labelInput.Top - 3);
inputTextBoxes.Add(textBoxNewInput);
this.Controls.Add(labelInput);
this.Controls.Add(textBoxNewInput);
}
}
The answer is:
private void resetButton_Click(object sender,EventArgs e)
{
for (int i = 0; i <= inputTextBoxes.Length; i++)
{
inputTextBoxes[i].Text = "";
}
}
And you should declare inputTextBoxes is a class member which is same class' of buttons.
Move the following line outside the event handler function (outside the function but inside the class)
List<TextBox> inputTextBoxes;
Then on the reset button click
private void btnReset_Click(object sender, EventArgs e)
{
foreach(TextBox txt in inputTextBoxes)
{
this.Controls.Remove(txt);
}
inputTextBoxes.Clear();
}
Edit: Corrected the class type in foreach loop (from Button to TextBox)

Array of buttons: change property

I have an array of buttons, like this:
int x = 0, y = 0;
butt2 = new Button[100];
for (int i = 0; i < 100; i++)
{
butt2[i] = new Button();
int names = i;
butt2[i].Name = "b2" + names.ToString();
butt2[i].Location = new Point(525 + (x * 31), 70 + (y * 21));
butt2[i].Visible = true;
butt2[i].Size = new Size(30, 20);
butt2[i].Click += new EventHandler(butt2_2_Click); //problem lies here (1)
this.Controls.Add(butt2[i]);
}
private void butt2_2_Click(object sender, EventArgs e)
{
// want code here
}
I want to change the back color of the button when clicked. I was thinking of passing i to be able to do this:
butt2[i].BackColor = Color.Green;
This should do the trick:
private void butt2_2_Click(object sender, EventArgs e)
{
Button pushedBtn = sender as Button;
if(pushedBtn != null)
{
pushedBtn.BackColor = Color.Green;
}
}
And this holds for most UI events, the 'object sender' parameter refers to the control that 'sent'/'fired' the event.
To learn more about C# event handling, I would start here.
Also, here is a SO question about GUI event handling, answered nicely by Juliet (accepted answer).
Hope this helps.

Populate form with controls based on int value

I was wondering how to go about doing something such as this:
I need to create a Form with a specific number of buttons based on an integer value representing the number of buttons needed, then give them their own specific names so that each can have their own unique event handlers.
A real example I can think of doing this would be the Windows log-in screen, where the number of controls created is based on the number of users and whether there is a Guest account or not. How do you think that they programmed that?
Thank you.
for (int i = 0; i < 5; i++)
{
Button newButton = new Button();
newButton.Name = "button" + i.ToString();
newButton.Text = "Button #" + i.ToString();
newButton.Location = new Point(32, i * 32);
newButton.Click += new EventHandler(button1_Click);
this.Controls.Add(newButton);
}
private void button1_Click(object sender, EventArgs e)
{
if (((Button)sender).Name == "button0")
MessageBox.Show("Button 0");
else if (((Button)sender).Name == "button1")
MessageBox.Show("Button 1");
}
Somehow you have to define the names of all the buttons. I would suggest you to create a new string array and write the button names inside, and then use them in buttons creation loop:
//do the same length as the for loop below:
string[] buttonNames = { "button1", "button2", "button3", "button4", "button5" };
for (int i = 0; i < buttonNames.Lenght; i++)
{
Button newButton = new Button();
newButton.Name = "button" + i.ToString();
newButton.Text = buttonNames[i]; //each button will now get its own name from array
newButton.Location = new Point(32, i * 32);
newbutton.Size = new Size(25,100); //maybe you can set different sizes too (especially for X axes)
newButton.Click += new EventHandler(buttons_Click);
this.Controls.Add(newButton);
}
private void buttons_Click(object sender, EventArgs e)
{
Button btn = sender as Button
MessageBox.Show("You clicked button: " + btn.Text + ".");
}

Categories

Resources