简体   繁体   English

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

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

I have a picturebox on an application on Visual Studio 2010 (on Visual C#). 我在Visual Studio 2010(在Visual C#上)的应用程序上有一个图片框。

The picturebox by default has an Image. 图片框默认情况下具有图像。 I want the user to be able to click it and it would change to another Image and then if he clicks it again, the picturebox.Image would change back the first one etc.. 我希望用户能够单击它并将其更改为另一个图像,然后如果他再次单击它,则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;
            }
        }

This is what I have, but it doesn't work. 这是我所拥有的,但是不起作用。 I know there is something wrong with my if statement but I can't figure out what. 我知道我的if语句出了点问题,但我不知道是什么。

Edit: The first image appears on my Form, but when I click it, it doesn't change to the second one. 编辑:第一个图像出现在我的窗体上,但是当我单击它时,它不会更改为第二个图像。

What you should try and rather have is a boolean flag that you toggle on every click. 您应该尝试的是一个布尔标志,您可以在每次单击时进行切换。

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