locked
how to roll a dice in windows application using c#

    Question

  • hi, i want to roll a dice .i do like this

      <Button Content="Roll a Dice" Name="button1" HorizontalAlignment="Left" Margin="542,325,0,0" VerticalAlignment="Top" Click="button1_Click"/>
            <TextBlock HorizontalAlignment="Left" Name="textBlock1" Margin="604,507,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top"/>
            <Image HorizontalAlignment="Left" Name="image1" Height="100" Margin="863,391,0,0" VerticalAlignment="Top" Width="100"/>

    in mainpage.cs i write this code

                

     private void button1_Click(object sender, RoutedEventArgs e)
            {


                Random num = new Random();
                int Number = num.Next(1, 7);
                  BitmapImage Img = new BitmapImage(new Uri(@"DiceFaces\" + Number.ToString() + ".png", UriKind.Relative));
                textBlock1.Text = Number.ToString() + " Number";
                 image1.Source = Img;
            }

    and the images named as 1.png,2.png as well .when i press the button "roll a dice " nothing happens.but i want to press a button,roll a dice and the same number appears in a text block like if number appears on a dice like 3,the same number display in a textblock but how can i do like this.kindly help me please .there is an exception occurs in this line 

           BitmapImage Img = new BitmapImage(new Uri(@"DiceFaces\" + Number.ToString() + ".png", UriKind.Relative));
           

    how can i resolve this exception


    Rabia (Microsoft Student Partner)

    Monday, January 26, 2015 8:33 AM

Answers

  • This works for me:

    Random num = new Random();
    int Number = num.Next(1, 7);
    BitmapImage Img = new BitmapImage(new Uri("ms-appx:///DiceFaces/" + Number.ToString() + ".jpg"));
    textBlock1.Text = Number.ToString() + " Number";
    image1.Source = Img;

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Monday, January 26, 2015 3:13 PM
    Moderator

All replies

  • This works for me:

    Random num = new Random();
    int Number = num.Next(1, 7);
    BitmapImage Img = new BitmapImage(new Uri("ms-appx:///DiceFaces/" + Number.ToString() + ".jpg"));
    textBlock1.Text = Number.ToString() + " Number";
    image1.Source = Img;

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Monday, January 26, 2015 3:13 PM
    Moderator
  • thanks alot Sir,it works for me.thankyou so much.

    Rabia (Microsoft Student Partner)

    Tuesday, January 27, 2015 9:45 AM