Reputation: 1027
I have a form which consist of many buttons (50+) and they all have the same name except for the suffix number. (btn_0
, btn_1
, btn_3
, etc.)
I want to change the text of those buttons in one operation.
Is there a way of treating buttons like arrays?
btn_[i].Text = "something"?
Maybe execute a string?
"btn_{0}.Text=\"something\""
Upvotes: 2
Views: 3094
Reputation: 18420
you will need to access each button at a time to do this.
Do it in a loop like this
foreach(var btn in this.Controls)
{
Button tmpbtn;
try
{
tmpbtn = (Button) btn;
}
catch(InvalidCastException e)
{
//perform required exception handelling if any.
}
if(tmpbtn != null)
{
if(string.Compare(tmpbtn.Name,0,"btn_",0,4) == 0)
{
tmpbtn.Text = "Somthing"; //Place your text here
}
}
}
Have a look for the Overloaded Compare method used.
Upvotes: 4
Reputation: 3562
Don't know specifics but the pattern probably goes like this
for each(Control c in this.controls)
{
if(c is Button) //Check the type
{
Button b = c as button;
b.Text="new text";
}
}
or use excel with its autofil and text concatenation abilities to do it as a block of text. eg
btn1.text="hi";
btn2.text="world";
...
Upvotes: 1
Reputation: 2964
why not use jquery to rename all at once?
jQuery("form :button").attr('value','Saved!')
Upvotes: -1
Reputation: 119
if you know how many buttons there is you can make a loop. though it's not perfect and there might be a smarter way to do this but I can't see why I wouldn't work
Upvotes: 1