Reputation: 6802
I'm currently working on an Windows Phone 7 App
I have at least 9 Textfields with the name "TEXTFIELD_ID_1" , "TEXTFIELD_ID_2"
etc...
There will be more.
I want to access every of these fields with a loop:
if (currentpage == 1)
{
for (int i = 1; i<10; i++)
{
string[] beitrag = result.Split(new string[] { "<split_inner>" }, StringSplitOptions.None);
profile_img_1.Source = (ImageSource)new ImageSourceConverter().ConvertFromString("EXAMPLE.jpg);
}
}
As you can see I access Image one using the direct way (1). How can I use my variable "i" to access the image / textfield ?
Thx
Upvotes: 0
Views: 122
Reputation: 101701
If names are regular like that then just use:
string name = "TEXTFIELD_ID_" + i;
this.Controls[name].Text = "bla bla bla...";
The key point is the ControlCollection
. this
refers to your Form
.You can access a Form Control with it's name
or index
.
Edit: In WP7 you must have a container like a Grid
. For example if you have a grid control named LayoutRoot
, you can access your elements with Children
property.If you want loop through your TextBoxes you can use OfType
extension method:
var elements = LayoutRoot.Children;
foreach (var element in elements.OfType<TextBox>())
{
var currentTextBox = element as TextBox;
...
}
Upvotes: 2
Reputation: 6366
You can use FindControl method in your loop:
if (currentpage == 1)
{
for(int i = 1; i<10; i++)
{
string[] beitrag = result.Split(new string[] { "<split_inner>" }, StringSplitOptions.None);
Control imgControl = FindControl(string.Format("profile_img_{0}", i));
imgControl.Source = (ImageSource)new ImageSourceConverter()
.ConvertFromString("EXAMPLE.jpg);
}
}
Upvotes: 0