CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Oct 2010
    Posts
    15

    Problems with IF sectence

    int diceRoll1 = RollDice(min, max);
    System.Threading.Thread.Sleep(5);

    MessageBox.Show("The number rolled was: " + diceRoll1 + ".");

    public int RollDice(int min, int max)
    {
    Random random = new Random();
    return random.Next(1, 7);

    }

    Want to make a IF sectence which does this:

    When i roll i get a value, i want this value to decide what picturebox is going to be set as true. So lets say i have stacked 6 pics on each other. If the return value is 1, i want picture number 1 to be set as true, and the other to be set as false. Which means pic number 1 is the only one to show.

    So basically i want the IF sectence to work like this :

    If return value is 1 - set 1 true


    But the problem is the IF sectence, i don't know how to write the correct code to achieve this.

    I've tried many variations of wrong IF sectences to try to manage this. Help!

    Thanks.

  2. #2
    Join Date
    Dec 2008
    Posts
    144

    Re: Problems with IF sectence

    You don't even need an If conditional to handle this. Just create a List or Array of your picture objects and call the index of the assigned random number.

    I've got some code at my office that does this- I'll post it up later today when I go in.

  3. #3
    Join Date
    Oct 2007
    Posts
    25

    Re: Problems with IF sectence

    CreganTur's solution is the best, but for interest, this would be the other ways to do it:

    Using If's:

    int diceValue = RollDice(1,5);
    if (diceValue == 1)
    {
    set your picture to 1
    }
    elseif (diceValue ==2)
    {
    set your picture to 2
    } elseif (diceValue ==3)
    {
    set picture to 3.
    }
    etc.


    Another way is with "switch".

    switch (RollDice(1,5))
    {
    case 1:
    set picture 1
    break;
    case 2:
    set picture 2
    break;
    etc. etc.
    }

  4. #4
    Join Date
    Jan 2010
    Posts
    1,133

    Re: Problems with IF sectence

    Quote Originally Posted by hyarion View Post
    CreganTur's solution is the best, but for interest, this would be the other ways to do it:

    Using If's:

    int diceValue = RollDice(1,5);
    if (diceValue == 1)
    {
    set your picture to 1
    }
    elseif (diceValue ==2)
    {
    set your picture to 2
    } elseif (diceValue ==3)
    {
    set picture to 3.
    }
    etc.


    Another way is with "switch".

    switch (RollDice(1,5))
    {
    case 1:
    set picture 1
    break;
    case 2:
    set picture 2
    break;
    etc. etc.
    }
    What if it was a simulation of a pen-and-paper RPG where they use a "100-sided" die?

  5. #5
    Join Date
    Dec 2008
    Posts
    144

    Re: Problems with IF sectence

    Quote Originally Posted by TheGreatCthulhu View Post
    What if it was a simulation of a pen-and-paper RPG where they use a "100-sided" die?
    Lists FTW!

  6. #6
    Join Date
    Jan 2010
    Posts
    1,133

    Re: Problems with IF sectence

    Quote Originally Posted by CreganTur View Post
    Lists FTW!
    It was a rhetorical question.

  7. #7
    Join Date
    Jan 2011
    Posts
    6

    Re: Problems with IF sectence

    i thing this will solve your probelm
    _____________________________________

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace picturevisibletrue
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }

    private void btnRollDice_Click(object sender, EventArgs e)
    {
    int diceRoll = RollDice();
    MessageBox.Show("The number rolled was: " + diceRoll + ".");
    selectpict(diceRoll);
    }
    Random objRandom = new Random();
    public int RollDice()
    {
    return objRandom.Next(1, 7);
    }

    public void selectpict(int _diceRoll)
    {
    //using switch instead of IF
    switch (_diceRoll)
    {
    case 1:
    {
    pictureBox1.Visible = true;
    pictureBox2.Visible = false;
    pictureBox3.Visible = false;
    pictureBox4.Visible = false;
    pictureBox5.Visible = false;
    pictureBox6.Visible = false;
    break;
    }
    case 2:
    {
    pictureBox1.Visible = false;
    pictureBox2.Visible = true;
    pictureBox3.Visible = false;
    pictureBox4.Visible = false;
    pictureBox5.Visible = false;
    pictureBox6.Visible = false;
    break;
    }
    case 3:
    {
    pictureBox1.Visible = false;
    pictureBox2.Visible = false;
    pictureBox3.Visible = true;
    pictureBox4.Visible = false;
    pictureBox5.Visible = false;
    pictureBox6.Visible = false;
    break;
    }
    case 4:
    {
    pictureBox1.Visible = false;
    pictureBox2.Visible = false;
    pictureBox3.Visible = false;
    pictureBox4.Visible = true;
    pictureBox5.Visible = false;
    pictureBox6.Visible = false;
    break;
    }
    case 5:
    {
    pictureBox1.Visible = false;
    pictureBox2.Visible = false;
    pictureBox3.Visible = false;
    pictureBox4.Visible = false;
    pictureBox5.Visible = true;
    pictureBox6.Visible = false;
    break;
    }
    case 6:
    {
    pictureBox1.Visible = false;
    pictureBox2.Visible = false;
    pictureBox3.Visible = false;
    pictureBox4.Visible = false;
    pictureBox5.Visible = false;
    pictureBox6.Visible = true;
    break;
    }
    default: break;
    }

    }
    }
    }


    _____________________
    made by me

  8. #8
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Problems with IF sectence

    Wow is that ugly and redundant. At least make it more simple by simply setting the one PictureBox that should be visible in the switch and all others to false. Really though, just use a list and be done with it.

  9. #9
    Join Date
    Jan 2011
    Posts
    6

    Re: Problems with IF sectence

    now its more simple, i made one function to make all false then only to make true that one
    ----------------------

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace picturevisibletrue
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }

    private void btnRollDice_Click(object sender, EventArgs e)
    {
    int diceRoll = RollDice();
    MessageBox.Show("The number rolled was: " + diceRoll + ".");
    selectpict(diceRoll);
    }
    Random objRandom = new Random();
    public int RollDice()
    {
    return objRandom.Next(1, 7);
    }

    public void selectpict(int _diceRoll)
    {
    //using switch instead of IF
    switch (_diceRoll)
    {
    case 1:
    MadeFalseVisiblePict();
    pictureBox1.Visible = true;
    break;
    case 2:
    MadeFalseVisiblePict();
    pictureBox2.Visible = true;
    break;
    case 3:
    MadeFalseVisiblePict();
    pictureBox3.Visible = true;
    break;
    case 4:
    MadeFalseVisiblePict();
    pictureBox4.Visible = true;
    break;
    case 5:
    MadeFalseVisiblePict();
    pictureBox5.Visible = true;
    break;
    case 6:
    MadeFalseVisiblePict();
    pictureBox6.Visible = true;
    break;
    default: break;
    }
    }
    public void MadeFalseVisiblePict()
    {
    pictureBox1.Visible = false;
    pictureBox2.Visible = false;
    pictureBox3.Visible = false;
    pictureBox4.Visible = false;
    pictureBox5.Visible = false;
    pictureBox6.Visible = false;
    }
    }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured