Windows Develop Bookmark and Share   
 index > Windows Forms General > General Question regarding runtime event programming
 

General Question regarding runtime event programming

I am writing a card game program, I have a class called Card:PictureBox, and a class called Deck:Card
In deck I have the following declaration:
private Card [] cards = new Card[52];

and in card I have

public

Card()

{

Bitmap bp = new Bitmap(75,102);

pb.Image = bp;

pb.MouseMove +=

new System.Windows.Forms.MouseEventHandler(this.MoveCard);

pb.MouseDown +=

new System.Windows.Forms.MouseEventHandler(this.CardMouseDown);

pb.MouseUp +=

new System.Windows.Forms.MouseEventHandler(this.CardMouseUp);

}
the event handlers MoveCard, CardMouseDown, and CardMouseUp are also in card.
Now when I display the card on the form, the events for the card never seem to fire. The question is why not. It there something I am doing wrong?

OldProgrammer123456  Saturday, September 19, 2009 5:21 PM
I found the problem, I think. The problem is in your Card class. The class itself is oftype PictureBox, and yet you have a PictureBox member also in the class. You've set the Image and theevent handlerson this PictureBox instead of on the class itself. So, I believe your Card class should look like this instead (I removed the PictureBox member):

class Card : PictureBox
{
	private bool isDragging = false;
	private int currentX, currentY;
	private int formwidth;
	private int formheight;
	private Bitmap bp;

	public int FormWidth { set { formwidth = value; } }
	public int FormHeight { set { formheight = value; } }
	public Bitmap BP { set { bp = value; } }

	public Card()
	{
		Bitmap bp = new Bitmap(75, 102);
		this.Image = bp;
		this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.MoveCard);
		this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.CardMouseDown);
		this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.CardMouseUp);
	}

	private void MoveCard(object sender, MouseEventArgs e)
	{
		int x, y;
		if (isDragging)
		{
			y = ((PictureBox)sender).Top + (e.Y - currentY);
			x = ((PictureBox)sender).Left + (e.X - currentX);

			if (x < 1) { x = 1; }
			if (x > Parent.Width - 85) { x = Parent.Width - 85; }
			if (y < 1) { y = 1; }
			if (y > Parent.Height - 130) { y = Parent.Height - 130; }

			((PictureBox)sender).Top = y;
			((PictureBox)sender).Left = x;
		}
	}
	private void CardMouseDown(object sender, MouseEventArgs e)
	{
		isDragging = true;
		currentX = e.X;
		currentY = e.Y;
		//Visible = false; // this is here to see if the event is being fired, it will come out later 
	}
	private void CardMouseUp(object sender, MouseEventArgs e)
	{
		isDragging = false;
		//Visible = true; //This is here to see if the event is being fired, it will come out later. 
	}
}


~~Bonnie Berent [C# MVP]

(new blog --- not many posts yet --- be patient)
geek-goddess-bonnie.blogspot.com
BonnieB  Monday, September 21, 2009 6:18 PM
You might need to post a little more code.
~~Bonnie Berent [C# MVP]

(new blog --- not many posts yet --- be patient)
geek-goddess-bonnie.blogspot.com
BonnieB  Monday, September 21, 2009 4:24 AM
Hello,

I guess pb is PictureBox control, one pb holds 1 card. When you drag it (Mouse down, move, then mouse up), the pb’s location will be changed. Do I understanding correct?

I think the MouseDown event is fired but the code is not correct so you cannot see the card being moved. Could you please post enough code?

You can put a MessageBox in the MouseDown event handled method (CardMouseDown) to see if my analysis is correct. Please feel free to tell me if I misunderstood you.

Sincerely,
Kira Qian
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework!
Kira Qian  Monday, September 21, 2009 9:55 AM
Here is all the code.
In the main Form:

Deck d = new Deck();

d.XPosition = 10;

d.YPosition = 10;

d.ShowCard(

this,0);

In Card

class Card : PictureBox

{

private bool isDragging = false;

private int currentX, currentY;

private int formwidth;

private int formheight;

private PictureBox pb = new PictureBox();

private Bitmap bp;

public int FormWidth { set { formwidth = value; } }

public int FormHeight { set { formheight = value; } }

public Bitmap BP { set { bp = value; } }

public Card()

{

Bitmap bp = new Bitmap(75,102);

pb.Image = bp;

pb.MouseMove +=

new System.Windows.Forms.MouseEventHandler(this.MoveCard);

pb.MouseDown +=

new System.Windows.Forms.MouseEventHandler(this.CardMouseDown);

pb.MouseUp +=

new System.Windows.Forms.MouseEventHandler(this.CardMouseUp);

}

private void MoveCard(object sender, MouseEventArgs e)

{

int x, y;

if (isDragging)

{

y = ((

PictureBox)sender).Top + (e.Y - currentY);

x = ((

PictureBox)sender).Left + (e.X - currentX);

if (x < 1) { x = 1; }

if (x > Parent.Width - 85) { x = Parent.Width - 85; }

if (y < 1) { y = 1; }

if (y > Parent.Height - 130) { y = Parent.Height - 130; }

((

PictureBox)sender).Top = y;

((

PictureBox)sender).Left = x;

}

}

private void CardMouseDown(object sender, MouseEventArgs e)

{

isDragging =

true;

currentX = e.X;

currentY = e.Y;

Visible =

false; // this is here to see if the event is being fired, it will come out later

}

private void CardMouseUp(object sender, MouseEventArgs e)

{

isDragging =

false;

Visible =

true; //This is here to see if the event is being fired, it will come out later.

}

}

in Deck

class Deck

{

private Card [] cards = new Card[52];

private int xposition;

private int yposition;

public PictureBox[] Cards { get { return cards; } }

public int XPosition { set { xposition = value; } }

public int YPosition { set { yposition = value; } }

public Deck()

{

InitializeCards();

GenerateCards();

}

private void InitializeCards()

{

int x;

for (x = 0; x < 52; x++)

{

cards[x] =

new Card();

}

}

private void GenerateCards()

{

Bitmap bmp = new Bitmap("cards.jpg");

int i, j, x, y, x1=0, y1=0;

int cardcount = 0;

Color currentcolor = new Color();

for (j = 0; j < 4; j++)

{

for (i = 0; i < 13; i++)

{

Bitmap card = new Bitmap(75, 102);

for (x = i * 75, x1 = 0; x < (i * 75) + 75; x++, x1++)

{

for (y = j * 102, y1 = 0; y < (j * 102) + 102; y++, y1++)

{

currentcolor = bmp.GetPixel(x, y);

card.SetPixel(x1, y1, currentcolor);

}

}

cards[cardcount].BP = card;

cardcount++;

}

}

}

public void ShowCard(Form P,int c)

{

cards[c].Top = yposition;

cards[c].Left = xposition;

P.Controls.Add(cards[c]);

}

}

OldProgrammer123456  Monday, September 21, 2009 3:10 PM
I copied portions of your code into a test app and the mouse events seem to be working ok. But, I didn't actually take your exact code, only the event handlers. So, that part of it seems ok ... maybe I should try again with your exact code and see if anything goes wrong, because at first glance, and from the simple test I did,it all looks good.
~~Bonnie Berent [C# MVP]

(new blog --- not many posts yet --- be patient)
geek-goddess-bonnie.blogspot.com
BonnieB  Monday, September 21, 2009 4:35 PM
Yes when the cards class is the only thing in the code, it worked for me too. But then when I added the Deck class and made an array of cards in the deck class, that is when it stopped working. So I figure I am either declaring the card class incorrectly or you can't add events in this way.
OldProgrammer123456  Monday, September 21, 2009 6:14 PM
I found the problem, I think. The problem is in your Card class. The class itself is oftype PictureBox, and yet you have a PictureBox member also in the class. You've set the Image and theevent handlerson this PictureBox instead of on the class itself. So, I believe your Card class should look like this instead (I removed the PictureBox member):

class Card : PictureBox
{
	private bool isDragging = false;
	private int currentX, currentY;
	private int formwidth;
	private int formheight;
	private Bitmap bp;

	public int FormWidth { set { formwidth = value; } }
	public int FormHeight { set { formheight = value; } }
	public Bitmap BP { set { bp = value; } }

	public Card()
	{
		Bitmap bp = new Bitmap(75, 102);
		this.Image = bp;
		this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.MoveCard);
		this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.CardMouseDown);
		this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.CardMouseUp);
	}

	private void MoveCard(object sender, MouseEventArgs e)
	{
		int x, y;
		if (isDragging)
		{
			y = ((PictureBox)sender).Top + (e.Y - currentY);
			x = ((PictureBox)sender).Left + (e.X - currentX);

			if (x < 1) { x = 1; }
			if (x > Parent.Width - 85) { x = Parent.Width - 85; }
			if (y < 1) { y = 1; }
			if (y > Parent.Height - 130) { y = Parent.Height - 130; }

			((PictureBox)sender).Top = y;
			((PictureBox)sender).Left = x;
		}
	}
	private void CardMouseDown(object sender, MouseEventArgs e)
	{
		isDragging = true;
		currentX = e.X;
		currentY = e.Y;
		//Visible = false; // this is here to see if the event is being fired, it will come out later 
	}
	private void CardMouseUp(object sender, MouseEventArgs e)
	{
		isDragging = false;
		//Visible = true; //This is here to see if the event is being fired, it will come out later. 
	}
}


~~Bonnie Berent [C# MVP]

(new blog --- not many posts yet --- be patient)
geek-goddess-bonnie.blogspot.com
BonnieB  Monday, September 21, 2009 6:18 PM
Thanks, that did the trick.
OldProgrammer123456  Monday, September 21, 2009 7:30 PM
Great! Glad I could help! =0)
~~Bonnie Berent [C# MVP]

(new blog --- not many posts yet --- be patient)
geek-goddess-bonnie.blogspot.com
BonnieB  Tuesday, September 22, 2009 12:21 AM

You can use google to search for other answers

Custom Search

More Threads

• The Visible property!
• c# mshtml and SHDocVw Web Forms
• about combobox
• Datagridview Text File, Icon
• Is this possible?
• need to make the outlook 2007 TODO BAR!
• UserControl DefaultValue
• checkedlistbox
• How to get treeView newName after LabelEdit ?
• Resizing a user control