繁体   English   中英

Visual C#无法更改picturebox.click上的图像

[英]Visual C# cannot change picturebox.Image on click

我在Visual Studio 2010(在Visual C#上)的应用程序上有一个图片框。

图片框默认情况下具有图像。 我希望用户能够单击它并将其更改为另一个图像,然后如果他再次单击它,则picturebox.Image将变回第一个图像,依此类推。

 private void pictureBox1_Click(object sender, EventArgs e)
        {
            if (pictureBox1.Image == WindowsFormsApplication2.Properties.Resources.English_flag)
            {
                pictureBox1.Image = WindowsFormsApplication2.Properties.Resources.Greek_flag;
            }
            else
            {
                pictureBox1.Image = WindowsFormsApplication2.Properties.Resources.English_flag;
            }
        }

这是我所拥有的,但是不起作用。 我知道我的if语句出了点问题,但我不知道是什么。

编辑:第一个图像出现在我的窗体上,但是当我单击它时,它不会更改为第二个图像。

您应该尝试的是一个布尔标志,您可以在每次单击时进行切换。

bool flag = true;
private void pictureBox1_Click(object sender, EventArgs e)
    {
            pictureBox1.Image = flag ? WindowsFormsApplication2.Properties.Resources.Greek_flag 
                                     : WindowsFormsApplication2.Properties.Resources.English_flag;
        flag = !flag;

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

            private Image first;
            private Image reitmi;
            private Image reitmi2;
            private Image selectForCancel;

 private void Form2_Load(object sender, EventArgs e)
            {

                first = Properties.Resources.Open;
                reitmi = Properties.Resources.Select;
                reitmi2 = Properties.Resources.Reserve;



                pictureBox1.Image = Properties.Resources.Open;

    }

private void pictureBox2_Click(object sender, EventArgs e)
        {    
        if (pictureBox2.Image == first)
                {
                    pictureBox2.Image = reitmi;

                    listHoldingSeats.Items.Add("B1");
                    txtListCount.Text =listHoldingSeats.Items.Count.ToString();

                }


                else
                {
                    pictureBox2.Image = first;
                    listCancelledList.Items.Add("B1");
                } 


        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM