Im trying to create a button in C# with the image as the button, I don't really want to use a picture box as i Require the "Label" bit of the button as the text for the buttons aren't in the image, This is what it looks like
If anyone can help me fix that white frame it would be much appreciated, Thanks!
EDIT: I found what was causing it but still no fix :( The BackColor on the actual form is where the white is coming from but you can't set the BackColor of a form to Transparent :/
Your image must have transparent background. Also see #IAbstract's suggestion.
As I answered here to remove the border you should set the FlatAppearance.BorderColor to transparent. The whole code to remove completely the border looks like:
customButton.TabStop = false;
//it's the best thing set flatstyle to flat when dealing with a custom button
customButton.FlatStyle = FlatStyle.Flat;
customButton.FlatAppearance.BorderSize = 0;
//set the border color to transparent by setting the alpha to 0 (it doesn't support Color.Transparent)
customButton.FlatAppearance.BorderColor = Color.FromArgb(0, 0, 0, 0);
I think a Background.Color = Transparent should fix it if WinForms.
Update
If you have set border and background properties to transparent, double check the white border to be transparent in the actual graphic?
Related
I'm just messing about with the Visual Styles offered by .NET using Winforms and I have a problem. I am simply executing this code, called from the OnPaint method of a custom Panel, nothing special.
private void DrawBox(PaintEventArgs e)
{
ComboBoxRenderer.DrawDropDownButton(e.Graphics, e.ClipRectangle, ComboBoxState.Normal);}
My problem is that the Background Color of the Button is grey while the Background Color of a true ComboBox is white (on my Computer anyway).
Two questions here, why the Color change and how do I make my Button render using the same Background Color as a true ComboBox?
Thanks
Danny
ClipRectangle is not what you want. Set the actual rectangle you want to draw. That being said, yeah, using VisualStyles is going to make you mad. This will get you close:
VisualStyleRenderer vsr = new VisualStyleRenderer("EDIT", 1, 1);
vsr.DrawBackground(e.Graphics, controlRectangle);
vsr.SetParameters("COMBOBOX", 7, 1);
vsr.DrawBackground(e.Graphics, arrowRectangle);
Ok, I'm doing this:
UIButton btReturn;
btReturn = UIButton.FromType (UIButtonType.RoundedRect);
btReturn.Frame = btnBounds;
btReturn.SetTitle (Caption, UIControlState.Normal);
btReturn.Layer.CornerRadius = 14;
btReturn.ClipsToBounds = true;
btReturn.TouchUpInside += touchHandler;
btReturn.BackgroundColor = UIColor.FromRGB (0xe4, 0x96, 0x37);
return btReturn;
Which begs the question... Why the heck is it come up as WHITE background??? I originally tried to set it to clear with a background image, but that doesn't work either...
What could be wrong?
This is because the RoundedRect type of UIButton doesn't allow for any other colour background apart from white.
You'll have to use a UIButtonType.Custom to change the background colour (but you'll lose the nice rounded button and end up with a square button - you'll have to design your own fancy looking button in your favourite graphics package :)
Try like this,
btReturn.BackgroundView = null;
btReturn.BackgroundColor = UIColor.Black;
Also refer the following links,
How to change background color of UIButton in Monotouch
How to set the background color on a DialogViewController (Monotouch.Dialog)
Look here: is-it-even-possible-to-change-a-uibuttons-background-color there is an example aswell on how to create your own button, as you cannot modify the rounded button.
I have a form (see screenshot):
As you can see, its a pretty basic form, with a save button. I have programmed it so that if any of the text fields get changed, the "SAVE" button changes color so that its obvious that I haven't clicked save and don't forget to. Unfortunately, simply changing the BackColor of the button to red isn't enough, because its UGLY as sin.
What can I do to change the color of the button to red, but not as ugly. As you can see, the "BackColor" doesn't change the entire button, just the inner piece. The border is still the same old fashioned transparent grey.
A little bit of a LinearGradientBrush can go a long way to soften the harshness of a pure red button.
button1.ForeColor = Color.White;
Bitmap bmp = new Bitmap(button1.Width, button1.Height);
using (Graphics g = Graphics.FromImage(bmp)) {
Rectangle r = new Rectangle(0, 0, bmp.Width, bmp.Height);
using (LinearGradientBrush br = new LinearGradientBrush(
r,
Color.Red,
Color.DarkRed,
LinearGradientMode.Vertical)) {
g.FillRectangle(br, r);
}
}
then you can just assign the image to the button's BackgroundImage property:
button1.BackgroundImage = bmp;
Result:
Note: Assigning a background image will lose the mouse hover coloring of the button.
Another solution would be to add an Icon (e.g. exclamation mark) to the button instead to inform the user that the changes haven't been saved yet.
There are many tutorials online on how to create nice buttons with c#. For example this one allows you to create Vista like buttons. Have a look here: http://www.codeproject.com/Articles/19318/Vista-Style-Button-in-C
For basic colors visit this SO question:
C#: Changing Button BackColor has no effect
You can just use one of the button's properties, "FlatStyle". By default, it is standard. But if you switch it to "Popup", your background color will be extended to the area of the button. You can compare the followings:
left-standard, right-popup
This won't work in WinForms, but you might want to switch over to WPF.
It's much more convenient because you can configure EVERYTHING. (Even the color of a progressbar)
Edit: The OP doesn't have to entirely rewrite his application.
He just needs to redo the layout of it, the code can be C&P'd over to the new WPF project, fyi
Edit²: You don't even need to use code to change the color of a WPF button, you can just define a red overlay with 30% opacity in the XAML file. It's that easy, really.
Is there a way in which I can change the button color and preserve the Windows VisualStyle? I want to create a button that looks like checkBox2 (color and style)
this.button2.BackColor = System.Drawing.SystemColors.GradientActiveCaption;
this.button2.UseVisualStyleBackColor = false; //if true, BackColor gets ignored
No, not really. This kind of button background is drawn by VisualStyleRenderer.DrawBackground(). Which in turns pinvokes DrawThemeBackground(). These methods don't take a color. None is needed because the color is already specified in the theme.
Simulating the appearance with a LinearGradientBrush is your only real hope. Note that custom drawing a button is quite difficult, all the code is internal and no owner-draw is provided.
Consider using an image.
What's the quickest way to show a red/green light indicator on a C# form?
I originally thought about using radio buttons, but not sure how to set the color of the dot, only the foreground/background text.
Then I thought about drawing a circle. Couldn't find a toolbox shape for that, and didn't want to write code just to draw the circle.
Basically, I'm writing a little application specific monitor, that shows a red light if certain services are down, or certain web services are not responding.
This is what I have so far using a square button instead of a circle. The code is just what I want, I just want a round shape.
if (allGood)
{
btnIISIndicator.BackColor = Color.Green;
}
else
{
btnIISIndicator.BackColor = Color.Red;
}
This is simple, just use System.Windows.Shapes for the object and System.Windows.Media.Brushes for the colors.
For a circle you can do the following:
System.Windows.Shapes.Ellipse circle = new System.Windows.Shapes.Ellipse();
circle.Height = 20; //or some size
circle.Width = 20; //height and width is the same for a circle
circle.Fill = System.Windows.Media.Brushes.Red;
Then you can make a function to do your check for red and green.
Also, you can use hex values for the colors as well:
circle.Fill = new System.Windows.Media.SolidColorBrush((Color)ColorConverter.ConvertFromString("#RRGGBB"));
Not exactly related to the question at hand, but your code could be shortened somewhat using the ternary operator as such:
btnIISIndicator.BackColor = allGood ? Color.Green : Color.Red;
But that all depends on your (or your organization's) definition of readability and maintainability.
I would just make a panel or PictureBox and set the Background image to that of a red/green light. Either make the images in PhotoShop/PaintShop/MS Paint or download some stock images off the web.
Whenever the status changes, just swap the image out.
just try this it works for me.
SolidColorBrush solidColor=new SolidColorBrush();
solidColor.Color=Colors.Red;
ellips_circle.Fill=solidColor;
Use an image, but theres some great icons available here so you dont have to actually make some.
Create red and green bitmaps and use the PictureBox control to show the bitmaps.
I just use some standard images and put them in a picturebox. works great on our apps.
I simply used a non enabled button as indicator since I did not manage to install the WinUI for shapes. Same suggestion as question but simplified.
indicatorButton.Enabled = false;
...
if (allGood)
{
indicatorButton.BackColor = Color.Green;
indicatorButton.Text = "On";
}
else
{
indicatorButton.BackColor = Color.Red;
indicatorButton.Text = "Off";
}