Remove button border when entered - c#

When I hover over buttons on my C# form I have them highlighted yellow like how Microsoft office products but I don't want it to show the button border. I've seen people mention FlatStyle or FlatAppearance but it doesn't seem to recognize those commands. I'm looking into rendering now but I'm new to C# and I'm certain there must be a simple way to do it, It's not something I want to spend a lot of time on if possible.
I must stress I've been reading through books on windows forms programming and haven't found any answers its not that I'm lazy but I often find SO a very good source with really good input.
Tried this:
this.TSVehicleButton.BorderStyle = None;
Tried this:
this.TSVehicleButton.System.Windows.Forms.BorderStyle = None;
I tried lots of things but didn't mention it as part of my question, I'm new to C# and didn't want to come across as stupid. People get a little bitchy when people put things that they tried and isn't right.

Use following on
public class CustomButton : Button
{
public CustomButton()
: base()
{
// Prevent the button from drawing its own border
FlatAppearance.BorderSize = 0;
FlatStyle = System.Windows.Forms.FlatStyle.Flat;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
// Draw Border using color specified in Flat Appearance
Pen pen = new Pen(FlatAppearance.BorderColor, 1);
Rectangle rectangle = new Rectangle(0, 0, Size.Width - 1, Size.Height - 1);
e.Graphics.DrawRectangle(pen, rectangle);
}
}
I might help you.

just use this.FlatStyle = FlatStyle.Flat;

Just set the following Button properties, for example at design time in the properties window:
FlatStyle=Flat
FlatAppearance.BorderSize=0
Or run the following code, for example for a button named button1:
button1.FlatStyle=True
button1.FlatAppearance.BorderSize=0

This worked perfectly for me, and it's so simple!
button.FlatAppearance.BorderColor = Color.FromArgb(0, 255, 255, 255); // transparent
button.FlatAppearance.BorderSize = 0;
Now the button has no borders, even when you enter the cursor. This worked perfectly on Visual Studio 2015.

there is a way of taking the border away from the button and its in the button properties.
First make sure you have clicked on the button so that the properties belong to the button.
on the properties pane go and find Flat Appearance and expand it by clicking on the + sign in front of it.
This will open more option for you, one of the option is border size which is set to 1, change this to 0,
Next, 3 lines below that there is a property called FlatStyle and its set to standard, this needs to be changed to Flat.
Flat
Job done!
Hope this helps

Related

WinForms Control Alpha Blending [duplicate]

TL;DR: Look at the picture below
So I'm trying to make a little picture, and I and people around me are kinda out of ideas.
I have a table (the sitting+eating one) in the middle (seen from above), and people sitting around it. Those people are round, as isthe table.
Every person has their own picturebox, I just use one picture, rotate it, and set it as image in the next box.
Thep roblem now is: The PictureBoxes of people on corners overlap the table with empty corner, in the image there is transparency there. It should show the table below it, but instead it shows the background of the Form :(
Edit: All backgrounds are set to transparent, the Form has the marble as background and white ("Window") as background colour.
I put one person in the back and one in the front, so it's easy to see:
Edit 2 (same as ocmment):
In the last two days I read this question about 10 times, and not one that described this exact problem has had an actual answer. When trying to push one of those, I was told I should post a new question.
Example: How to make picturebox transparent?
Transparency in winforms is kind of misleading, since it's not really transparency.
Winforms controls mimic transparency by painting the part of their parent control they would hide instead of their own background.
However, they will not paint the other controls that might be partially covered by them.
This is the reason your top most picture boxes hides your big picture box.
You can overcome this by creating a custom control that inherits from PictureBox and override its OnPaintBackground method (taken, with slight adjustments, from this code project article):
protected override void OnPaintBackground(PaintEventArgs e)
{
base.OnPaintBackground(e);
Graphics g = e.Graphics;
if (this.Parent != null)
{
var index = Parent.Controls.GetChildIndex(this);
for (var i = Parent.Controls.Count - 1; i > index; i--)
{
var c = Parent.Controls[i];
if (c.Bounds.IntersectsWith(Bounds) && c.Visible)
{
using (var bmp = new Bitmap(c.Width, c.Height, g))
{
c.DrawToBitmap(bmp, c.ClientRectangle);
g.TranslateTransform(c.Left - Left, c.Top - Top);
g.DrawImageUnscaled(bmp, Point.Empty);
g.TranslateTransform(Left - c.Left, Top - c.Top);
}
}
}
}
}
Microsoft have published a Knowledge base article to solve this problem a long time ago, however it's a bit out-dated and it's code sample is in VB.Net.
Another option is to paint the images yourself, without picture boxes to hold them, by using Graphics.DrawImage method.
The best place to do it is probably in the OnPaint method of the form.

WP8 Change the color behind the application bar

I am writing a WP8 app that overrides the color theme on the phone to always be white. Now before people discredit me for this decision, the app itself is supposed to be a messenger like application and the white background simply makes everything easier to read. In the future I do want to allow people to be able to choose between black or white in case battery life is important but I need to get over this hurdle first.
Currently the problem is that even after overriding the theme colors the color BEHIND the application bar still refuses to change. I'm not talking about the background color of the application bar but the rectangle drawn behind the application bar as it is animated to pop upwards from the bottom of the screen. It is very visible and it is quiet annoying even if it only appears for about a second.
I know there must be a way to do this as applications like Office, Google Mail and Skype all override the color theme and implement the white theme instead and they do not have this same problem.
If anyone could help that would be great!
I've found a solution but it's not a very nice one. If anyone finds a better solution please let me know.
I solved this problem by setting the application bar opacity to be near 1 but not 1 (I set it to 0.99). This will tell windows to not rescale the window (which is the cause of the black background).
I then set the bottom margin of that page to be the height of the application bar.
Here's the code for anyone interested:
private void panoramaMain_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Panorama p = (Panorama) sender;
if(p.SelectedIndex == 1) {
messageList.Margin = new Thickness(0, 0, 0, ApplicationBar.DefaultSize);
ApplicationBar.IsVisible = true;
} else {
messageList.Margin = new Thickness(0, 0, 0, 0);
ApplicationBar.IsVisible = false;
}
}

How to make a UserControls BackColor transparent in C#?

I created a simple stick man in a Windows Form User-Control (consisting of a radio button and three labels and one progress bar).
I set the back-color of the new user-control to transparent so that when I drag it onto my form, it blends with other colors and drawings on the form.
I am not getting what I'm trying to achieve.
Here is the picture:
UserControl already supports this, its ControlStyles.SupportsTransparentBackColor style flag is already turned on. All you have to do is set the BackColor property to Color.Transparent.
Next thing you have to keep in mind in that this transparency is simulated, it is done by asking the Parent of the control to draw itself to produce the background. So what is important is that you get the Parent set correctly. That's a bit tricky to do if the parent is not a container control. Like a PictureBox. The designer will make the Form the parent so you will see the form's background, not the picture box. You'll need to fix that in code, edit the form constructor and make it look similar to this:
var pos = this.PointToScreen(userControl11.Location);
userControl11.Parent = pictureBox1;
userControl11.Location = pictureBox1.PointToClient(pos);
In constructor set style of control to support a transparent backcolor
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
and then set Background to transperent color
this.BackColor = Color.Transparent;
From MSDN
A more complex approach (and possibly working one) is described here - with overrding of CreateParams and OnPaint.
Why all those things?
UserControl class has property Region.
Set this to what ever shape you like and no other adjustments are needed.
public partial class TranspBackground : UserControl
{
public TranspBackground()
{
InitializeComponent();
}
GraphicsPath GrPath
{
get
{
GraphicsPath grPath = new GraphicsPath();
grPath.AddEllipse(this.ClientRectangle);
return grPath;
}
}
protected override void OnPaint(PaintEventArgs e)
{
// set the region property to the desired path like this
this.Region = new System.Drawing.Region(GrPath);
// other drawing goes here
e.Graphics.FillEllipse(new SolidBrush(ForeColor), ClientRectangle);
}
}
The result is as in the image below:
No low level code, no tweaking, simple and clean.
There is however one issue but in most cases it can go undetected, the edges are not smooth and anti-aliasing will not help either.
But the workaround is fairly easy. In fact much easier than all those complex background handling..

Borderless Winform with a 1px border

This might sound like a weird question but I have C# Winform that I set the FormBorderStyle to None. So far everything is good but I was wondering if there was a way to add like a 1px border on around my form ? I know I could do it by creating my own image but I was wondering if there was a more natural way of doing it.
Thanks
I consider using an image, or creating unnecessary controls for something that is easily paintable using GDI+ a waste of resources.
I think the simplest solution is overriding the OnPaint method of your form and drawing the border yourself:
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.DrawRectangle(Pens.Black, this.Bounds);
}
Of course, you may also use your own Pen with your own color and width.
Use padding 1;1;1;1 to your form and set a background color to your form, and put a panel to your form. Set white or other normal background color to the panel. And set dock in parent controller. The background color of the form will act as a border.
How about just adding a Panel (and setting it's border) to the Form?
Thanks for the suggestions, I've decided to create 4 1px label and just toss on the edge on each side. That way:
1. They are minding their own business on the side rather than taking up the whole middle if you use use a groupbox or panel.
2. You are able to choose change your border color.
There is no more natural or non natural ways to do it. It depends on what you want.
If you put a background image on the form, you have to consider a fact that in order to be able to support resizable for you have to have resizable background images.
If you simply draw on the background with a Pen or Brush, you can support also resizable form, but you have to work more if you want to do something cool, instead with image it's easier.
You can embed some control inside the form and with color's of them make a feeling of the border. Like control, you can use Panel, as suggested in comment, can use GroupBox that creates thin broder arround, or something else.
I created this method, so you could easily set the borderposition, color and thickness.
private void customBackgroundPainter(PaintEventArgs e, int linethickness = 2, Color linecolor = new Color(), int offsetborder = 6)
{
Rectangle rect = new Rectangle(offsetborder, offsetborder, this.ClientSize.Width - (offsetborder * 2), this.ClientSize.Height - (offsetborder * 2));
Pen pen = new Pen(new Color());
pen.Width = linethickness;
if (linecolor != new Color())
{
pen.Color = linecolor;
}
else
{
pen.Color = Color.Black;
}
e.Graphics.DrawRectangle(pen, rect);
}
You could use it in the OnPaintBackground likes this:
protected override void OnPaintBackground(PaintEventArgs e)
{
base.OnPaintBackground(e);
customBackgroundPainter(
e,
linethickness: 3,
linecolor: Color.DarkOrange,
offsetborder: 5
);
}

Fade between Tab Pages in a Tab Control

I have a Tab Control with multiple Tab Pages. I want to be able to fade the tabs back and forth. I don't see an opacity option on the Tab Controls. Is there a way to cause a fade effect when I switch from one Tab Page to another?
There is no magic Fade switch in the standard windows control.
You could dump the content of the tab to a bitmap (using DrawToBitmap or CopyFromScreen?), show that bitmap in front of the TabControl, switch tabs, and then fade the bitmap.
I decided to post what I did to get my solution working. GvS had the closest answer and sent me on my quest in the right direction so I gave him (might be a her, but come on) the correct answer check mark since I can't give it to myself.
I never did figure out how to "crossfade" from one tab to another (bring opacity down on one and bring opacity up on the other) but I found a wait to paint a grey box over a bitmap with more and more grey giving it the effect of fading into my background which is also grey. Then I start the second tab as a bitmap of grey that I slowly add less grey combined with the tab image each iteration giving it a fade up effect.
This solution leads to a nice fade effect (even if I do say so myself) but it is very linear. I am going to play a little with a Random Number Generator for the alphablend variable and see if that might make it a little less linear, but then again the users might appreciate the predictability. Btw, I fire the switch tab event with a button_click.
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
public int alphablend;
public Bitmap myBitmap;
private void button1_Click(object sender, EventArgs e)
{
alphablend = 0;
pictureBox1.Visible = true;
myBitmap = new Bitmap(tabControl1.Width, tabControl1.Height);
while (alphablend <= 246)
{
tabControl1.DrawToBitmap(myBitmap, new Rectangle(0, 0, tabControl1.Width, tabControl1.Height));
alphablend = alphablend + 10;
pictureBox1.Refresh();//this calls the paint action
}
tabControl1.SelectTab("tabPage2");
while (alphablend >= 0)
{
tabControl1.DrawToBitmap(myBitmap, new Rectangle(0, 0, tabControl1.Width, tabControl1.Height));
alphablend = alphablend - 10;
pictureBox1.Refresh();//this calls the paint action
}
pictureBox1.Visible = false;
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Graphics bitmapGraphics = Graphics.FromImage(myBitmap);
SolidBrush greyBrush = new SolidBrush(Color.FromArgb(alphablend, 240, 240, 240));
bitmapGraphics.CompositingMode = CompositingMode.SourceOver;
bitmapGraphics.FillRectangle(greyBrush, new Rectangle(0, 0, tabControl1.Width, tabControl1.Height));
e.Graphics.CompositingQuality = CompositingQuality.GammaCorrected;
e.Graphics.DrawImage(myBitmap, 0, 0);
}
I don't see a specification of Winform/Webform, so I'll assume WebForm...
You can use an AJAX AnimationExtender.
Failing that, a bad way (which would work) would be to accept a QueryString that causes the page to auto-navigate to the tab you want, and use page Transitions.
For Winforms, you could use WPF :)
I don't believe that the WPF TabControl can be brought to fade from one TabItem to the next, since it is not equipped to display multiple TabItems simultaneously.
You could try to emulate this by stitching together two ListViews (one for the tab strip and one for the panel) and add the effect into the ControlTemplate of the latter. Use the ControlTemplates of the TabControl and the ListView as starting point.
Depending on how your tabs/page work, you may be able to handle it at the page level by adding meta tags to the page:
<meta http-equiv="Page-Enter" content="blendTrans(Duration=0)">
<meta http-equiv="Page-Exit" content="blendTrans(Duration=0)">
Just change the duration to make the fade longer or shorter. This is commonly referred to as FAJAX.

Categories

Resources