I think this is a stupid question, but I don't understand what is happening here.
I use this code:
private void pictureBox1_MouseHover(object sender, EventArgs e)
{
pictureBox1.Visible = false;
pictureBox1.BackColor = Color.Black;
}
private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
pictureBox1.Visible = true;
}
The problem is: The picturebox changes color to black if mouse is over picturebox, but the visibility doesn't change. Why?
I think your problem is as soon as you hover the picture it really disappears (that's why you see the back color turn to black, the event is firing). However the picture disappears which lead to situation where your mouse is not on the picture anymore, therefore Mouse_Leave event firing.
You can use MouseEnter event instead of MouseHover and bool field isHover that you can use in attempt to reduce flickering :
public partial class Form1: Form
{
bool isHover = false;
private void pictureBox1_MouseEnter(object sender, EventArgs e)
{
if(isHover) return;
// with MouseHover this control visibility appears to be locked with MouseEnter it is not
pictureBox2.Visible = false;
pictureBox2.BackColor = Color.Black;
}
private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
if(!isHover) return;
isHover = false;
pictureBox2.Visible = true;
}
...
}
Related
Is there a way to create a toggle button in C# windows forms? I want to have is as such that when you toggle the button, the background changes from red to transparent. I've done that with a button but it would be better with toggle. Thanks
According to your description, you want the background to change from red to transparent when you switch buttons.
You can try the following code to solve this problem.
private void Form1_Load(object sender, EventArgs e)
{
radioButton1.Checked = true;
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
this.BackColor = Color.Red;
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
this.TransparencyKey = Color.Turquoise;
this.BackColor = Color.Turquoise;
}
Result:
I am a novice in C# WindowForm projects, and I am facing some issues on how to toggle 'one button' into changing between 2 backcolors in C# WindowForm.
In order to toggle 'one button', I am required to use IF function in this assignment. However, my problem is that I do not know how to transition from color1 to 2 by pressing the button when running the code, I somewhat know the syntax structure of IF and else.
Could anyone help please?
namespace draft
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btn1_Click(object sender, EventArgs e)
{
bool btnclick = true;
if (btnclick == true)
{
BackColor = Color.Purple;
return;
}
if (btnclick == false)
{
BackColor = Color.Green;
return;
}
}
}
}
As you are declaring btnclick on click event and setting it to true, it will be true every time and only if block will be executed. Check current background color and change it.
private void btn1_Click(object sender, EventArgs e)
{
if (BackColor == Color.Purple)
{
BackColor = Color.Green;
return;
}
else
{
BackColor = Color.Purple;
return;
}
}
Here's the simplest code:
private void btn1_Click(object sender, EventArgs e)
{
BackColor = BackColor == Color.Purple ? Color.Green : Color.Purple;
}
I want to show a PictureBox on button click, this should be on first click Show PictureBox, and on next click Hide PictureBox, again on third click show PictureBox and similar on fourth click again hide the PictureBox.
I have tried below code but it stuck after dispose of picture.
public bool i;
private void button1_Click(object sender, EventArgs e)
{
if (!i)
{
m1();
i = true;
}
else
{
m2();
i = false;
}
}
public void m1()
{
pictureBox1.Show();
}
public void m2()
{
pictureBox1.Dispose();
}
You don't need to Dispose your PictureBox. Just set the visibility like this:
private void button1_Click(object sender, EventArgs e)
{
pictureBox1.Visible = !pictureBox1.Visible;
}
pictureBox.Visible = true;
pictureBox.Visible = false;
I assume this is for Winforms? Calling Dispose will destroy the picturebox object in memory so that is why you are having the issue.
Use:
// Show the picture box
pictureBox.Visible = true;
// Hide the picture box
pictureBox.Visible = false;
// Show the picture box
pictureBox.Visible = true;
// Hide the picture box
pictureBox.Visible = false;
How can I change a button's style, in particular the image, upon the mouse being pressed and held and then the cursor dragged away from the button?
You'll notice that, on this action, the default behaviour for the button is to revert it's style to hover style. This can be partially configured using the MouseOverBackColor. I want to ensure that, whenever the MouseOverBackColor is applied, I also have a specific image on the button.
I have tried the code below, to have an "isMouseDown" flag which is checked in the leave event. However, this doesn't work for me.
private void btnFormMinimize_MouseClick(object sender, MouseEventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}
bool isMouseDown = false;
private void btnFormMinimize_MouseDown(object sender, MouseEventArgs e)
{
Button b = (Button)sender;
b.Image = Properties.Resources.icon_minimize_click;
isMouseDown = true;
}
private void btnFormMinimize_MouseEnter(object sender, EventArgs e)
{
Button b = (Button)sender;
b.Image = Properties.Resources.icon_minimize_hover;
}
private void btnFormMinimize_MouseLeave(object sender, EventArgs e)
{
Button b = (Button)sender;
if (isMouseDown)
{
b.Image = Properties.Resources.icon_minimize_hover;
}
else
{
b.Image = Properties.Resources.icon_minimize;
}
}
private void btnFormMinimize_MouseUp(object sender, MouseEventArgs e)
{
isMouseDown= false;
}
Thanks in advance.
My Windows Forms code has two superposed PictureBoxes: a small X on the top corner of a user-loaded image. I'll call them DelImage and Image, respectively. Like this:
http://imgur.com/fsW7R8i
The DelImage appears (Visible = true) on Image_MouseEnter and disappears on Image_MouseLeave. Now, I want the DelImage to have a custom behavior as well when the mouse enters it, but DelImage never fires events (nor MouseEnter, nor MouseLeave, nor Click). I've tried bringing it to front, but it already is in front of the Image.
Here's some code:
private void picImage_MouseEnter(object sender, EventArgs e)
{
delImage.Visible = true;
}
private void picImage_MouseLeave(object sender, EventArgs e)
{
delImage.Visible = false;
}
private void picDelImage_MouseEnter(object sender, EventArgs e)
{
delImage.Visible = true;
this.Cursor = Cursors.Hand;
}
private void picDelImage_MouseLeave(object sender, EventArgs e)
{
this.Cursor = Cursors.Arrow;
}
private void picDelImage_Click(object sender, EventArgs e)
{
Panel currentPanel = this.tlpImages.Controls["pnlImage"] as Panel;
currentPanel.Visible = false;
}
With this code, the picDelImage events are never reached. I never enter DelImage, apparently because I never leave Image. What can I do to make my PictureBoxes have the expected behavior?