How to detect a click on a component that is not visible - c#

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.

Related

How can i get number of PictureBox that was clicked?

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.

How can i move scrollbars of PictureBox when button is pressed in Visual Studio?

How can i move scrollbars of picturebox when a button is pressed?
I have flowLayoutPanel (Dock: none, AutoSize: false, AutoScroll: true) and i placed on it a PictureBox (Dock: none, SizeMode: AutoSize). I loaded a large image (9000x6315px) into PictureBox so scrollbars are visible and allow me to scroll map. But.. only with mouse. How can i scroll a PictureBox using a code, when a button is pressed?
Problem is BETTER visible in this video on youtube (duration 3mins) and will let you better understand what i mean:
https://youtu.be/3Haqzsyn_zE
In Embarcadero Rad Studio i could write something like this:
ScrollBox1->HorzScrollBar->Position=500;
ScrollBox1->VertScrollBar->Position=500;
Is it possible in VS?
Thank you!
Add a temporary button to your form, with this code (inserting the name of your FlowLayoutPanel):
Console.WriteLine(flowLayoutPanel1.AutoScrollPosition.ToString());
Scroll your zoomed map to the position you want it to be in and click the button.
Example output:
{X=-146,Y=-164}
Whatever those values are, you want to store the opposite of them. Repeat the process and write down all the positions you need.
Now you can set the AutoScrollPosition of your FlowLayoutPanel to any of those points, and it will scroll there. For instance, if that was the desired point for your "B1" button:
private void B1_Click(object sender, EventArgs e)
{
Point pt = new Point(146, 164);
flowLayoutPanel1.AutoScrollPosition = pt;
}

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 ...

Picture box On Picture Box

This is in regards to C# coding, im quite new to this programming language, and do not know much about any other as well, but what i would like to achieve is to have a picture box as the background, and have another picture box over lapping it but the transparent part has to show the picture box behind. I have been able to have a transparent picture box, but the thing is it only shows the form's back color rather than the picture box behind it. can anyone help with this?
IN other words a picture box over a picture box, but able to see through the first picture box where it is clear, and see the picture box behind.
Thanks in advance.
Go to Project -> Add user control.
Give that User Control the BackGroundImage.
Drag your picturebox onto the the usercontrol. Make the PictureBox's Backcolor transparent.
Build the project.
In the designer, you should be able to drag your new usercontrol onto the form.
This will do what you want.
for background, you can use Graphics. build a paint event for your form visually:
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawImage(Image.FromFile("address of image"), 0, 0, this.Width, this.Height);
}
it will color the form and you don't need picturebox for background.

Why are my disabled buttons changing color?

My problem is: When my application is opened, I disable all buttons (in form_Load) and their color changes to the color of the background. But then I do some action (like clicking on a button), and in this action I disable the buttons again.
Now some of these buttons become GRAY and others become as the background.
Why is this? I don't want the the gray effect. Normally when I disable the button at the beginning of the application, it becomes the color I expect, but when I try to disable them again this strange behavior appeared. What to do?
My code is like:
private void _btnDownload2PC_Click(object sender, EventArgs e)
{
// do action
_btnDownloadToPC.Enabled=false; // its color became gray
_btnDownloadToPhone.Enabled=false; // its color became like the
// background color and it can't
// be pressed
}
I figured out that the problem is when I use the button_MouseLeave() or button_MouseMove() functions. For example:
private void _btnOneToCort_MouseLeave(object sender, EventArgs e)
{
this._btnOneToCart.Image=global::MyProject.Properties.Resources.button3over;
}
but this doesn't make sense. Why does this function change my button settings (I don't know what these are) When I use these function this strange behavior appears, but when I don't, everything goes right?
You can programmatically access the color of a button. Set a breakpoint and do that to see if they really are changing.
My guess: the image that you are setting has a gray background that is different than the standard disabled color. You will need to edit the image and remove the background.
Why don't disable buttons in designer? If not acceptable, do that in form constructor not Form_Load.
Also, how can you click on button if it's disabled?
Are form color settings default? Have you changed windows theme color preferences?

Categories