Resolved Should be an easy one for anyone advanced (regarding winforms textbox access)

AverageWriter

Member
Joined
Dec 30, 2021
Messages
10
Programming Experience
Beginner
Good afternoon all.
Given that I'm not all that smart, I've found myself stumped again.
So, to condense the project down, at the moment, there are four radio buttons. Each one is set to alter a public variable called "backgroundLayer" to a number from 0 to 4, depending on which button is clicked.

There are four text boxes, each one called BackgroundText 1, 2, 3 and 4.

So normally, to put something in them, one would set, for example, BackgroundText1.Text to a value.

Now, the amount of code that occurs to create the output is rather substantial. To avoid duplicating it multiple times, it would be preferable to use the value of that "backgroundLayer" variable and append it to "BackgroundText" and finally add ".Text", creating the full reference, ie (BackgroundText4.Text).

I've searched through the developer references and scoured Google, but I still can't figure out the best way to do this.
 
Put them in an array get one by index
C#:
TextBox[] boxes = {box1, box2, etc};
boxes[1].Text = "something";
 
Solution
Look at how similar this solution is the solution for your other thread:

 
Look at how similar this solution is the solution for your other thread:


You're absolutely right. I'm still wrapping my head around methods and terminology, getting familiar with how these things work.
 
Back
Top Bottom