How can i get number of PictureBox that was clicked? - c#

I make Tic Tac Toe game, i have 9 cells that the player can click. Initially cell don't have any picture, but when cell was clicked it's picture must changes. How can i create this event for every cell without code duplicate?

The signature for a click event has a "sender" object. You can use that for reference. Cast it to the appropriate type, and you can access all its public properties directly.
You should be able to do something like:
protected void pictureClick(Object sender, EventArgs e) {
PictureBox pic = sender as PictureBox;
if (pic != null) {
// set the image based on which players turn it is.
}
}
and now each picture box would have its onClick event set to this one pictureClick function.
--- Edit ---
I'd also add that using independent UI controls in this manner is extremely inefficient for large tile games. For a 3x3 tic-tac-toe grid, it's probably fine, but for something like a 8x8 chessboard, the refresh times for all 64 squares is going to be noticeable, because every UI component on the page has its Paint() method called when the window refreshes. I'm speaking from experience here. I once tried to use this approach of having a grid of 10x10 Panel components with custom draw methods based on the game state data, and anytime the window was resized, the game would hang for several seconds while everything would refresh.

Related

How to detect a click on a component that is not visible

Is it possible to detect a click on a picturebox that is not visible
I have tried this :
private void PictureBox1_Click(object sender, EventArgs e)
{
MessageBox.Show("teste");
}
But even if the picture box has no image and that background color is transparent, it hides the elements behind it.
I explain myself a little better;
I do a tic-tac-toe and I have the image of the cross that is not visible when the game is started; as soon as I press on a delimited area (where I want to place the cross), I want it to become visible
How can I do that?
Thank you.
I would use the PictureBox with an ImageList containing 2 images: the first is the background of your game field and the second is the cross.
In your Click Event I would toggle the pictures on click then.

How to create fade button effect with pictureboxes

I'm trying to create an effect so when the user hovers the mouse over a picturebox, the button fades to a hoverimage, and when they leave, it fades back to the original. I'm using pictureboxes as buttons in a program. I'm doing this because all the buttons will be pictures with no button textures, so I didn't see the point in using a button. Just so you can visualize it, here is the original image:
And the image to fade to:
I could still change these images a bit, but thats the general idea.
How would I go about creating this fading effect? I'm picturing something using timers and opacity settings, but I don't know how any of that stuff could help me solve this.
E: Heres a bit of code I have. It changes from image to image when I hover, but its not a fade, and it looks very choppy.
private void pictureBox3_MouseEnter(object sender, EventArgs e)
{
pictureBox3.Image = pictureBox37.Image;
}
private void pictureBox3_MouseLeave(object sender, EventArgs e)
{
pictureBox3.Image = pictureBox38.Image;
}
pictureBox37 and pictureBox38 are invisible reference pictureboxes with the images I need.
I don't think there's any WinForms support for this kind of animation, so you'd have to do this manually. If you have more than one of these buttons, I recommend creating your own UserControl with this functionality. I can give you some pointers how to do this:
Your UserControl will have two Image properties, one for the normal and one for the hover image
You'll have to override the OnPaint method to do your own custom painting, blending these two images onto your Graphics according to a position property. This is a float where 0 indicates showing the normal image and 1 the hover image. Any value in between means a blend of these two images. I found some good blending code here.
Then you'll need a Timer to update this position dynamically and redraw the button (by calling Invalidate). I recommend using a Windows.Forms.Timer, which has the advantage that its Tick event is always executed on the main Thread, so you don't have to use Invoke to modify your control.
And last but not least you override the OnMouseEnter and OnMouseLeave methods, to set this all into motion. These methods could set a positionChange property that indicates in which direction position is changing.
Hope this helps ...

GDI update graphics issue

i tried to search for this on google, but didnt seem to find anything related that could help me.
My problem is this, i have a panel in which i draw a cube, and i added a group box with 3 radio buttons and 4 normal buttons(these do the rotation of the cube)
For testing i have another button added on the panel but not in the group box.
The problem is this, when i push on any button it doesn't update the cube's rotation, only when i move the mouse on the test button(over it)
If i try to move the buttons outside the group box then all works well, but they don't work if they stay inside the group box.
Does anyone know how i can fix this?
May I suggest that instead of redrawing the panel you create a Bitmap for drawing your (rotated) cube and use a PictureBox to display it? The PictureBox could be inside the Panel together with the GroupBox.
The flickering comes because the panel redraws his background first and then raises the Paint event. To avoid this you must to create a custom control and do the drawing in OnPaintBackground protected method. This seems a overkill. Or you can write to a Bitmap and put it in the BackgroundImage property.

Making a slideshow viewer, handling events fired from arbitrary controls

I am extending an image viewer to support slideshow functionality. I have used a split container to separate the main form into two panels.
Panel on the left will contain a list of thumbnails
Panel on the right will contain the full-size image
Each panel supports drag and drop. When I drop an image file into the thumbs panel, it should create a thumbnail and display it on the panel, starting from the top and working its way down as more images are dropped. By default, the first image available will be shown on the panel to the right.
When I select another thumbnail, the viewer will display the full-size image.
What is a good way to implement this list of thumbnails? I've looked through the list of controls available but can't decide which one is most appropriate for this.
I was thinking of dynamically creating PictureBox objects, but then it didn't seem obvious how after I register a Click event, how I would identify which PictureBox the event was sent from.
I am looking for one of two possible types of answers
going ahead with the PictureBox creation idea, but understanding how I should be handling the Click events to correctly show the desired image.
an alternative suggestion for displaying the list of thumbs (if the PictureBox idea is not feasible)
You can tie the event handler to multiply controls and identify them by the "sender" parameter. It always points to the event sender.
void OnClick(object sender, KeyEventArgs e) {
PictureEdit editor = (PictureEdit)sender;
}
Sub OnClick(ByVal sender As Object, ByVal e As KeyEventArgs)
Dim editor as PictureEdit = CType(sender, PictureEdit)
End Sub
Alternatively, you can create your own (not that complex) control divided into rectangular areas displaying images.

C# Only refresh PictureBox/Panel when tell them to refresh?

I'm making a bomberman game in a C# windows form application. It has over 300 pictureboxes (walls) that are placed on a panel. The picturebox of bomberman himself is also on that panel.
When the location of bombermans picturebox changes, all the controls on the panel are automatically refreshed. Because there are so many controls on that panel, and because the location of the picturebox changes multiple times per second, the program becomes laggy when I try to move.
I want to have control over the refresh event of the panel (and it's controls), because I think my problem is solved when only the pictureboxes that need to be refreshed, are refreshed programmatically.
I hope someone can help me out with this!
Ruud.
If you move the child, the parent has to be refreshed because it may need to draw the area where child was located previously. It would also mean that all children (of parent) would get refreshed.
OTH, using so many controls may not be a good idea. I would suggest you to keep data structures describing walls and then use it to draw it within Panel (or your custom control). You can write your own logic for hit testing (mouse or keyboard click within wall boundary) by capturing mouse/keyboard events at Panel/Parent level. With correct organization data structure, hit testing can be very efficient.
You are trying to paint the whole form which will surely take time . If you want to change only a part of the form, which in your case is Moving the bomberman to a new position, Only invalidate the area you want to repaint and then pass it to the Invalidate method.
Do something similar to this.
//Invalidate previous position of bomberman
Rectangle invalid = new Rectangle(picturebox1.Location.x,picturebox1.Location.y,picturebox1.Width,picturebox1.Height);
Invalidate(invalid);
//Add code to move your picture box and then call above two lines again
invalid = new Rectangle(picturebox1.Location.x,picturebox1.Location.y,picturebox1.Width,picturebox1.Height);
Invalidate(invalid);
Note sure but somthing similar polished code would work...
Here is a link to an example for reference. http://msdn.microsoft.com/en-us/library/ms229628.aspx

Categories