none
Check if Button have image

    Question

  • Dear All,

    How to check if a button have an image or not?

    i have try this code but not working :

     If IsNothing(CType(child, Button).Image) Then
         CType(child, Button).TextAlign = ContentAlignment.MiddleCenter
    End If

    Best regards,

    Surbakti

    Monday, March 13, 2017 3:25 AM

All replies

  • How to check if a button have an image or not?

    i have try this code but not working :

    What do you mean 'not working'?   Is there an error, or is it not doing what you expected?  If here is an error then post the exact error message. If it's not doing what you expected then you should indicate what you expected to happen, and what actually happened.

    Monday, March 13, 2017 6:23 AM
  • The purpose of that code, if the button not have image, button textAlign set to middleCenter.

    i said not working because all button textAlign set to middleCenter not only button that no image.

    So i want set button that no image to TextAlign MiddleCenter.

    Monday, March 13, 2017 6:34 AM
  • Since MiddleCenter is the default alignment, maybe you need an Else that sets the alignment for other buttons. Or like this:

       Dim button = CType(child, Button)

       button.TextAlign = If(button.Image Is Nothing, ContentAlignment.MiddleCenter, ContentAlignment.MiddleLeft)

    Also make sure that the code is executed when expected.


    Monday, March 13, 2017 7:37 AM
  • The code is working for me when included into:

    Private Sub OriginalCode(ByRef oneButton As Button)
            If IsNothing(CType(oneButton, Button).Image) Then
                CType(oneButton, Button).TextAlign = ContentAlignment.MiddleCenter
            End If
        End Sub

    However, you are using "child"... it looks suspect to me as you may be in another form or something...could you post the rest of the code?

    Monday, March 13, 2017 8:30 AM
  • The purpose of that code, if the button not have image, button textAlign set to middleCenter.

    The code does what is expected - it sets the text alignment to middle-centre if the button has no image.  If that's not working for you then I would consider the context in which you are using it:

    - What exactly does 'child' refer to?
    - how are you checking whether the button has an image or not?
    - What is the current text alignment setting for a button?  If it is already set at middle-centre, then nothing will change.

    Monday, March 13, 2017 9:10 AM