Making a picture box become visible / invisible on a certain integer? (C#)

490 views Asked by At

I was wondering how I would make a picture box become invisible or visible when a certain integer matches.

My program revolves around 2 players going around a board, and when they add there 2 Di up, they will move the amount of spaces.

My problem being, My friend and I have no idea what is wrong with the current code we have, it throws no errors which baffles him, especially myself.

I've made it so my program add's the Di up on every roll, and add's it to the integer.

Anyone have any idea's on whats wrong? If not, a better approach?

Code

private void SelectPos(PictureBox pic)
{
    PictureBox[] numbers = { P1_1, P1_2, P1_3, P1_4, P1_5, P1_6, P1_7, P1_8, P1_9, P1_10, P1_11, P1_12, P1_13, P1_14, P1_15, P1_16, P1_17, P1_18, P1_19, P1_20, P1_21, P1_22, P1_23, P1_24, P1_25, P1_26, P1_27, P1_28, P1_29, P1_30, P1_31, P1_32, P1_33, P1_34, P1_35, P1_36, P1_37, P1_38, P1_39, P1_40, P1_41, P1_42, P1_43, P1_44, P1_45, P1_46, P1_47, P1_48, P1_49 };
    for (int i = 0; i < numbers.Length; i++)
    {
        if (pic == numbers[i])
        {
            numbers[i].Visible = true;
            MessageBox.Show("k");
        }

        {
            numbers[i].Visible = false;
            MessageBox.Show("l");
        }
    }
}

private void bunifuFlatButton1_Click(object sender, EventArgs e)
{
    Roll();
    System.Threading.Thread.Sleep(100);
    Roll2();
    Goes_Num.Text = (int.Parse(Goes_Num.Text) + 1).ToString();
    if (Convert.ToInt32(Goes_Num.Text) % 2 == 0)
    {
        WhichPlayer.Text = "Player 2";
        P2_Number.Text = (int.Parse(P2_Number.Text) + 1).ToString();
        int p2Int = Convert.ToInt32(P2_Pos.Text);
        P2_Pos.Text = (p2Int + dice + dice2).ToString();
    }
    else if (Convert.ToInt32(Goes_Num.Text) % 2 != 0)
    {
        WhichPlayer.Text = "Player 1";
        P1_Number.Text = (int.Parse(P1_Number.Text) + 1).ToString();
        int p1Int = Convert.ToInt32(P1_Pos.Text);
        P1_Pos.Text = (p1Int + dice + dice2).ToString();
        int P1 = (Convert.ToInt32(P1_Pos.Text));
        SelectPos(P1_1);

        /*switch (P1)
        {
            case 1:
                P1_1.Visible = true;
                break;
            case 2:
                P1_2.Visible = true;
                break;
        }*/

        /*String[] hi = { "1", "2" };
        for (int i = 0; i < hi.Length; i++)
        {
            var visible = p1
            if(visible == hi[i])
            {
                hi[i].Visible = true;
            }
            else
            {
                hi[i].Visible = false;
            }

        }*/
    }
}

(P1-1 all the way to P1-49 are images)

Thanks, James

2

There are 2 answers

0
james ledger On

Complete code:

        private void SelectPos(int pic)
    {
        PictureBox[] numbers = { P1_1, P1_2, P1_3, P1_4, P1_5, P1_6, P1_7, P1_8, P1_9, P1_10, P1_11, P1_12, P1_13, P1_14, P1_15, P1_16, P1_17, P1_18, P1_19, P1_20, P1_21, P1_22, P1_23, P1_24, P1_25, P1_26, P1_27, P1_28, P1_29, P1_30, P1_31, P1_32, P1_33, P1_34, P1_35, P1_36, P1_37, P1_38, P1_39, P1_40, P1_41, P1_42, P1_43, P1_44, P1_45, P1_46, P1_47, P1_48, P1_49 };

        //Set all picture boxes to be not visible
        for (int i = 0; i < numbers.Length; i++)
        {
            numbers[i].Visible = false;
        }

        //Set the picture at the given index to visible
        numbers[pic].Visible = true;
    }

    private void SelectPos2(int pic2)
    {
        PictureBox[] numbers2 = { P2_1, P2_2, P2_3, P2_4, P2_5, P2_6, P2_7, P2_8, P2_9, P2_10, P2_11, P2_12, P2_13, P2_14, P2_15, P2_16, P2_17, P2_18, P2_19, P2_20, P2_21, P2_22, P2_23, P2_24, P2_25, P2_26, P2_27, P2_28, P2_29, P2_30, P2_31, P2_32, P2_33, P2_34, P2_35, P2_36, P2_37, P2_38, P2_39, P2_40, P2_41, P2_42, P2_43, P2_44, P2_45, P2_46, P2_47, P2_48, P2_49 };

        //Set all picture boxes to be not visible
        for (int i = 0; i < numbers2.Length; i++)
        {
            numbers2[i].Visible = false;
        }

        //Set the picture at the given index to visible
        numbers2[pic2].Visible = true;
    }

    private void bunifuFlatButton1_Click(object sender, EventArgs e)
    {
        Roll();
        System.Threading.Thread.Sleep(100);
        Roll2();
        Goes_Num.Text = (int.Parse(Goes_Num.Text) + 1).ToString();
        if (Convert.ToInt32(Goes_Num.Text) % 2 == 0)
        {
            WhichPlayer.Text = "Player 2";
            P2_Number.Text = (int.Parse(P2_Number.Text) + 1).ToString();
            int p2Int = Convert.ToInt32(P2_Pos.Text);
            P2_Pos.Text = (p2Int + dice + dice2).ToString();
            int P2 = (Convert.ToInt32(P2_Pos.Text));
            SelectPos2(P2);
        }
        else if (Convert.ToInt32(Goes_Num.Text) % 2 != 0)
        {
            WhichPlayer.Text = "Player 1";
            P1_Number.Text = (int.Parse(P1_Number.Text) + 1).ToString();
            int p1Int = Convert.ToInt32(P1_Pos.Text);
            P1_Pos.Text = (p1Int + dice + dice2).ToString();
            int P1 = (Convert.ToInt32(P1_Pos.Text));
            SelectPos(P1);
        }
    }
9
amura.cxg On

It looks like you're trying to pass an int to your SelectPos function but it expects a PictureBox. You could fix this doing something similar to the following:

private void SelectPos(int pic)
{
    PictureBox[] numbers = { P1_1, P1_2, P1_3, P1_4, P1_5, P1_6, P1_7, P1_8, P1_9, P1_10, P1_11, P1_12, P1_13, P1_14, P1_15, P1_16, P1_17, P1_18, P1_19, P1_20, P1_21, P1_22, P1_23, P1_24, P1_25, P1_26, P1_27, P1_28, P1_29, P1_30, P1_31, P1_32, P1_33, P1_34, P1_35, P1_36, P1_37, P1_38, P1_39, P1_40, P1_41, P1_42, P1_43, P1_44, P1_45, P1_46, P1_47, P1_48, P1_49 };

    //Set all picture boxes to be not visible
    for (int i = 0; i < numbers.Length; i++)
    {
        numbers[i].Visible = false;
    }

    //Set the picture at the given index to visible
    numbers[pic].Visible = true;
}

private void bunifuFlatButton1_Click(object sender, EventArgs e)
{
    Roll();
    System.Threading.Thread.Sleep(100);
    Roll2();
    Goes_Num.Text = (int.Parse(Goes_Num.Text) + 1).ToString();
    if (Convert.ToInt32(Goes_Num.Text) % 2 == 0)
    {
        WhichPlayer.Text = "Player 2";
        P2_Number.Text = (int.Parse(P2_Number.Text) + 1).ToString();
        int p2Int = Convert.ToInt32(P2_Pos.Text);
        P2_Pos.Text = (p2Int + dice + dice2).ToString();
    }
    else if (Convert.ToInt32(Goes_Num.Text) % 2 != 0)
    {
        WhichPlayer.Text = "Player 1";
        P1_Number.Text = (int.Parse(P1_Number.Text) + 1).ToString();
        int p1Int = Convert.ToInt32(P1_Pos.Text);
        P1_Pos.Text = (p1Int + dice + dice2).ToString();
        int P1 = (Convert.ToInt32(P1_Pos.Text));
        SelectPos(P1);
    }
}

You may have to manipulate the value of pic so that it is within the bounds of the array (0-48). For example if pic is between 1 and 49 you would need to subtract 1: numbers[pic-1]. Without seeing your whole program I can't tell you exactly how that part of the code would look but it should be pretty easy to figure out. If you aren't familiar with arrays and indexing check out this link or just Google C# Arrays.

As a side note it would be better to the numbers array as a private member of the class this code is in. Unless the values in the array change there's no point in building the array every time the method is called.