Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to develop a aligner interface where i would input a sentence
and it will be tokenized into words. After tokenizing, Every word will be set into
a dynamic toggle buttons and these buttons will be labeled dynamically also.

I would like to change the togglebutton name dynamically where i could change the assignment every execution of the loop.

Example code:
Assuming the name of the togglebuttons are item0,item1,item2,item3.....
And i would like to populate each togglebuttons with each word from the array..

String[] sentenceWord = sourceTxtbox.Text.split(' ');

for(int i=0;i<sentenceword.length)>
{

item+i = sentenceWord[i]; --> error what other means to do this?
}
Posted

Why would you want to change the name? How would you access them next time? That way madness lies...

Set the Text is easy, just set the button.Text property.
Or, you could use the Tag property - it can take an instance of any object.

And if you want to run through a list of buttons, there are two things you want to do:
1) Check that you have enough buttons or you loop will run off the end of them (even if you fix the for loop syntax) if I type a long sentence.
2) Create an array or List of Buttons to hold them, and use an index into that collection. This way you do not need to use a name for each or change it.


"Lets say i would limit the number of words into 10..
I just want to change the name in the loop in runtime..

so that i could store the content of array..
item0.Text = sentenceWord[0];
item1.Text = sentenceWord[1];

the only thing that is changing is the number after the item name..
so that i could assign each of the toggle button a value text..

Is that not possible?"


Not really, no - instead, set them up as an array:
C#
private Button[] items = new Button[10];

Then create them dynamically:
C#
for (int i = 0; i < 10; i++)
   {
   Button b = new Button();
   b.Text = "??";
   items[i] = b;
   Controls.Add(b);
   }
You can them use them pretty much as you describe.

(You will probably want to set the Location, and add a Click handler to each in the loop as well)
 
Share this answer
 
v2
Comments
lore jairus 20-Oct-12 8:02am    
Lets say i would limit the number of words into 10..
I just want to change the name in the loop in runtime..

so that i could store the content of array..
item0.Text = sentenceWord[0];
item1.Text = sentenceWord[1];

the only thing that is changing is the number after the item name..
so that i could assign each of the toggle button a value text..

Is that not possible?
OriginalGriff 20-Oct-12 8:35am    
Answer updated
What you propose above will not do what you want. You need to set the string as the Text[^] property of the control.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900