.NET Framework Developer Center > .NET Development Forums > Windows Presentation Foundation (WPF) > How do I enumerate the controls on a form and take action when I have found the one I want?
Ask a questionAsk a question
 

AnswerHow do I enumerate the controls on a form and take action when I have found the one I want?

  • Saturday, November 07, 2009 11:21 AMk0065126 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    What I want to do is this :-

    for (int i = 0; i < Grid1.Children.Count; i++)
         {
         if (current control's name is 'imageX')
                {
                 do something
                 i = Grid1.Children.Count;
                }
        }

    but I am unable to work out how to accomplish this.

    Viv

Answers

  • Saturday, November 07, 2009 11:33 AMRahul P Nath Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    Hi,
    Here you are checking for the name of the control.In this case you should add a name to the control when you are adding to the grid.

     <Grid Name="Grid1">
            <Button Name="Button1" Height="35" VerticalAlignment="Top" Margin="0,0,65,0" />
            <CheckBox Height="16" Margin="53,86,105,0" Name="checkBox1" VerticalAlignment="Top">CheckBox</CheckBox>
            <ComboBox Height="23" Margin="59,0,99,74" Name="comboBox1" VerticalAlignment="Bottom" />
        </Grid>
    Here i have added some elements to the grid.

     foreach (FrameworkElement  objelement in Grid1.Children)
                {
                    if (objelement.Name == "comboBox1")
                    {
                        MessageBox.Show("Combo Found");  
                    }
                }
    Actually in this case what are you trying to do?

    Hope it helps
    Please mark posts as answers/helpful if it answers your query. This would be helpful for others facing the same kind of problem
    • Marked As Answer byk0065126 Saturday, November 07, 2009 12:01 PM
    •  

All Replies

  • Saturday, November 07, 2009 11:33 AMRahul P Nath Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    Hi,
    Here you are checking for the name of the control.In this case you should add a name to the control when you are adding to the grid.

     <Grid Name="Grid1">
            <Button Name="Button1" Height="35" VerticalAlignment="Top" Margin="0,0,65,0" />
            <CheckBox Height="16" Margin="53,86,105,0" Name="checkBox1" VerticalAlignment="Top">CheckBox</CheckBox>
            <ComboBox Height="23" Margin="59,0,99,74" Name="comboBox1" VerticalAlignment="Bottom" />
        </Grid>
    Here i have added some elements to the grid.

     foreach (FrameworkElement  objelement in Grid1.Children)
                {
                    if (objelement.Name == "comboBox1")
                    {
                        MessageBox.Show("Combo Found");  
                    }
                }
    Actually in this case what are you trying to do?

    Hope it helps
    Please mark posts as answers/helpful if it answers your query. This would be helpful for others facing the same kind of problem
    • Marked As Answer byk0065126 Saturday, November 07, 2009 12:01 PM
    •  
  • Saturday, November 07, 2009 11:35 AMOlaf Rabbachin Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi Viv,

    you'll want to traverse the visual or logical tree. See this thread .


    Cheers,
    Olaf
  • Saturday, November 07, 2009 12:01 PMk0065126 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks Rahul, you have solved my problem.

    Viv
  • Saturday, November 07, 2009 12:06 PMk0065126 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks Olaf, that looks useful and I will try it.

    Viv