使图片框在某个整数上可见/不可见?(C#)

使图片框在某个整数上可见/不可见?(C#),c#,image,visible,invisible,C#,Image,Visible,Invisible,我想知道当某个整数匹配时,如何使图片框变得不可见或可见 我的程序围绕着一个棋盘上的两个玩家,当他们加上两个Di时,他们将移动空间的数量 我的问题是,我的朋友和我都不知道我们现有的代码有什么问题,它不会抛出任何让他困惑的错误,尤其是我自己 我已经做了,所以我的程序在每卷上加上Di,然后加上整数 有人知道怎么了吗?如果没有,更好的方法是什么 代码 private void SelectPos(PictureBox pic) { PictureBox[] numbers = { P1_1, P

我想知道当某个整数匹配时,如何使图片框变得不可见或可见

我的程序围绕着一个棋盘上的两个玩家,当他们加上两个Di时,他们将移动空间的数量

我的问题是,我的朋友和我都不知道我们现有的代码有什么问题,它不会抛出任何让他困惑的错误,尤其是我自己

我已经做了,所以我的程序在每卷上加上Di,然后加上整数

有人知道怎么了吗?如果没有,更好的方法是什么

代码

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;
            }

        }*/
    }
}
private void SelectPos(PictureBox图片)
{
PictureBox[]数字=1{1{P11、1 1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、10、1、1、1、1、1、1、1、1、1、1、1、10、1、1、1、10、1、1、10、1、1、10、1、1、1、1、10、10、1、1、1、10、1、10、10、1、1、10、1、1、1、10、10、1、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
(P1-1一直到P1-49都是图像)

谢谢,
James

看起来您正试图将
int
传递给
SelectPos
函数,但它需要一个
PictureBox
。您可以通过以下类似操作修复此问题:

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);
    }
}
private void SelectPos(int pic)
{
PictureBox[]数字=1{1{P11、1 1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、10、1、1、1、1、1、1、1、1、1、1、1、10、1、1、1、10、1、1、10、1、1、10、1、1、1、1、10、10、1、1、1、10、1、10、10、1、1、10、1、1、1、10、10、1、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
您可能需要操纵
pic
的值,使其在数组的范围内(0-48)。例如,如果
pic
介于1和49之间,则需要减去1:
数字[pic-1]
。在没有看到整个程序的情况下,我无法确切地告诉您该部分代码的外观,但这应该很容易理解。如果您不熟悉数组和索引,请查看链接或只使用谷歌C#数组

作为旁注,最好将
numbers
数组作为该代码所在类的私有成员。除非数组中的值发生更改,否则每次调用该方法时都没有必要构建数组。

完整代码:

        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);
        }
    }
private void SelectPos(int pic)
{
PictureBox[]数字=1{1{P11、1 1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、1、10、1、1、1、1、1、1、1、1、1、1、1、10、1、1、1、10、1、1、10、1、1、10、1、1、1、1、10、10、1、1、1、10、1、10、10、1、1、10、1、1、1、10、10、1、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