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)
Related
I have an action for my button in c# Winform like this:
private void btnAction_Click(object sender, EventArgs e)
{
TextBox tbxdg = new TextBox();
tbxdg.Name = "tbx_DG" + cx.ToString();
tbxdg.Location = new Point(508, 12 + (40 * cx));
tbxdg.Size = new Size(200, 24);
tbxdg.Font = new Font("Tahoma", 10);
panel2.Controls.Add(tbxdg);
cx++;
}
Now I want to get text from the textbox that i've created by clicking my button. I've tried call the textbox by the name that i given to it in the button click action but it's not working.
u can try this:
var textBoxText = panel2.Controls.Find("name of textbox", false).First().Text;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int cx = 0;
private void button1_Click(object sender, EventArgs e)
{
TextBox tbxdg = new TextBox();
tbxdg.Name = "tbx_DG" + cx.ToString();
tbxdg.Location = new Point(0, 0 + (40 * cx));
tbxdg.Size = new Size(200, 24);
tbxdg.Font = new Font("Tahoma", 10);
panel1.Controls.Add(tbxdg);
cx++;
}
private void button2_Click(object sender, EventArgs e)
{
label1.Text = string.Empty;
foreach (TextBox tb in panel1.Controls)
{
label1.Text += $"{tb.Name} - {tb.Text}\n";
}
}
}
Demo
Instead of searching the control name in the panel, another approach is to add all the dynamic text boxes to global List<TextBox>
Please read comments inside the example:
private int cx;
private List<TextBox> DynamicTextBoxesList = new List<TextBox>();
private void btnAction_Click(object sender, EventArgs e)
{
TextBox tbxdg = new TextBox();
tbxdg.Name = "tbx_DG" + cx.ToString();
tbxdg.Location = new Point(508, 12 + (40 * cx));
tbxdg.Size = new Size(200, 24);
tbxdg.Font = new Font("Tahoma", 10);
panel2.Controls.Add(tbxdg);
// add to list
DynamicTextBoxesList.Add(tbxdg);
cx++;
}
// button event for example how to use DynamicTextBoxesList
private void btnExampleFoaccesingTextBoxes_Click(object sender, EventArgs e)
{
if (DynamicTextBoxesList.Count > 0)
{
foreach (TextBox t in DynamicTextBoxesList)
{
MessageBox.Show(t.Text);
}
// or you can find by name for example you need cx=1:
var txtbox = DynamicTextBoxesList.Where(x => x.Name == "tbx_DG1").FirstOrDefault();
if (txtbox != null)
{
MessageBox.Show(txtbox.Text);
}
}
}
so I created my own private void that can create buttons
private void addButtonsToForm()
{
for (int i = 0; i < 26; i++)
{
Button currentNewButton = new Button();
currentNewButton.Size = new Size(20, 30);
currentNewButton.Location = new Point(20 + 25 * i, 420);
currentNewButton.Text = ((char)(65 + i)).ToString();
currentNewButton.Click += LetterClicked;
letters[i] = currentNewButton;
this.Controls.Add(letters[i]);
}
}
The buttons are alphabets and will be accessed when the user wants to choose a letter ... but the problem is I'm trying to figure out how to go back when the user clicked or selected a button..
Originally I wanted to do was i could just hide all the buttons created and just make the previous button visible but for some reason it only hides the only button that is clicked
//this is under private void LetterClicked(object sender, EventArgs e)
Button selectedLetter = (Button)sender;
selectedLetter.Enabled = false;
i thought of stupid codes like
addbuttonstoform().visible = false; but of course that won't work.. but you might get an idea to where i want to go.... it's a bit confusing to explain... I'm new in c# and i'm creating a guess the word game so help could be great..
Here is a working solution for your problem. See below:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Alphabets
{
public partial class Form1 : Form
{
private Button[] _letters;
public Form1()
{
InitializeComponent();
AddButtonsToForm();
}
private void AddButtonsToForm()
{
_letters = new Button[26];
for (int i = 0; i < 26; i++)
{
Button currentNewButton = new Button
{
Name = "BtnLetter"+ ((char)(65 + i)),
Size = new Size(20, 30),
Location = new Point(20 + 25 * i, 420),
Text = ((char) (65 + i)).ToString()
};
currentNewButton.Click += LetterClicked;
_letters[i] = currentNewButton;
this.Controls.Add(_letters[i]);
}
}
private void LetterClicked(object sender, EventArgs e)
{
var selectedLetter = (Button) sender;
//hide all other buttons
foreach (var letter in _letters)
{
if (letter.Text != selectedLetter.Text)
{
var buttons = this.Controls.Find("BtnLetter" + letter.Text, true);
buttons[0].Enabled = false;
}
}
}
}
}
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.
The program has a label, two radio buttons, and a set of generated buttons from A-Z. There are two URLs I would like to use that have a text list of names. When You click a lettered button, the program splits the text list and displays them in the label.
The two radio buttons are supposed to switch the URL being used. One URL has male first names, the other url has female first names. However, I am not sure how to get this to work...as I have it now, choosing the radio button does nothing... it will stay on whatever the first URL was.
using System;
using System.Windows.Forms;
using System.Net;
namespace IndexTable
{
public partial class frmIndex : Form
{
public frmIndex()
{
InitializeComponent();
}
private void Button_Click(object sender, EventArgs e)
{
Button btn = sender as Button; // Convert datatype
label1.Text = btn.Text + "\n"; ;
foreach (string c in name)
{
if (c != "" && c.Substring(0, 1) == btn.Text)
{
label1.Text += c + "\n";
//listBox1.Items.Add(c);
}
}
}
string[] name;
private void Form1_Load(object sender, EventArgs e)
{
int top = 10;
int left = 20;
int width = 30;
for (char i = 'A'; i <= 'M'; i++)
{
Button button = new Button();
button.Left = left;
button.Top = top;
button.Width = width;
button.Text = i.ToString();
this.Controls.Add(button);
button.Click += new System.EventHandler(this.Button_Click);
top += button.Height + 2;
}
left = 50;
top = 10;
for (char i = 'N'; i <= 'Z'; i++)
{
Button button = new Button();
button.Left = left;
button.Top = top;
button.Width = width;
button.Text = i.ToString();
this.Controls.Add(button);
button.Click += new System.EventHandler(this.Button_Click);
top += button.Height + 2;
}
WebClient wc = new WebClient();
string names = wc.DownloadString(url);
name = names.Split('\n');
}
private string url = "http://www.cs.cmu.edu/Groups/AI/areas/nlp/corpora/names/male.txt";
private void btnBoy_CheckedChanged(object sender, EventArgs e)
{
url = "http://scrapmaker.com/data/wordlists/names/male-names.txt";
}
private void btnGirl_CheckedChanged(object sender, EventArgs e)
{
url = "http://scrapmaker.com/data/wordlists/names/female-names.txt";
}
}
}
The CheckedChanged event fires whether the button becomes checked or becomes unchecked. Therefore you need to add a line of code to see what the current check state of the button is before responding:
private void btnBoy_CheckedChanged(object sender, EventArgs e)
{
if (btnBoy.Checked)
url = "http://scrapmaker.com/data/wordlists/names/male-names.txt";
}
EDIT: also you only use the url once, in the form load event, with the line
wc.DownloadString(url)
You'll need to add code to actually use the url again after you change it with the radiobuttons--either in the radiobutton event handler, or the other button event handler for instance.
Im trying to create a array of Checkboxes in Winforms and I have four Checkboxes and if I click on a Checkbox, a messagebox should display the checkboxes checked.
public void checkboxtest()
{
CheckBox[] boxes = new CheckBox[4];
boxes[0] = checkBox1;
boxes[1] = checkBox2;
boxes[2] = checkBox3;
boxes[3] = checkBox4;
for (int i = 0; i <= 4; i++)
{
if (boxes[i].Checked == true && boxes[i].Enabled)
{
MessageBox.Show("boxes[i] is clicked");
}
}
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
checkboxtest();
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
checkboxtest();
}
continues for 3 and 4 too...
How should I go about it ??
Thanks.
Your loop termination should be i < 4, not i <= 4 since your array only has 4 elements. Also boxes[i].Checked == true is redundant, you can just say boxes[i].Checked.
If you want to display the checked checkboxes when you toggle the state, you'll need to add an event handler to them (to handle the CheckBox.CheckChanged event):
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
_checkBoxes = new CheckBox[] { _checkBox1, _checkBox2, _checkBox3, _checkBox4 };
foreach (var checkBox in _checkBoxes)
checkBox.CheckedChanged += new EventHandler(ShowCheckedCheckboxes);
}
void ShowCheckedCheckboxes(object sender, EventArgs e)
{
string message = string.Empty;
for (int i = 0; i < _checkBoxes.Length; i++)
{
if (_checkBoxes[i].Checked && _checkBoxes[i].Enabled)
{
message += string.Format("boxes[{0}] is clicked\n", i);
}
}
MessageBox.Show(message);
}
CheckBox[] _checkBoxes;
}