locked
How to add click event to image in Visual Basic RRS feed

  • Question

  • Hi Programming Professionals,

    I am a novice programmer trying to make a card game. I need to have a click event (in visual basic or simple XAML) on several different card images.

    I have searched the forums for an answer but they all seem to be in C# or very complex XAML.

    Any help gratefully appreciated!!

    Thanks.

    Confused Hobbyist

    Tuesday, January 14, 2014 12:17 AM

Answers

  • The Click event is specific to Buttons. You cannot use it on an Image control, but you can embed your Image control in your button. Using a button is recommended over handling pointer events directly since it will automatically keep all of the semantic information about being a button, activate by keyboard, etc. Otherwise you will need to write quite a bit of framework code to make the image click correctly.

    <Button Click="Clicky">
    <Image x:Name="img1"
                   Source="Cards_PNG/heq.png"
                   HorizontalAlignment="Left"
                   Height="190"
                   Margin="455,118,0,0"
                   VerticalAlignment="Top"
                   Width="140" />
    </Button>

    Depending on specific needs you may want to edit the Button's template to change the borders, customize the button pressed look, etc. The easiest way to do this is to open the template in Blend.

    --Rob

    Tuesday, January 14, 2014 1:26 AM
    Moderator

All replies

  • See the XAML essential controls sample .

    In Xaml, select the control and type "Click=" then <tab> to add the event, or go to the events pane in the Properties tab and add your event handler name there. Either way a skeleton for the event will be added into your code.

    <Button x:Name="myButton" Click="MyClickEvent">Click</Button>

    Tuesday, January 14, 2014 12:31 AM
    Moderator
  • Hi Rob,

    Thanks for your quick response and the code. But my control that I need to use is an Image Control not a Button Control like in your response. Hope I didn't misunderstand your response.

    Here is the XAML code for my Image Control:

    <Image x:Name="img1"
                   Source="Cards_PNG/heq.png"
                   HorizontalAlignment="Left"
                   Height="190"
                   Margin="455,118,0,0"
                   VerticalAlignment="Top"
                   Width="140" />

    Thanks again for your expertise and help.

    Hobbyist



    Tuesday, January 14, 2014 1:02 AM
  • The Click event is specific to Buttons. You cannot use it on an Image control, but you can embed your Image control in your button. Using a button is recommended over handling pointer events directly since it will automatically keep all of the semantic information about being a button, activate by keyboard, etc. Otherwise you will need to write quite a bit of framework code to make the image click correctly.

    <Button Click="Clicky">
    <Image x:Name="img1"
                   Source="Cards_PNG/heq.png"
                   HorizontalAlignment="Left"
                   Height="190"
                   Margin="455,118,0,0"
                   VerticalAlignment="Top"
                   Width="140" />
    </Button>

    Depending on specific needs you may want to edit the Button's template to change the borders, customize the button pressed look, etc. The easiest way to do this is to open the template in Blend.

    --Rob

    Tuesday, January 14, 2014 1:26 AM
    Moderator
  • Hi Rob,

    Thank you very much for your kindness and patience. I appreciate the help!!

    I think this will serve my purpose fine.

    Thanks again

    Hobbyist

    Tuesday, January 14, 2014 2:01 AM