Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 132,353 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,120 people online right now. Registration is fast and FREE... Join Now!




Picture Box and multiple clicks

2 Pages V  1 2 >  
Reply to this topicStart new topic

Picture Box and multiple clicks, beginner C# game

momentsb4autumn
post 2 Oct, 2008 - 03:05 AM
Post #1


New D.I.C Head

*
Joined: 9 Oct, 2007
Posts: 18


My Contributions


Here is my problem:

I am trying to get this interface to work in this way:

I want to be able to click on one square once and have one picture show, then click on the same square again and have another picture come up, and click it a 3rd....4th....5th time to have other pictures show up. How would I go about doing that with this code? I don't know how to configure the event and event handlers correctly. crazy.gif

My friend suggested using image lists but I don't know enough C# to start that. blink.gif

Also, the images are animated .gif files. How do I implement them into a C# program? blink.gif

Thank you for taking the time to read this. biggrin.gif

Here is my code:

CODE

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 babypushdabutton
{
    public partial class Form1 : Form
    {
         public PictureBox[,] boardgrid;
        public Form1()
        {

            InitializeComponent();
            boardgrid = new PictureBox[5, 5];
            for (int x = 0; x < 5; x++)
            {

                for (int y = 0; y < 5; y++)
                {
                    boardgrid[x, y] = new PictureBox();
                    boardgrid[x, y].Left = x * 47;
                    boardgrid[x, y].Top = y * 47;
                    boardgrid[x, y].Width = 46;
                    boardgrid[x, y].Height = 46;
                    boardgrid[x, y].BackColor = Color.Black;
                    boardgrid[x, y].Tag = "0";
                    boardgrid[x, y].MouseLeave += new System.EventHandler(this.grid_MouseLeave);
                    boardgrid[x, y].MouseHover += new System.EventHandler(this.grid_MouseHover);
                    boardgrid[x, y].Click += new System.EventHandler(this.grid_Click);
                    this.Controls.Add(boardgrid[x, y]);


                }
            }
        }

        private void grid_MouseHover(object sender, EventArgs e)
        {
            PictureBox grid;
            grid = (PictureBox)sender;
            grid.BackColor = Color.IndianRed;
        }

        private void grid_MouseLeave(object sender, EventArgs e)
        {
            PictureBox grid;
            grid = (PictureBox)sender;
            grid.BackColor = Color.Black;
        }

        private void grid_Click(object sender, EventArgs e)
        {

            PictureBox grid;
            grid = (PictureBox)sender;
            grid.BackColor = Color.Red;

        }


    }
}

User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 2 Oct, 2008 - 04:25 AM
Post #2


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,402



Thanked 94 times

Dream Kudos: 2625

Expert In: Dingleberries

My Contributions


One way would be to have a MouseDown event.

Then, inside, have something like this:
csharp
void PictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Clicks == 1) // if the user clicked once
{
// do something
}
// etc
}


Alternatively, and I think this is what you're looking for, you could have a variable, defined in the scope of your class, let's call it int timesClicked = 0; MAKE SURE YOU INITIALISE IT TO ZERO.

Then, in the MouseDown event, have this:
csharp
void PictureBox1_MouseDown(object sender, MouseEventArgs e)
{
timesClicked++; // add one to the number of times clicked
// display a picture, according to how many times the picture box has been clicked
}

Hope this helps smile.gif
User is offlineProfile CardPM

Go to the top of the page

momentsb4autumn
post 2 Oct, 2008 - 04:49 AM
Post #3


New D.I.C Head

*
Joined: 9 Oct, 2007
Posts: 18


My Contributions


Interesting man...

suppose that I wanted to not use pictures but use colors for instance...
would the code look like this? Any of the squares, in this grid, can be clicked on.
Where would the timesclicked be implemented?
Thank you for your assistance...


CODE

void PictureBox_MouseDown(object sender, MouseEventArgs e)
        {
            int timesClicked = 0;
            if (timesClicked == 1) // if the user clicked once  
            {
                PictureBox grid;
                grid = (PictureBox)sender;
                grid.BackColor = Color.Green;
            }
            else if (timesClicked == 2) // if the user clicked twice  
            {
                PictureBox grid;
                grid = (PictureBox)sender;
                grid.BackColor = Color.Yellow;
            }
else if (timesClicked == 3) // if the user clicked three times  
            {
                PictureBox grid;
                grid = (PictureBox)sender;
                grid.BackColor = Color.Blue;
            }  
        }  


This post has been edited by momentsb4autumn: 2 Oct, 2008 - 04:50 AM
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 2 Oct, 2008 - 05:17 AM
Post #4


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,402



Thanked 94 times

Dream Kudos: 2625

Expert In: Dingleberries

My Contributions


Don't put timesClicked in your method, put it in the main class. Otherwise, it's just going to be initialised to 0 every time it's clicked, and your if/else will be useless.
Like this:
csharp
namespace MyProject
{
public partial class MainForm : Form
{
// this is the main scope of the class
int timesClicked = 0;
public MainForm() // the constructor
{
InitializeComponent(); // required for windows designer support
this.pictureBox1.MouseDown += new MouseEventHandler(PictureBox1_MouseDown);
}

void PictureBox1_MouseDown(object sender, MouseEventArgs e)
{
this.timesClicked++; // add 1 to the value of timesClicked
// do the if/else stuff here
}
}
}

Other than that, I think it looks OK.

smile.gif
User is offlineProfile CardPM

Go to the top of the page

momentsb4autumn
post 2 Oct, 2008 - 06:21 AM
Post #5


New D.I.C Head

*
Joined: 9 Oct, 2007
Posts: 18


My Contributions


That's awesome man, it works....BUT....

it only works for one square. Once I go through clicking it 3 times it turns blue on that one square. I leave the square and try to click on another but it doesn't work the same. How do I make it dynamic where I can click on ANY square and go through the 1 2 3 clicks and change the color thing?

Thanks alot man,

happy.gif


CODE

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 babypushdabutton
{
    public partial class Form1 : Form
    {
         public PictureBox[,] boardgrid;
         int timesClicked = 0;
        public Form1()
        {
            InitializeComponent();
            boardgrid = new PictureBox[5, 5];
            for (int x = 0; x < 5; x++)
            {

                for (int y = 0; y < 5; y++)
                {
                    boardgrid[x, y] = new PictureBox();
                    boardgrid[x, y].Left = x * 47;
                    boardgrid[x, y].Top = y * 47;
                    boardgrid[x, y].Width = 46;
                    boardgrid[x, y].Height = 46;
                    boardgrid[x, y].BackColor = Color.Black;
                    boardgrid[x, y].Tag = "0";
                    boardgrid[x, y].MouseLeave += new System.EventHandler(this.grid_MouseLeave);
                    boardgrid[x, y].MouseHover += new System.EventHandler(this.grid_MouseHover);
                    boardgrid[x, y].MouseDown += new MouseEventHandler(PictureBox_MouseDown);
                    boardgrid[x, y].Click += new System.EventHandler(this.grid_Click);
                    this.Controls.Add(boardgrid[x, y]);
                }
            }
        }

        private void grid_MouseHover(object sender, EventArgs e)
        {
            PictureBox grid;
            grid = (PictureBox)sender;
            grid.BackColor = Color.SlateGray;
        }

        private void grid_MouseLeave(object sender, EventArgs e)
        {
            PictureBox grid;
            grid = (PictureBox)sender;
            grid.BackColor = Color.Black;
        }

        private void grid_Click(object sender, EventArgs e)
        {

            PictureBox grid;
            grid = (PictureBox)sender;
         }

        void PictureBox_MouseDown(object sender, MouseEventArgs e)
        {
            this.timesClicked++; // add 1 to the value of timesClicked  
            if (timesClicked == 1) // if the user clicked once  
            {
                PictureBox grid;
                grid = (PictureBox)sender;
                grid.BackColor = Color.Green;
            }
            else if (timesClicked == 2) // if the user clicked twice  
            {
                PictureBox grid;
                grid = (PictureBox)sender;
                grid.BackColor = Color.Yellow;
            }
            else if (timesClicked == 3) // if the user clicked three times
            {
                PictureBox grid;
                grid = (PictureBox)sender;
                grid.BackColor = Color.Blue;
            }
        }  
    }
}
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 2 Oct, 2008 - 07:54 AM
Post #6


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,402



Thanked 94 times

Dream Kudos: 2625

Expert In: Dingleberries

My Contributions


You'd have to have an array of timesClicked which you reference against your board.

Does that make sense?

Additionally, after you've reached 3, you could reassign timesClicked to 0 so that you can continue to change the colour.
User is offlineProfile CardPM

Go to the top of the page

momentsb4autumn
post 3 Oct, 2008 - 09:39 PM
Post #7


New D.I.C Head

*
Joined: 9 Oct, 2007
Posts: 18


My Contributions



blink.gif biggrin.gif crazy.gif

Thank you for your help. Here is my next problem. I don't know how to take an image from my imageList1 and put it into one of the squares. My imageList1 has 3 images in it and I want to take one of them out and put it into the grid.


Also: biggrin.gif
I am now able to click on "one" square and change the colors three times, but I want to be able to dynamically have each square behave differently where one could be blue, and then one could be green, and yet another one can be yellow. This should apply for the entire grid and is dependent of how many times I click on each element of the grid. Any help would be appreciated. Thank you.



CODE

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 babypushdabutton
{
    public partial class Form1 : Form
    {
         public PictureBox[,] boardgrid;
         int timesClicked = 0;
         int PictureBox = 0;
        public Form1()
        {
            InitializeComponent();
            boardgrid = new PictureBox[5, 5];
            for (int x = 0; x < 5; x++)
            {

                for (int y = 0; y < 5; y++)
                {
                    boardgrid[x, y] = new PictureBox();
                    boardgrid[x, y].Left = x * 47;
                    boardgrid[x, y].Top = y * 47;
                    boardgrid[x, y].Width = 46;
                    boardgrid[x, y].Height = 46;
                    boardgrid[x, y].BackColor = Color.Black;
                    boardgrid[x, y].Tag = "0";
                    boardgrid[x, y].MouseLeave += new System.EventHandler(this.grid_MouseLeave);
                    boardgrid[x, y].MouseHover += new System.EventHandler(this.grid_MouseHover);
                    boardgrid[x, y].MouseDown += new MouseEventHandler(PictureBox_MouseDown);
                    boardgrid[x, y].Click += new System.EventHandler(this.grid_Click);
                    this.Controls.Add(boardgrid[x, y]);
                }
            }
        }

        private void grid_MouseHover(object sender, EventArgs e)
        {
            PictureBox grid;
            grid = (PictureBox)sender;
            grid.BackColor = Color.SlateGray;
        }

        private void grid_MouseLeave(object sender, EventArgs e)
        {
            PictureBox grid;
            grid = (PictureBox)sender;
            grid.BackColor = Color.Black;
        }

        private void grid_Click(object sender, EventArgs e)
        {

            PictureBox grid;
            grid = (PictureBox)sender;
         }

        void PictureBox_MouseDown(object sender, MouseEventArgs e)
        {
            this.PictureBox++;
            this.timesClicked++; // add 1 to the value of timesClicked  
            if (timesClicked == 0) // if the user clicked once  
            {
                PictureBox grid;
                grid = (PictureBox)sender;
                imageList1.Tag = 0;
            }
            if (timesClicked == 1) // if the user clicked once  
            {
                PictureBox grid;
                grid = (PictureBox)sender;
                imageList1.Tag = 0;
                grid.Image.Tag = 0;
            }
            else if (timesClicked == 2) // if the user clicked twice  
            {
                PictureBox grid;
                grid = (PictureBox)sender;
                grid.BackColor = Color.Yellow;
            }
            else if (timesClicked == 3) // if the user clicked three times
            {
                PictureBox grid;
                grid = (PictureBox)sender;
                grid.BackColor = Color.Blue;
              
                
            }
            else if (timesClicked == 4) // if the user clicked four times  
            {
                PictureBox grid;
                grid = (PictureBox)sender;
                grid.BackColor = Color.SlateGray;
                if (timesClicked == 4)
                {
                    timesClicked = 0;
                    grid.BackColor = Color.SlateGray;
                }
            }
        }  
        


    }
}
User is offlineProfile CardPM

Go to the top of the page

momentsb4autumn
post 4 Oct, 2008 - 01:47 AM
Post #8


New D.I.C Head

*
Joined: 9 Oct, 2007
Posts: 18


My Contributions


YES! I am able to do what I wanted it to do! biggrin.gif smile.gif biggrin.gif smile.gif tongue.gif biggrin.gif smile.gif

Now, any suggestions on how I make it show images instead of colors? blink.gif Thanks! This is exciting stuff happy.gif



CODE

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 babypushdabutton
{
    public partial class Form1 : Form
    {
         public PictureBox[,] boardgrid;
         int timesClicked = 0;
         int PictureBox = 0;
        public Form1()
        {
            InitializeComponent();
            boardgrid = new PictureBox[5, 5];
            for (int x = 0; x < 5; x++)
            {

                for (int y = 0; y < 5; y++)
                {
                    boardgrid[x, y] = new PictureBox();
                    boardgrid[x, y].Left = x * 47;
                    boardgrid[x, y].Top = y * 47;
                    boardgrid[x, y].Width = 46;
                    boardgrid[x, y].Height = 46;
                    boardgrid[x, y].BackColor = Color.Black;
                    boardgrid[x, y].Tag = "0";
                    //boardgrid[x, y].MouseLeave += new System.EventHandler(this.grid_MouseLeave);
                   // boardgrid[x, y].MouseHover += new System.EventHandler(this.grid_MouseHover);
                    boardgrid[x, y].MouseDown += new MouseEventHandler(PictureBox_MouseDown);
                    boardgrid[x, y].Click += new System.EventHandler(this.grid_Click);
                    this.Controls.Add(boardgrid[x, y]);
                }
            }
        }

        //private void grid_MouseHover(object sender, EventArgs e)
       // {
       //     PictureBox grid;
        //    grid = (PictureBox)sender;
        //    grid.BackColor = Color.SlateGray;
       // }

        //private void grid_MouseLeave(object sender, EventArgs e)
        //{
        //    PictureBox grid;
        //    grid = (PictureBox)sender;
        //    grid.BackColor = Color.Black;
        //}

        private void grid_Click(object sender, EventArgs e)
        {

            PictureBox grid;
            grid = (PictureBox)sender;

         }

        void PictureBox_MouseDown(object sender, MouseEventArgs e)
        {
            
            this.timesClicked++;
            PictureBox grid;// add 1 to the value of timesClicked  
            grid = (PictureBox)sender;
            if (grid.Tag == "0") // if the user clicked once  
            {
                    
                grid.BackColor = Color.White;
                grid.Tag = "1";
              
            }
            else if (grid.Tag == "1") // if the user clicked once  
            {
                
              
                grid.BackColor = Color.Blue;
                grid.Tag = "2";
            }
            else if (grid.Tag == "2") // if the user clicked twice  
            {
              
              
                grid.BackColor = Color.Yellow;
                grid.Tag = "3";
            }
            else if (grid.Tag == "3") // if the user clicked three times
            {
              
                grid.BackColor = Color.Red;
                grid.Tag = "4";
              
                
            }
            else if (grid.Tag == "4") // if the user clicked four times  
            {

                grid.BackColor = Color.SlateGray;
                grid.Tag = "5";
            }
            else if (grid.Tag == "5")
            {
                
                grid.BackColor = Color.Black;
                grid.Tag = "0";
            }
        }  
        


    }
}


User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 4 Oct, 2008 - 03:05 AM
Post #9


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,402



Thanked 94 times

Dream Kudos: 2625

Expert In: Dingleberries

My Contributions


Simple. Change grid.BackColor = blah; to:
grid.Image = new System.Drawing.Bitmap(path);

smile.gif
User is offlineProfile CardPM

Go to the top of the page

momentsb4autumn
post 4 Oct, 2008 - 05:59 AM
Post #10


New D.I.C Head

*
Joined: 9 Oct, 2007
Posts: 18


My Contributions


QUOTE(gabehabe @ 4 Oct, 2008 - 04:05 AM) *

Simple. Change grid.BackColor = blah; to:
grid.Image = new System.Drawing.Bitmap(path);

smile.gif


Hello,

How can I use animaged .gif files or is that not possible?

Thanks ph34r.gif

User is offlineProfile CardPM

Go to the top of the page

gbertoli3
post 4 Oct, 2008 - 07:57 AM
Post #11


DIC at Heart + Code

Group Icon
Joined: 23 Jun, 2008
Posts: 1,026



Thanked 16 times

Dream Kudos: 950
My Contributions


Animated GIFs will work the same way any other image will work only it will be animated.

Hope this helps
User is offlineProfile CardPM

Go to the top of the page

momentsb4autumn
post 6 Oct, 2008 - 07:30 PM
Post #12


New D.I.C Head

*
Joined: 9 Oct, 2007
Posts: 18


My Contributions


QUOTE(gbertoli3 @ 4 Oct, 2008 - 08:57 AM) *

Animated GIFs will work the same way any other image will work only it will be animated.

Hope this helps

ph34r.gif
That's awesome... icon_up.gif

hey, can C# apps show Adobe Flash animations? Just wondering... blink.gif

Thanks cool.gif
ph34r.gif
User is offlineProfile CardPM

Go to the top of the page