Get Control Height

DeepakDYDeepakDY INMember ✭✭✭

Hello Developer,

I am designing a page and want to round button for this i am using corner radius property but i did't mention button's height and grid height and i want to set corner radius value = button.height / 2 means half value of button height.

   <Grid Grid.Column="1"  ColumnSpacing="10" RowSpacing="10" >
                    <Grid.RowDefinitions>
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                        <ColumnDefinition />
                        <ColumnDefinition />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>

                    <Button Grid.Row="1" Grid.Column="0" x:Name="btn1" Text="1" />
                    <Button Grid.Row="1" Grid.Column="1" x:Name="btn2" Text="2" />
                    <Button Grid.Row="1" Grid.Column="2" x:Name="btn3" Text="3" />
                    <Button Grid.Row="1" Grid.Column="3" x:Name="btnPlus" Text="+" />
                </Grid>

i am trying to get button height from backend but getting height value -1 of button.

this is my backend code

  btn1.CornerRadius = Convert.ToInt32(btn1.HeightRequest / 2);
            btn2.CornerRadius = Convert.ToInt32(btn2.HeightRequest / 2);
            btn3.CornerRadius = Convert.ToInt32(btn3.HeightRequest / 2);
            btnPlus.CornerRadius = Convert.ToInt32(btnPlus.HeightRequest / 2);
            btn0.CornerRadius = Convert.ToInt32(btn0.HeightRequest / 2);

any other option to set corner radius value to half of button height value.

Best Answer

  • ColeXColeX Xamurai
    edited June 14 Accepted Answer

    Yes , you call it in page constructor , the value could be access correctly when in SizeChanged event , otherwise it is -1.

     public Page3()
        {
            InitializeComponent();
    
            btn1.SizeChanged += Btn1_SizeChanged;
            btn2.SizeChanged += Btn1_SizeChanged;
            btn3.SizeChanged += Btn1_SizeChanged;
            btnPlus.SizeChanged += Btn1_SizeChanged;
        }
    
        private void Btn1_SizeChanged(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            btn.CornerRadius = Convert.ToInt32(btn.Height / 2);
        }
    

Answers

  • ColeXColeX Member, Xamarin Team Xamurai
    edited June 14

    You could get width and height in the SizeChanged event , and use Height not HeightRequest

            btn1.SizeChanged += Btn1_SizeChanged;
            btn2.SizeChanged += Btn1_SizeChanged;
            btn3.SizeChanged += Btn1_SizeChanged;
            btnPlus.SizeChanged += Btn1_SizeChanged;
    
        private void Btn1_SizeChanged(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            btn.CornerRadius = Convert.ToInt32(btn.Height/ 2);
        }
    
  • DeepakDYDeepakDY INMember ✭✭✭

    Hello @ColeX

    it is possible without any event or in constructor.

  • ColeXColeX Member, Xamarin Team Xamurai

    where do you want to call the code ?

  • DeepakDYDeepakDY INMember ✭✭✭

    Hello @ColeX

    in content page constructor.

  • ColeXColeX Member, Xamarin Team Xamurai
    edited June 14 Accepted Answer

    Yes , you call it in page constructor , the value could be access correctly when in SizeChanged event , otherwise it is -1.

     public Page3()
        {
            InitializeComponent();
    
            btn1.SizeChanged += Btn1_SizeChanged;
            btn2.SizeChanged += Btn1_SizeChanged;
            btn3.SizeChanged += Btn1_SizeChanged;
            btnPlus.SizeChanged += Btn1_SizeChanged;
        }
    
        private void Btn1_SizeChanged(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            btn.CornerRadius = Convert.ToInt32(btn.Height / 2);
        }
    
  • DeepakDYDeepakDY INMember ✭✭✭

    Hello @ColeX

    Great its working for me :)

Sign In or Register to comment.