Issue
I have a loop that I need to create Entries
dynamically, and with that, I need to setFocus()
on a specifically Entry
, and then, another one, etc.
How can I change the name of variables in runtime to I identify it?
for(i = 0; i < list.Count; i++)
{
//the result that I want:
Entry entry + i = new Entry(){ Placeholder = "Entry number" + i.ToString() };
}
//result
entry1 = /* Placeholder */ "Entry number 1";
entry2 = /* Placeholder */ "Entry number 2";
entry3 = /* Placeholder */ "Entry number 3";
EDIT:
I forgot to put an Entry event that I need to use:
entries[i].Completed += (s, e) =>
{
if (!entries[i].Text.Contains("\u00b2"))
{
entries[i].Text += "\u00b2";
}
};
entries[i].Focus();
when it enters in this event, it can't know how entry that I'm calling, always get the last entry of this array.
Solution
You would use some kind of collection, for example an array:
var entries = new Entry[3];
for(i = 0; i < list.Count; i++)
{
//the result that I want:
entries[i] = new Entry(){ Placeholder = "Entry number" + i.ToString() };
}
//result
entries[0] = /* Placeholder */ "Entry number 1";
entries[1] = /* Placeholder */ "Entry number 2";
entries[2] = /* Placeholder */ "Entry number 3";
To know the called entry you can either cast the sender parameter like Jason suggests, var entry = (Entry)s
. Or capture a variable:
// for loops require copying of the index to a local variable when capturing.
var currentIndex = i;
entries[i].Completed += (s, e) =>
{
if (!entries[currentIndex].Text.Contains("\u00b2"))
{
entries[currentIndex].Text += "\u00b2";
}
};
Variable names mostly exist before compilation, so asking how to change it at runtime is kind of a nonsensical question.
Answered By - JonasH
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.