2

I was wondering if somebody could help me improve my code (?)

Three weeks ago I decided that it would be very useful to learn C#/C++ (decided to start with c#) and I'm doing my best, but I have problems with understanding some basics- for example arrays.

I would like to add "x" textboxes (where "x" is the value form numericUpDown) with a button click.

I found a solution how to do this, but I have this feeling that it is possible to write this in a different (better) way ( I assume advanced programers would use lists or arrays).

Please forgive me if I'm wrong, as I mentioned before- I'm new and doing my best to learn.

Here is my code:

private void button1_Click(object sender, EventArgs e) { if (numericUpDown1.Value == 1) { txtbx1.AutoSize = true; Controls.Add(txtbx1); txtbx1.Location = new Point(70, 100); } else if (numericUpDown1.Value == 2) { txtbx1.AutoSize = true; Controls.Add(txtbx1); txtbx1.Location = new Point(70, 100); txtbx2.AutoSize = true; Controls.Add(txtbx2); txtbx2.Location = new Point(70, 130); } else if (numericUpDown1.Value == 3) { txtbx1.AutoSize = true; Controls.Add(txtbx1); txtbx1.Location = new Point(70, 100); txtbx2.AutoSize = true; Controls.Add(txtbx2); txtbx2.Location = new Point(70, 130); txtx3.AutoSize = true; Controls.Add(txtbx3); txtbx3.Location = new Point(70, 160); } } 
4
  • 9
    this question would be more appropriate on board code review codereview.stackexchange.com Commented Aug 2, 2013 at 20:32
  • take a look at switch statements Commented Aug 2, 2013 at 20:33
  • From the StackOverflow "About" page: "Not all questions work well in our format. Avoid questions that are primarily opinion-based, or that are likely to generate discussion rather than answers.". A question in this format is very subjective. It doesn't have any problems per se, and if it was more complicated, there would be many more opinions on how to accomplish what you are trying to do. That's why @Chips_100 suggested going to a different site. I actually didn't know about codereview.stackeschange.com!! I'm excited to have learned about it just now. Just an FYI for the future. Commented Aug 2, 2013 at 20:40
  • This question appears to be off-topic because it belongs on Code Review. Commented Aug 3, 2013 at 3:51

3 Answers 3

6

Don't repeat yourself, in a simple way you can do like this:

private void button1_Click(object sender, EventArgs e) { int y = 100; int x = 70; for (int i = 0; i < numericUpDown1.Value; i++) { var txtbx = new TextBox(); txtbx.AutoSize = true; Controls.Add(txtbx); txtbx.Location = new Point(x, y); // Increase the y-position for next textbox. y += 30; } } 
Sign up to request clarification or add additional context in comments.

Comments

5

Instead of pre-creating the TextBox controls, you could always create them on the fly:

// This is optional - in case you want to save these for use later. List<TextBox> newTextBoxes = new List<TextBox>(); private void button1_Click(object sender, EventArgs e) { int y = 100; for (int i=0;i<numericUpDown1.Value;++i) { TextBox newBox = new TextBox { AutoSize = true, Location = new Point(70, y) }; y += 30; Controls.Add(newBox); // This saves these for later, if required newTextBoxes.Add(newBox); } } 

Comments

0

You could do one of two things. The first is to use a switch statement, or my preference is to use a loop, though this can get a little tricky when using a gui. Something like this:

for (int i = 0; i < numericUpDown1.Value; i++) { //Make an TextBox Array or instantiate a new TextBox each iteration //and set properties here } 

But as I said, the placement of these text boxes can get a little tricky if you do it in a loop, so it would be easier to just use the switch statement. They are relatively straightforward to use, but let us know if you run into any problems.

Also, a note on your code, give the text boxes and other GUI elements meaningful names; I know the designer automatically assigns them names and it's not a problem now, but it will become extremely confusing when working with many elements.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.