Use foreach for form button controls

manu aloyau 41 Reputation points
2021-09-30T05:36:28.87+00:00

Hello,

I want to loop (foreach) through my image button or labels in a xamarin.form.

as in this example
https://learn.microsoft.com/en-us/answers/questions/463433/use-foreach-for-form-button-controls.html

Thank you
Manu

Developer technologies | .NET | Xamarin
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Anonymous
    2021-09-30T06:51:01.14+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    You can put your Controls to <StackLayout> in the ContentPage.

       <?xml version="1.0" encoding="utf-8" ?>  
       <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
                    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:sho="http://sharpnado.com" xmlns:app141="clr-namespace:App141"  
                    x:Class="App141.MainPage">  
         
         
           <StackLayout>  
               <Button Text="dddd"></Button>  
               <Button Text="ddddg11"></Button>  
         
               <app141:MyEntryHideKeyboard  x:Name="MyEntry"></app141:MyEntryHideKeyboard>  
           </StackLayout>  
         
       </ContentPage>  
    

    Then we can use foreach for form button controls in the backround code.

       public MainPage()  
               {  
                   InitializeComponent();  
         
                   foreach (View i in ((StackLayout)this.Content).Children.Where(x => x.GetType() == typeof(Button))) {  
                       Button button= i as Button;  
                         
                     
                   }  
                     
               }  
    

    Best Regards,

    Leon Lu


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. manu aloyau 41 Reputation points
    2021-10-04T08:17:21.683+00:00

    Hello,
    I hadn't seen your answer, sorry.
    here is my work

    137259-capture-decran-146.png

    Merci pour votre aide
    Manu


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.