Hi,
I used a for-loop to create a number of TextBlocks and add them to a Grid. Later on, in an Event Handler, I want to refer to these TextBlocks and be able to change the text of either some or all, depending on my need.
The problem I'm having is how to refer to the TextBlocks, which were created using one generic name (txtBox) in common for each instance. I tried accessing using the txtBox.Name I set during creation, but that doesn't work.
Would appreciate any suggestions. Thanks!
Loading
Sam HobbsPosted Mar 29, 2010, 5:26 PM
Avtel TelavPosted Mar 29, 2010, 11:55 AM
private void InitializeTunings()
{
for (int i = 0; i < 6; i++)
{
for (int j = 1; j < 13; j++)
{
Button genericButton = new Button();
genericButton.Name = "Button" + j.ToString() + i.ToString();
genericButton.FontSize = 40;
genericButton.VerticalAlignment = VerticalAlignment.Stretch;
genericButton.HorizontalAlignment = HorizontalAlignment.Stretch;
genericButton.Foreground = new SolidColorBrush(Colors.White);
// Put into the List of TextBlocks
ButtonList.Add(genericButton);
THEN, IN A LATER EVENT HANDLER I HAVE THIS:
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 13; j++)
{
Button btnTEST = (Button)this.FindName("Button" + j.ToString() + i.ToString());
btnTEST.Content = "Btn" + j.ToString() + i.ToString();
}
}
THIS RETURNS AN EXCEPTION: "Object reference not set to an instance of an object". Which I take to mean that it did not find any Buttons with that Name. I also tried the name of the Grid parent control that holds the buttons instead of 'this', and any other possible parent control. But this still returned the same error.
Jaish MathewsPosted Mar 29, 2010, 2:59 AM
Need you code to get an assumption.