|
Of course. Here is an alternate use of indirect access to textbox strings. Any suggestions?
static public int boxCount = 0;
static public TextBox[] tbox = new TextBox[16];
private void Form1_Load(object sender, EventArgs e)
{
tbox[0] = textBox1;
tbox[1] = textBox2;
tbox[2] = textBox3;
boxCount = 3;
}
private void loadBoxes()
{
for (int x = 0; x < boxCount; x++)
{
if (tbox[x].Text == "")
tbox[x].Text = "0";
}
}
modified 28-Dec-12 12:43pm.
|
|
|
|
|
No. There's nothing like that in .NET at all. C# is not an interpreted language so it's impossible to evaluate a string at run-time and treat it as code. It has to be compiled in order to work.
You've have to use Reflection to get the control using a string like that and get it's value. Though, you may want to think twice about doing this at all as Reflection isn't exactly speedy to use.
The other problem is that are you seriously calling your controls Textbox1, Textbox2, ...?? That's horrible as you have no idea what each textbox is used for.
|
|
|
|
|
OK. Thanks Dave. That clears it up. The text box values are not of consequence but if they are null it is a problem. Trying to add them to a listbox is rejecting null strings. Position in the list box is critical for each item added so a place holder needs to be added.
|
|
|
|
|
Textbox.Text will never return null, though it can be an empty string. Those terms mean very different things, so don't confuse people (or search engines) by using them incorrectly.
codejuggler9 wrote: Trying to add them to a listbox is rejecting null strings. Position in the list
box is critical for each item added so a place holder needs to be added
Doesn't make any sense to me as it has nothing to do with the original question.
|
|
|
|