Is there a simple, programmatic way of getting a reference to an object via the name of the object?
For example, if one created a grid of, say, 6 Textboxes in the Design Window, just wondering if there is a better way simple programmatic way of assigning an array of references to the textboxes?
i.e. a better way than hard-coding like this:
TextBox[] tb = new TextBox[6];
tb[0] = textBox1;
tb[1] = textBox2;
tb[2] = textBox3;
tb[3] = textBox4;
tb[4] = textBox5;
tb[5] = textBox6;
Could one create a for loop with a string of the textbox's name, i.e. string.Format("textBox{0}",i+1) ?
But how can one assign that particular textbox by its name?
Loading
Hirendra SisodiyaPosted Apr 1, 2010, 2:44 AM
you can try this Tj:
TextBox temp_textBox = (TextBox) (this.Controls.Find(string.Format("textBox{0}",i+1), false))[0];
or you can also use this in this way :
(TextBox) (this.Controls.Find(string.Format("textBox{0}",i+1), false))[0].Text = "test";
but make sure that textbox should have unique name and key.
thanks
Please mark as answere if it helps
Sam HobbsPosted Apr 1, 2010, 7:54 PM
TjPosted Apr 1, 2010, 3:33 PM
Look, mate, I'm the new guy here. I'm treading very carefully and cautiously, taking my first fledgling steps into a new Web Forum - but I am having to try very hard not to take offence at your increasingly provocative, hostile and high-handed retorts.
Now maybe it's a simple misunderstanding? An Anglo-American, linguistic difference? I don't know.
But you seem to find the very first sentence - "Is there a simple, programmatic way of getting a reference to an object via the name of the object?" - unclear and ambiguous?
Well I'm afraid that, looking back at it, I don't. It makes perfect sense to me.
Now either you're being deliberately rude or - worse still - you don't even realise you are?! (I've taken several steps to remove the heat from the situation, but instead you keep trying to stoke things up.)
Please just calm down... I thought we were all on here, trying to help each other out?
Sam HobbsPosted Apr 1, 2010, 2:18 PM
Sam HobbsPosted Mar 31, 2010, 7:09 PM
If anyone expects someone requesting assistance to accept a suggestion that does not satisfy requirements, then that person should not be offering assistance. I know I get real frustrated when I ask a question and someone insists that I should do it their way yet their way does not help. On the other hand, if the requirements are not adequately described, then it is unlikely the suggestions will help.
Your question is very similar to a question a couple days ago. I suggested a solution that is a better way than what the person was doing. I am not sure that solution will help you but as best as I can guess it will. You can use the Tag property of a control to efficiently access data specific to the application. I get the impression that that is what you need.
The terms solution and approach are essentially synonymous.
TjPosted Mar 31, 2010, 1:25 PM
But, no, I wasn't describing "the solution"; I was merely describing an approach which would obviously work, but one that would get more and more tedious the more entries that were added to the array - 'There has to be a better way(?)!'
But if one distils a problem down to what one thinks is the crux of the problem, and then asks for help (without mentioning the specific context), one often comes away with the perfect solution... to an entirely different problem!
;O)
Sam HobbsPosted Mar 31, 2010, 12:19 PM
TjPosted Mar 31, 2010, 10:07 AM
However, I've managed to work the problem out myself by looking at the Form Designer Code:
Taking a similar situation with a 13x13 grid of ComboBoxes (i.e. 169)
for (int i = 0; i < 168; i++)
{
string comboBoxName = string.Format("comboBox{0}", i + 1);
int comboBoxIndex = this.panel1.Controls.IndexOfKey(comboBoxName);
cb[i] = (ComboBox)this.panel1.Controls[comboBoxIndex ];
}
Hirendra SisodiyaPosted Mar 31, 2010, 9:05 AM
i think we can not do it in this way ..
you can use Tb[i] in for loop...simple...,
Thanks
Please mark as answere if it helps