How to remove the black border from the disabled button? - c#

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.

Related

BackgroundImage shifts when mouse cursor is hovering over the button

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.

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));
}
}

Changing a ToolTip's cursor

How can I set the default cursor shown when hovering over a ToolTip? I've already created a custom ToolTip class to adjust the color/font, so if necessary I can make changes there.
The ToolTip is shown from a panel in a windows form. The panel's cursor property is set to Cursors.Hand and I would like the ToolTip to match. I've tried changing the form-level cursor in the panel's MouseEnter event, but it's only semi-functional. It quickly alternates between Cursors.Default and Cursors.Hand when I move the cursor around over the ToolTip (as if the tip is forcing the default cursor).
Any help would be greatly appreciated.
On iconPanel's MouseEnter:
Cursor = Cursors.Hand;
if(!showingTip) {
showingTip = true;
changeIconTip.Show("Choose image...", iconPanel, 2, 2, 4000);
}
On iconPanel's MouseLeave:
Rectangle iconPanelArea = new Rectangle(iconPanel.PointToScreen(new Point(0, 0)), iconPanel.Size);
Point c = Cursor.Position;
if(!iconPanelArea.Contains(c)) {
showingTip = false;
changeIconTip.Hide(iconPanel);
Cursor = Cursors.Default;
}
Note: On MouseLeave when the cursor hovers over the ToolTip, technically it 'leaves' the iconPanel (even though it's still inside of the panel's bounds). The Contains check only sets the cursor back to the default if it visually leaves the panel.
I'm not able to reproduce the cursor alternating. On Windows 8.1, the cursor stays the hand when the mouse is over the tooltip. Try this code:
public class Form3 : Form {
Panel iconPanel = new Panel() { BackColor = Color.RosyBrown, Size = new Size(200, 200) };
ToolTip changeIconTip = new ToolTip();
bool showingTip = false;
public Form3() {
Controls.Add(iconPanel);
iconPanel.MouseEnter += iconPanel_MouseEnter;
iconPanel.MouseLeave += iconPanel_MouseLeave;
}
void iconPanel_MouseLeave(object sender, EventArgs e) {
Rectangle iconPanelArea = new Rectangle(iconPanel.PointToScreen(new Point(0, 0)), iconPanel.Size);
Point c = Cursor.Position;
if (!iconPanelArea.Contains(c)) {
showingTip = false;
changeIconTip.Hide(iconPanel);
Cursor = Cursors.Default;
}
}
void iconPanel_MouseEnter(object sender, EventArgs e) {
Cursor = Cursors.Hand;
if (!showingTip) {
showingTip = true;
changeIconTip.Show("Choose image...", iconPanel, 2, 2, 4000);
}
}
}

C#: Changing Button BackColor has no effect

I'm having a problem with C# buttons in Windows Forms.
I've create a number of buttons programmatically and add them to a form afterwards.
Interestingly, every modification to those buttons (location and size) except for the modification of the BackColor is readily executed. Only the button's color remains unchanged.
The code looks something like this:
public class SimpleSortAlgDisplayer : ISortAlgDisplayer
{
#region ISortAlgDisplayer Member
void ISortAlgDisplayer.Init(int[] Data)
{
this.DataLength = Data.Length;
this.DispWin = new CurrentSortStateWin();
this.DispWin.Show();
this.DispWin.Size = new Size(60 + (10 * this.DataLength), 120);
this.myArrayElements = new Button[this.DataLength];
for (int i = 0; i < this.DataLength; i++)
{
this.myArrayElements[i] = new Button();
//begin of series of invoked actions
this.myArrayElements[i].Size=new Size(5,(int)(((80)*(double)Data[i])/1000));
this.myArrayElements[i].Location = new Point(30 + (i * 10), 90-(this.myArrayElements[i].Size.Height));
this.myArrayElements[i].Enabled = true;
this.myArrayElements[i].BackColor = Color.MidnightBlue;
this.myArrayElements[i].UseVisualStyleBackColor = true;
this.DispWin.Controls.Add(this.myArrayElements[i]);
this.myArrayElements[i].Refresh();
}
}
Ideas anyone?
A similar question was asked here but the answers to it were not very helpful:
Trying to use Invoke gives me the run-time error that DispWin is not yet created.
Setting UseVisualStyleBackColor to false changes nothing.
Setting BackColor and ForeColor or Showing DispWin only after adding and formatting the Buttons also had no effect.
Where am I going wrong?
You are trying to set up the color, but then you override it saying UseVisualStyleBackColor = true
if you want to use your custom color, you need to set UseVisualStyleBackColor to false or the color will only be applied to the button upon mouse over.
a simple test uploaded to GitHub
public partial class mainForm : Form
{
Random randonGen = new Random();
public mainForm()
{
InitializeComponent();
}
private void mainForm_Load(object sender, EventArgs e)
{
populate();
}
private void populate()
{
Control[] buttonsLeft = createButtons().ToArray();
Control[] buttonsRight = createButtons().ToArray();
pRight.Controls.AddRange(buttonsRight);
pLeft.Controls.AddRange(buttonsLeft);
}
private List<Button> createButtons()
{
List<Button> buttons = new List<Button>();
for (int i = 1; i <= 5; i++)
{
buttons.Add(
new Button()
{
Size = new Size(200, 35),
Enabled = true,
BackColor = GetColor(),
ForeColor = GetColor(),
UseVisualStyleBackColor = false,
Left = 20,
Top = (i * 40),
Text = String.Concat("Button ", i)
});
}
return buttons;
}
private Color GetColor()
{
return Color.FromArgb(randonGen.Next(255), randonGen.Next(255), randonGen.Next(255));
}
}
result
If FlatStyle for button is set to System, it will not show any backcolor rather use backcolor from template of system colors.
Make sure you do not have a BackgroundImage set. This overrides the BackColor.
In the properties window for Button. Look for 'FlatStyle' property and change it from 'System' to 'Flat', 'Standard' or 'Popup' and you will be able to see the button color change. I just fixed my issue with this.

Categories