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!
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.
My friend suggested using image lists but I don't know enough C# to start that.
Also, the images are animated .gif files. How do I implement them into a C# program?
Thank you for taking the time to read this.
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]);
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 }
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
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 } } }
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,
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]); } } }
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; } } } }
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: 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]); } } }
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; } } }
Now, any suggestions on how I make it show images instead of colors? Thanks! This is exciting stuff
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]); } } }
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 {