I want to make a custom button control (image button is ok) like this one.
I'm a new user, so I can't post image here. So I uploaded the picture here
I'm kind of desperate right now after trying some tutorials
Any suggestion is highly appreciated.
Thanks
Updated 08/10/2019: I asked this question so many years ago, and at that time I didn't have the permission to upload image, so the image I uploaded to the third party site is long gone now. I got many requests about re-uploading the image, so here is what I remember from that project I did eight years ago, I just find some random images about window form that match my memory
This is when the button is in normal state
This is when the button is hovered or clicked, with the rounded border
You could create a class that inherits from Button to keep all your styling in one place. To do the hover and pressed states you can override the mouse enter / leave events of the button and change style.
Here is an example from one of our projects (I changed the colours but your get the idea). Where we change some colours you could switch the images.
public class MossieButton : Button
{
private static Font _normalFont = new Font("Arial", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
private static Color _back = System.Drawing.Color.Grey;
private static Color _border = System.Drawing.Color.Black;
private static Color _activeBorder = System.Drawing.Color.Red;
private static Color _fore = System.Drawing.Color.White;
private static Padding _margin = new System.Windows.Forms.Padding(5, 0, 5, 0);
private static Padding _padding = new System.Windows.Forms.Padding(3, 3, 3, 3);
private static Size _minSize = new System.Drawing.Size(100, 30);
private bool _active;
public MossieButton()
: base()
{
base.Font = _normalFont;
base.BackColor = _border;
base.ForeColor = _fore;
base.FlatAppearance.BorderColor = _back;
base.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
base.Margin = _margin;
base.Padding = _padding;
base.MinimumSize = _minSize;
}
protected override void OnControlAdded(ControlEventArgs e)
{
base.OnControlAdded(e);
UseVisualStyleBackColor = false;
}
protected override void OnMouseEnter(System.EventArgs e)
{
base.OnMouseEnter(e);
if (!_active)
base.FlatAppearance.BorderColor = _activeBorder;
}
protected override void OnMouseLeave(System.EventArgs e)
{
base.OnMouseLeave(e);
if (!_active)
base.FlatAppearance.BorderColor = _border;
}
public void SetStateActive()
{
_active = true;
base.FlatAppearance.BorderColor = _activeBorder;
}
public void SetStateNormal()
{
_active = false;
base.FlatAppearance.BorderColor = _border;
}
}
Can't see the picture but I guess you can change the border of the button and set a background image.
button1.FlatStyle = FlatStyle.Flat;
button1.BackgroundImage = Bitmap.FromFile("image.jpg");
I think the simplest way is set some properties of the button like below and
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button1.Image = "Any Image"
this.button1.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.button1.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageBeforeText;
then write the code for
private void button1_Click(object sender, EventArgs e)
{
//Code for Image Appearance.
button1.Text = "OnClick";
}
private void button1_MouseEnter(object sender, EventArgs e)
{
//Code for Image Appearance.
button1.Text = "Enter";
}
private void button1_MouseLeave(object sender, EventArgs e)
{
//Code for Image Appearance.
button1.Text = "Normal";
}
Update:
I don't know whether I am going correct or not but I think You can also achive your goal by putting a Button and a label inside a panel and arrange them according to your choice. Make the button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat at initial with Label.Text="Normal". Then on Mouse enter to the Panel draw a rectangle with a border around the button and change the text of the label to "Hover". Like that Clicking on the Panel also you change the rectangle border according to you and make the label.Text="OnClick".
Related
I want to build a Windows Forms App that has a menu (several labels) on it's left side which is toggled. On the right side there should be some columns i can scroll through. Jst like Excel with it's fixed rownumbers.
Is there a way to do this? Preferably an easy one.
I think you can use two panels to make the form like the picture you provided.
The following code is a code example and you can refer to it.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
ScrollBar hScrollBar1 = new HScrollBar();
private void Form1_Load(object sender, EventArgs e)
{
panel1.BorderStyle = BorderStyle.FixedSingle;
panel1.Dock = DockStyle.Left;
panel2.BorderStyle = BorderStyle.FixedSingle;
panel2.Dock = DockStyle.Fill;
hScrollBar1.Dock = DockStyle.Bottom;
hScrollBar1.Scroll += new ScrollEventHandler(hScroller_Scroll);
panel2.Controls.Add(hScrollBar1);
panel2.HorizontalScroll.Visible = false;
panel2.HorizontalScroll.Enabled = true;
}
private void hScroller_Scroll(object sender, ScrollEventArgs e)
{
panel2.HorizontalScroll.Value = e.NewValue;
}
}
The specific result:
in XAML i created button and when i click on the button then background will be changed to red color(because i set it) but if i close my application and then again start application so background is not red. What i need to do is keep background there like before(when i clicked on button) so have red background if i again start application. Your help will be for me very important. Thank you experts. Btw this is code for button now: :).
private void MenuFlyoutItem_Click(object sender, RoutedEventArgs e)
{
background.Background = new SolidColorBrush(Colors.Red);
}
you need to save the selected color; or save somwething some you can determine what color it has to be. In the constructor or your page read this value and set the right color.
you can use localsettings to save this.
ApplicationData.Current.LocalSettings.Values["BGColor"] = "Red";
for example.
In the constructor:
if ( ApplicationData.Current.LocalSettings.Values["BGColor"] == "Red")
{
background.Background = new SolidColorBrush(Colors.Red);
}
full sample:
public MainPage()
{
this.InitializeComponent();
if ((string)ApplicationData.Current.LocalSettings.Values["BGColor"] == "Red")
LayoutRoot.Background = new SolidColorBrush(Colors.Red);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
ApplicationData.Current.LocalSettings.Values["BGColor"] = "Red";
LayoutRoot.Background = new SolidColorBrush(Colors.Red);
}
I want to add icon inside a button. Here is my code
private void Printbutton_Click(object sender, EventArgs e)
{
// Assign an image to the button.
Printbutton.Image = Image.FromFile("D:\\Downloads\\print.png");
// Align the image and text on the button.
Printbutton.ImageAlign = ContentAlignment.MiddleRight;
Printbutton.TextAlign = ContentAlignment.MiddleLeft;
// Give the button a flat appearance.
Printbutton.FlatStyle = FlatStyle.Flat;
if (SetupThePrinting())
printDocument1.Print();
}
The problem here is that the icon doesn't appear at first , it appears when I click to the button.
What's wrong here ?
you added the icon in printbutton_click event instead defining it in Form initializecomponents
public Form2()
{
InitializeComponent();
// Assign an image to the button.
Printbutton.Image = Image.FromFile("D:\\Downloads\\print.png");
// Align the image and text on the button.
Printbutton.ImageAlign = ContentAlignment.MiddleRight;
Printbutton.TextAlign = ContentAlignment.MiddleLeft;
// Give the button a flat appearance.
Printbutton.FlatStyle = FlatStyle.Flat;
}
private void Printbutton_Click(object sender, EventArgs e)
{
if (SetupThePrinting())
printDocument1.Print();
}
Like this
public Form1()
{
InitializeComponent();
// Assign an image to the button.
button1.Image = Image.FromFile("C:\\Yourfolder");
// Align the image and text on the button.
button1.ImageAlign = ContentAlignment.MiddleRight;
button1.TextAlign = ContentAlignment.MiddleLeft;
// Give the button a flat appearance.
button1.FlatStyle = FlatStyle.Flat;
}
private void button1_Click(object sender, EventArgs e)
{
// Your print code.
}
I was wondering how could I do this. I know I can use the button component but it has the little gray stuff around it when I give it a image. With image button how could I show another image for the hover effect
You want to create a button with no border but displays different images when the user hovers over it with the mouse? Here's how you can do it:
Add an ImageList control to your form at add two images, one for the button's normal appearance and one for when the mouse is hovering over.
Add your button and set the following properties:
FlatStyle = Flat
FlatAppearance.BorderColor (and maybe MouseOverBackColor & MouseDownBackColor) to your form's background color
ImageList = the ImageList you added to the form
ImageIndex to the index value of your normal image
Code the MouseHover and MouseLeave events for the button like this:
// ImageList index value for the hover image.
private void button1_MouseHover(object sender, EventArgs e) => button1.ImageIndex = 1;
// ImageList index value for the normal image.
private void button1_MouseLeave(object sender, EventArgs e) => button1.ImageIndex = 0;
I believe that will give you the visual effect you're looking for.
Small summary (Border, MouseDownBackColor, MouseOverBackColor)
FlatApperance
BorderColor = Black or what ever you want
BorderSize = can be set to 0
MouseDownBackColor = Transparent
MouseOverBackColor = Transparent
Text = none
For MouseDown:
// ImageList index value for the mouse down image.
private void button1_MouseDown(object sender, MouseEventArgs e) => button1.ImageIndex = 2;
You can assign the BackgroundImage property for the button. You can also use the OnMouseEnter and OnMouseExit events to change the background as per your request.
See BackgroundImage OnMouseEnter OnMouseLeave
I also needed an image button, but I wanted one like the ToolstripMenuButton.
With the correct borders and colors on hover.
So I made a custom control to do just that:
using System;
using System.ComponentModel;
using System.Windows.Forms;
namespace LastenBoekInfrastructure.Controls.Controls
{
[DefaultEvent("Click")]
public class ImageButton : UserControl
{
public string ToolTipText
{
get { return _bButton.ToolTipText; }
set { _bButton.ToolTipText = value; }
}
public bool CheckOnClick
{
get { return _bButton.CheckOnClick; }
set { _bButton.CheckOnClick = value; }
}
public bool DoubleClickEnabled
{
get { return _bButton.DoubleClickEnabled; }
set { _bButton.DoubleClickEnabled = value; }
}
public System.Drawing.Image Image
{
get { return _bButton.Image; }
set { _bButton.Image = value; }
}
public new event EventHandler Click;
public new event EventHandler DoubleClick;
private ToolStrip _tsMain;
private ToolStripButton _bButton;
public ImageButton()
{
InitializeComponent();
}
private void InitializeComponent()
{
var resources = new ComponentResourceManager(typeof(ImageButton));
_tsMain = new ToolStrip();
_bButton = new ToolStripButton();
_tsMain.SuspendLayout();
SuspendLayout();
//
// tsMain
//
_tsMain.BackColor = System.Drawing.Color.Transparent;
_tsMain.CanOverflow = false;
_tsMain.Dock = DockStyle.Fill;
_tsMain.GripMargin = new Padding(0);
_tsMain.GripStyle = ToolStripGripStyle.Hidden;
_tsMain.Items.AddRange(new ToolStripItem[] {
_bButton});
_tsMain.Location = new System.Drawing.Point(0, 0);
_tsMain.Name = "_tsMain";
_tsMain.Size = new System.Drawing.Size(25, 25);
_tsMain.TabIndex = 0;
_tsMain.Renderer = new ImageButtonToolStripSystemRenderer();
//
// bButton
//
_bButton.DisplayStyle = ToolStripItemDisplayStyle.Image;
_bButton.Image = ((System.Drawing.Image)(resources.GetObject("_bButton.Image")));
_bButton.ImageTransparentColor = System.Drawing.Color.Magenta;
_bButton.Name = "_bButton";
_bButton.Size = new System.Drawing.Size(23, 22);
_bButton.Click += bButton_Click;
_bButton.DoubleClick += bButton_DoubleClick;
//
// ImageButton
//
Controls.Add(_tsMain);
Name = "ImageButton";
Size = new System.Drawing.Size(25, 25);
_tsMain.ResumeLayout(false);
_tsMain.PerformLayout();
ResumeLayout(false);
PerformLayout();
}
void bButton_Click(object sender, EventArgs e)
{
if (Click != null)
{
Click(this, e);
}
}
void bButton_DoubleClick(object sender, EventArgs e)
{
if(DoubleClick != null)
{
DoubleClick(this, e);
}
}
public class ImageButtonToolStripSystemRenderer : ToolStripSystemRenderer
{
protected override void OnRenderToolStripBorder(ToolStripRenderEventArgs e)
{
//base.OnRenderToolStripBorder(e);
}
}
}
}
I want to add a label to my form , and I want it without any color- I want just it's text to be visible, I don't find this option in the label's properties, can anyone help me please?
Do you want to make the label (except for the text) transparent? Windows Forms (I assume WinForms - is this true) doesn't really support transparency. The easiest way, sometimes, is Label's Backcolor to Transparent.
label1.BackColor = System.Drawing.Color.Transparent;
You will run into problems though, as WinForms really doesn't properly support transparency. Otherwise, see here:
http://www.doogal.co.uk/transparent.php
http://www.codeproject.com/KB/dotnet/transparent_controls_net.aspx
http://www.daniweb.com/code/snippet216425.html
Setting the parent of a usercontrol prevents it from being transparent
Good luck!
If you picture box in the background then use this:
label1.Parent = pictureBox1;
label1.BackColor = Color.Transparent;
Put this code below InitializeComponent(); or in Form_Load Method.
Ref: https://www.c-sharpcorner.com/blogs/how-to-make-a-transparent-label-over-a-picturebox1
You are right. but here is the simplest way for making the back color of the label transparent
In the properties window of that label select Web.. In Web select Transparent
:)
this.label1.BackColor = System.Drawing.Color.Transparent;
Let's view 2 possible cases.
Background is a color.
Double-Click the form background in VS constructor. Then, write this code:
/*
This code will set all your object's background color to the same as the form.
This should be written in the body of <FormName>_Load(object, EventArgs).
*/
Control[] objs = new Control[] { /* your object list, e. g { myLabel, myPicture } */ };
foreach (Control control in objs) {
control.BackColor = Color.Transparent;
// OR
control.BackColor = this.BackColor;
}
Background is a PictureBox.
This is also easy. We just need to make all objects as children of your PictureBox and set it's color to transparent. Code:
/*
This code will set all your object's background to transparent and show the PBox.
This should be written in the body of <FormName>_Load(object, EventArgs)'s foreach loop.
Put everything before it the same as in 1st code fragment.
*/
control.Parent = back;
control.BackColor = Color.Transparent;
Let's see the pictures.
Color
Before
After
PictureBox
Before
After (here foreground were changed, don't mind this)
Generally, labels and textboxes that appear in front of an image is best organized in a panel. When rendering, if labels need to be transparent to an image within the panel, you can switch to image as parent of labels in Form initiation like this:
var oldParent = panel1;
var newParent = pictureBox1;
foreach (var label in oldParent.Controls.OfType<Label>())
{
label.Location = newParent.PointToClient(label.Parent.PointToScreen(label.Location));
label.Parent = newParent;
label.BackColor = Color.Transparent;
}
This uses Graphics.CopyFromScreen so the control needs to be added when it's visable on screen.
public partial class TransparentLabelControl : Label
{
public TransparentLabelControl()
{
this.AutoSize = true;
this.Visible = false;
this.ImageAlign = ContentAlignment.TopLeft;
this.Visible = true;
this.Resize += TransparentLabelControl_Resize;
this.LocationChanged += TransparentLabelControl_LocationChanged;
this.TextChanged += TransparentLabelControl_TextChanged;
this.ParentChanged += TransparentLabelControl_ParentChanged;
}
#region Events
private void TransparentLabelControl_ParentChanged(object sender, EventArgs e)
{
SetTransparent();
if (this.Parent != null)
{
this.Parent.ControlAdded += Parent_ControlAdded;
this.Parent.ControlRemoved += Parent_ControlRemoved;
}
}
private void Parent_ControlRemoved(object sender, ControlEventArgs e)
{
SetTransparent();
}
private void Parent_ControlAdded(object sender, ControlEventArgs e)
{
if (this.Bounds.IntersectsWith(e.Control.Bounds))
{
SetTransparent();
}
}
private void TransparentLabelControl_TextChanged(object sender, EventArgs e)
{
SetTransparent();
}
private void TransparentLabelControl_LocationChanged(object sender, EventArgs e)
{
SetTransparent();
}
private void TransparentLabelControl_Resize(object sender, EventArgs e)
{
SetTransparent();
}
#endregion
public void SetTransparent()
{
if (this.Parent!= null)
{
this.Visible = false;
this.Image = this.takeComponentScreenShot(this.Parent);
this.Visible = true;
}
}
private Bitmap takeComponentScreenShot(Control control)
{
Rectangle rect = control.RectangleToScreen(this.Bounds);
if (rect.Width == 0 || rect.Height == 0)
{
return null;
}
Bitmap bmp = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(bmp);
g.CopyFromScreen(rect.Left, rect.Top, 0, 0, bmp.Size, CopyPixelOperation.SourceCopy);
return bmp;
}
}
An easy way to have a label with a picture behind it is to use the Image Property of the label itself. This will print the text over the top of the picture, and enable you to align the image (top/bottom/left/right/centre) as required.picture