BackgroundImage shifts when mouse cursor is hovering over the button - c#

Sorry for my english. This is the first time I write a question in this language. I will be glad if you correct my grammatical errors.
I create a changing_button and set its BackgroundImage property. I wanted this image to change when cursor is over this button. I made two pictures for that.
blue_image.png: enter image description here
red_image.png: enter image description here
Normally changing_button should display red_image. But if cursor is over this button, it should display blue_image. The problem is that blue_image shifts when I try to override BackgroundImage property: enter image description here
How can I fix this?
using System;
using System.Drawing;
using System.Windows.Forms;
namespace winforms_test_1
{
public partial class Form1 : Form
{
Button changing_button;
Image blue_image = Image.FromFile("D://images//blue_image.png");
Image red_image = Image.FromFile("D://images//red_image.png");
public Form1()
{
InitializeComponent();
TableLayoutPanel main_panel = new TableLayoutPanel
{
BackColor = Color.White,
Dock = DockStyle.Fill
};
changing_button = new Button
{
BackgroundImage = red_image,
BackgroundImageLayout = ImageLayout.Center,
FlatStyle = FlatStyle.Flat,
Margin = new Padding(0, 0, 0, 0),
Size = new Size(50, 50),
};
changing_button.FlatAppearance.BorderSize = 0;
changing_button.MouseEnter += new System.EventHandler(this.test_button_MouseEnter);
changing_button.MouseLeave += new System.EventHandler(this.test_button_MouseLeave);
main_panel.Controls.Add(changing_button, 0, 0);
Controls.Add(main_panel);
}
void test_button_MouseEnter(object sender, EventArgs e)
{
changing_button.Image = blue_image;
}
void test_button_MouseLeave(object sender, EventArgs e)
{
changing_button.Image = red_image;
}
}
}

Instead of changing the background image, set the initial Image to the red image. BackgroundImage and Image look like they have slightly different positions.
changing_button = new Button
{
Image = red_image,
ImageLayout = ImageLayout.Center,
FlatStyle = FlatStyle.Flat,
Margin = new Padding(0, 0, 0, 0),
Size = new Size(50, 50),
};
Use either BackgroundImage or Image, but not both.

Related

How to remove the black border from the disabled button?

I created a changing_button with a background image. The FlatAppearance.BorderSize property of this button is zero, so normally it displays without a border. But if I make changing_button disabled, it will have a black border:
How can I remove this border?
I guess that the border appears because changing_button is in focus then I change its Enable property. For that reason, I tried to remove focus from the button and set changing_button.TabStop = false, but it didn't help.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace winforms_test_1
{
public partial class Form1 : Form
{
Button changing_button;
private readonly Image enable_img = Image.FromFile("D://images//enable_img.png");
private readonly Image disable_img = Image.FromFile("D://images//disable_img.png");
public Form1()
{
InitializeComponent();
TableLayoutPanel main_panel = new TableLayoutPanel
{
BackColor = Color.White,
Dock = DockStyle.Fill
};
CreateButton();
Controls.Add(changing_button);
}
private void CreateButton()
{
changing_button = new Button
{
BackgroundImage = enable_img,
BackgroundImageLayout = ImageLayout.Center,
TabIndex = 1,
TabStop = false,
FlatStyle = FlatStyle.Flat,
Margin = new Padding(10, 10, 0, 0),
Location = new Point(40, 40),
};
changing_button.FlatAppearance.BorderSize = 0;
changing_button.Size = new Size(80, 50);
changing_button.Click += new System.EventHandler(this.Button_Click);
}
void Button_Click(object sender, EventArgs e)
{
changing_button.BackgroundImage = disable_img;
changing_button.Enabled = false;
changing_button.TabStop = false;
}
}
}
enable_img.png:
disable_img.png:
To workaround this, I set the border size to 1.
Then set the border color to be the same color of BackColor. Just change on the click event to match the current state.

Programmatically made button cannot change hover color c# winforms

I cannot find a way to change the hover color when delopying it programmatically in c# winforms.
I hope someone can help me!
Code:
Button btn = new Button
{
Name = "btn1",
Width = 250,
Height = 250,
Location = new Point(0, 15),
BackColor = Color.Transparent,
FlatStyle = FlatStyle.Flat,
BackgroundImage = img,
BackgroundImageLayout = ImageLayout.Stretch,
};
You can specify the hover color outside of the initialization block:
// We create button
Button btn = new Button
{
...
}
// And then specify hovering behaviour
// Blue while hovering
btn.FlatAppearance.MouseOverBackColor = Color.Blue;
// Red when pressing (uncomment if you want)
// btn.FlatAppearance.MouseDownBackColor = Color.Red;
use MouseEnter and MouseLeave for change background color in button
Button btn = new Button
{
Name = "btn1",
Width = 250,
Height = 250,
Location = new Point(0, 15),
BackColor = Color.Transparent,
FlatStyle = FlatStyle.Flat,
BackgroundImage = img,
BackgroundImageLayout = ImageLayout.Stretch,
};
btn.MouseEnter += OnMouseEnter;
btn.MouseLeave += OnMouseLeave;
private void OnMouseEnter(object sender, EventArgs e)
{
button1.BackColor = Color.Red;
}
private void OnMouseLeave(object sender, EventArgs e)
{
button1.BackColor = Color.Transparent;
}

Transparent control over drew shape

I'm new for create user control, and in my first usercontrol i used from picturebox and label ,
picturebox for draw a shape and label for show text over that shape.
i was set picturebox parent for label, and label backcolor to transparent also if don't have any text label set to visible = false
now i have a problem, when label is visible, i can't see picturebox correctly.
how can i solve this problem ?
also paint event on user control not work
private void Ucontrol_Paint(object sender, PaintEventArgs e)
{
if (RightToLeft)
{
lblTxt.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
}
else
{
lblTxt.RightToLeft = System.Windows.Forms.RightToLeft.No;
}
lblTxt.ForeColor = FontColor;
lblTxt.Text = Txt;
if (Question)
{
BorderColor = Color.DarkBlue;
BackColor = Color.FromArgb(75, 163, 234);
CreateQuestion(BorderColor, BackColor);
}
else
{
BorderColor = Color.DarkGreen;
BackColor = Color.FromArgb(59, 226, 75);
CreateAnswer(BorderColor, BackColor);
}
}
Forms controls don't have really a transpartent background, they copy it's parent content.
Also, a PictureBox can't be parent of another control as they aren't container.
Then, instead of using a picturebox just set the usercontrol background image and put the label on it, the transparency should work.
Here is a working example manually drawing the control content:
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
Label lbl = new Label();
lbl.Location = new Point(10, 10);
lbl.Width = 150;
lbl.Height = 150;
lbl.BackColor = Color.Transparent;
lbl.Text = #"asdfasdfasdfasdf\r\nasdfasdfasdf\r\n\r\nasdfasdfasdf";
lbl.Visible = true;
this.Controls.Add(lbl);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.FillRectangle(Brushes.Red, new Rectangle(10, 10, 100, 100));
e.Graphics.FillEllipse(Brushes.Yellow, new Rectangle(10, 10, 100, 100));
}
}

PictureBox doesn't show up in C# WinForm

I am trying to create a custom error dialog, that has a red x on the left hand side. Below is my code;
using JohnsonControls.FieldBusFDD.Properties;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace JohnsonControls.FieldBusFDD
{
public class ErrorDialog : Dialog
{
public static bool ShowDialog(string text, string title)
{
Form prompt = new Form();
prompt.Width = 435;
prompt.Height = 122;
prompt.FormBorderStyle = FormBorderStyle.FixedDialog;
prompt.Text = title;
prompt.StartPosition = FormStartPosition.CenterScreen;
PictureBox image = new PictureBox();
image.Image = Resources.red_x;
image.Location = new Point(10, 10);
image.Size = new Size(50, 50);
Label textLabel = new Label() { Left = 60, Top = 10, Width = 350, Text = text };
Button confirmation = new Button() { Text = "Ok", Left = 300, Width = 100, Top = 52 };
confirmation.Click += (sender, e) => { prompt.Close(); };
confirmation.DialogResult = DialogResult.OK;
prompt.Controls.Add(image);
prompt.Controls.Add(confirmation);
prompt.Controls.Add(textLabel);
prompt.AcceptButton = confirmation;
return prompt.ShowDialog() == DialogResult.OK ? true : false;
}
}
}
The file is stored in the project resources, and compiles just fine but the image is not displayed.
When the pixel size of the image is too big, the PictureBox only shows the upper-left corner of the image by default.
Try this:
image.SizeMode = PictureBoxSizeMode.Zoom;
From MSDN:
By default, in Normal mode, the Image is positioned in the upper-left corner of the PictureBox, and any part of the image that is too big for the PictureBox is clipped. Using the StretchImage value causes the image to stretch or shrink to fit the PictureBox. Using the Zoom value causes the image to be stretched or shrunk to fit the PictureBox; however, the aspect ratio in the original is maintained.
Try to strecth image on your picture box by:
image.SizeMode = PictureBoxSizeMode.StretchImage;
Using the StretchImage value causes the image to stretch or shrink to fit the PictureBox.

How to keep picture box right side of table lay out in window form c#

How to get picture box image at right side in TableLayoutPanel in c#? At present I am getting image next line of label but i need picture box text beside a label in TableLayoutPanel.Here is my code
PictureBox pb = new PictureBox();
pb.ImageLocation = ../imagesDT/answered.gif
tableLayoutPanel1.Controls.Add(pb);
I am trying to keep image beside the label in TableLayoutPanel, but at present getting image below the label.
Try this:
class Form4 : Form {
PictureBox pb = new PictureBox() { BackColor = Color.RosyBrown, Dock = DockStyle.Fill };
Label lb = new Label { Text = "Label", AutoSize = true };
TableLayoutPanel panel = new TableLayoutPanel { Dock = DockStyle.Fill };
public Form4() {
Controls.Add(panel);
panel.Controls.Add(lb, 0, 0);
panel.Controls.Add(pb, 1, 0);
}
}

Categories