I am trying to make number of TextBoxes according to number in TextBox1 and I want to use each TextBox value in my program. Their name becames txtbx0, txtbx1... but when I want to use in my program, it gives error "The name 'txtbx1' does not exist in the current context". How can I use them in my program?
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int y = Convert.ToInt32(textBox1.Text);
TextBox[] txtbx = new TextBox[y];
for (int i = 0; i < y; i++)
{
txtbx[i]= new TextBox();
txtbx[i].Location = new Point(20, i * 50);
txtbx[i].Size = new Size(100,50);
txtbx[i].Name = "txtbx"+i.ToString();
txtbx[i].Text = txtbx[i].Name;
flowLayoutPanel1.Controls.Add(txtbx[i]);
}
}
The way you have it right now, the textboxes could be accessed by using your array txtbx[whateverNumber]. In order to make them accessible outside of the method you posted, you'll need to make your txtbx array a class member instead of a method-scoped variable.
Something like:
class Form1 : Form
{
TextBox[] txtbx;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int y = Convert.ToInt32(textBox1.Text);
txtbx = new TextBox[y]; // Now this references the class member
for (int i = 0; i < y; i++)
... etc.
}
}
Accessing them individually by name is not really feasible because you'd have to have class member variables for each of them, but you don't know up front how many to make. The array method like you're doing is much better. You can just access them in other methods using txtbx[0] through txtbx[numBoxes - 1].
Related
this question might be really stupid but here it is anyway. What I want my programm to do: When I press a button I want to add a DatePicker Component to a List and then display all the Components in the Main Form. However when I press the button it only adds the components but doesnt show them in the Form Window. No Errors are thrown. What do I have to do to display the DatePicker Components in the Main Form?
//class containing the List of Components
class Eintrag
{
static public List<DateTimePicker> Anfangszeit = new List<DateTimePicker>();
}
//Main Form Class
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Eintrag.Anfangszeit.Add(new DateTimePicker());
for (int i = 0; i < Eintrag.Anfangszeit.Count; i++)
{
Eintrag.Anfangszeit[i].Location = new System.Drawing.Point(30, 50 + 50*i);
Eintrag.Anfangszeit[i].Size = new System.Drawing.Size(200, 20);
Eintrag.Anfangszeit[i].Visible = true;
Eintrag.Anfangszeit[i].Show();
}
}
}
John Wu is right, you have to add the Controls to the Form via Controls.Add()
private void button1_Click(object sender, EventArgs e)
{
Eintrag.Anfangszeit.Add(new DateTimePicker());
for (int i = 0; i < Eintrag.Anfangszeit.Count; i++) {
Eintrag.Anfangszeit[i].Location = new System.Drawing.Point(30, 50 + 50 * i);
Eintrag.Anfangszeit[i].Size = new System.Drawing.Size(200, 20);
Eintrag.Anfangszeit[i].Visible = true;
this.Controls.Add(Eintrag.Anfangszeit[i]);
Eintrag.Anfangszeit[i].Show();
}
}
I'm making a simple calculator to save myself from having to add up a bunch of invoice totals on basic calculator at work (since we use paper invoices). I'm just getting the basic functionalities in place currently and one of the functions I have is adding extra textboxes and then later trying to add all of the values in the textboxes to a subtotal. The problem I am having (I think) is that the compiler needs to have the the textbox ID's before the program is compiled. I'm also going to apologize for the sloppy variable names, I tried everything I could think of in my basic mindset until 4AM and by the time I was just using any variable. I've tried all the iterations, (ended up with do while statement as you can see).
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
int partboxes = 3;
int lastY = 83;
public Form1()
{
InitializeComponent();
}
private void buttonFinal_Click(object sender, EventArgs e)
{
AddParts();
}
public double AddParts()
{
int i = 1;
double megavalue = 0;
do
{
double totalsum = double.Parse(("partBox" + i).Text);
megavalue = totalsum + megavalue;
i = i + 1;
} while (i < partboxes);
string supervalue = megavalue.ToString();
lblPartsTotal.Text = supervalue;
return megavalue;
}
private void button1_Click(object sender, EventArgs e)
{
TextBox partBox = new TextBox();
partBox.Name = "partBox" + partboxes++;
partBox.Location = new System.Drawing.Point(12, lastY + 26);
partBox.Size = new System.Drawing.Size(100, 20);
// Add the textbox control to the form's control collection
this.Controls.Add(partBox);
lastY = lastY + 26;
partboxes = partboxes++;
}
}
}
You can get a textbox by name from the Controls property with
var tb = (TextBox)Controls["partBox" + i];
where i is the textbox number.
The indexer returns a Control. But since every control has a Text property, you can use it without casting.
double totalsum = double.Parse(Controls["partBox" + i].Text);
See: Control.ControlCollection Class
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.
I'm trying to create a mathematical game with a timer that calculates the number of correct questions within a specific time. Now I'm trying to increment an int value per button click if the answer is correct.
But it only increment once and sometimes does not increment:
private void button1_Click(object sender, EventArgs e)
{
int x = Randomnumber.Next(12);
int z = Randomnumber.Next(12);
int s = x * z;
int correct = 0;
//int cv = +correct;
textBox2.Text = x.ToString();
textBox3.Text = z.ToString();
if (s == Convert.ToInt32(textBox4.Text))
{
correct += 1;
numbercorrect.Text = correct.ToString();
}
}
your main form(i'm assuming you're using forms) is a class.
What I'd suggest is declaring a variable as a member of your forms class, and using that to hold the number of correct responses.
I'd imagine something like the following;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
int correct;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//insert logic here
correct++;
}
}
}
You need to move int correct declaration to class scope. Otherwise with every click, you start with new variable.
int correct = 0; is scoped within the function. Move it outside the function as a class field. That way it will preserve its value instead of being reset to 0 during each click.
Try this:
private int correct = 0;
private void button1_Click(object sender, EventArgs e)
{
int x = Randomnumber.Next(12);
int z = Randomnumber.Next(12);
int s = x * z;
//int cv = +correct;
textBox2.Text = x.ToString();
textBox3.Text = z.ToString();
if (s == Convert.ToInt32(textBox4.Text))
{
correct ++;
numbercorrect.Text = correct.ToString();
}
You always start your count with 0, and never get the original value.
Now the variable holding the data is outside the function and initialized when the form loads.
Every time the button is clicked, correct is beging reset to zero.
Try declaring correct outside of the method.
Try look at code bellow:
int correct = 0;
tryParse(numbercorrect.Text, out correct);
So your code must be like:
private void button1_Click(object sender, EventArgs e)
{
int x = Randomnumber.Next(12);
int z = Randomnumber.Next(12);
int s = x * z;
int correct = 0;
int.tryParse(numbercorrect.Text, out correct);
//int cv = +correct;
textBox2.Text = x.ToString();
textBox3.Text = z.ToString();
if (s == Convert.ToInt32(textBox4.Text))
{
correct += 1;
numbercorrect.Text = correct.ToString();
}
Having hard time understanding classes and why I can't access certain object.
How can i modify the code so I can change "map"(which is a bunch of labels) properties in all of my classes/events?
The method Draw2d() creates a couple of labels on the main form that I wish to change on different events(button click in this example).
Can someone help me, or just hint me into the right direction.
The Code:
public partial class Form1 : Form
{
public void Draw2d()
{
const int spacing = 20;
Label[][] map = new Label[5][];
for (int x = 0; x < 5; x++)
{
map[x] = new Label[5];
for (int y = 0; y < 5; y++)
{
map[x][y] = new Label();
map[x][y].AutoSize = true;
map[x][y].Location = new System.Drawing.Point(x * spacing, y * spacing);
map[x][y].Name = "map" + x.ToString() + "," + y.ToString();
map[x][y].Size = new System.Drawing.Size(spacing, spacing);
map[x][y].TabIndex = 0;
map[x][y].Text = "0";
}
this.Controls.AddRange(map[x]);
}
}
public Form1()
{
InitializeComponent();
}
public void Form1_Load(object sender, EventArgs e)
{
Draw2d();
}
private void button1_Click(object sender, EventArgs e)
{
map[0][0].Text = "1"; // <-- Doesn't work
}
}
Thanks!
you have to declare the map as property(global to class)
public partial class Form1 : Form {
public Label[][] map;
....
}
then you can use inside class like
this->map[...][...]
or from outside like
objClass->map[...][...]
My guess is that you added
public Label[][] map;
but forgot to change the second line of Draw2d from
Label[][] map = new Label[5][];
to
map = new Label[5][];
I just tried your code, and it works fine if you change those two lines. If that's not the problem, could you say what error you're getting, please?