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));
}
}
Related
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.
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;
}
We use the standard GroupBox and the Flat-Style. The form backgroundcolor is Gainsboro.
On my Windows 7 development machine it looks like this:
However, when running the app in a Windows Server 2016 Machine, it looks like this:
The borders are gone (not visible).
It appears to has something to do with the Background Color, but we're not sure how to fix it. When using a light blue Color, this happens on Server 2016:
Do you guys have any clue, why we can't see the white border with the BG-Color Gainsboro? It doesn't make any sense....
I dont have server 2016 to test it, but maybe overriding the Paint event of the borderColor will solve this problem, here is a custom GroupBox control, you can change borderColor Color inside the constructor.
namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
CustomGroupBox gb = new CustomGroupBox();
gb.Location = new Point(5, 5);
gb.Size = new Size(200, 100);
this.Controls.Add(gb);
}
}
public class CustomGroupBox : GroupBox
{
private Color borderColor;
public Color BorderColor
{
get { return this.borderColor; }
set { this.borderColor = value; }
}
public CustomGroupBox()
{
this.borderColor = Color.Red;
}
protected override void OnPaint(PaintEventArgs e)
{
Size tSize = TextRenderer.MeasureText(this.Text, this.Font);
Rectangle borderRect = e.ClipRectangle;
borderRect.Y += tSize.Height / 2;
borderRect.Height -= tSize.Height / 2;
ControlPaint.DrawBorder(e.Graphics, borderRect, this.borderColor, ButtonBorderStyle.Solid);
Rectangle textRect = e.ClipRectangle;
textRect.X += 6;
textRect.Width = tSize.Width;
textRect.Height = tSize.Height;
e.Graphics.FillRectangle(new SolidBrush(this.BackColor), textRect);
e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), textRect);
}
}
}
I'm having a label with a paint event this is my code:
public void draw(MouseEventArgs e, User user)
{
Label label = new Label();
label.Tag = user;
label.Paint += Label_Paint;
label.Location = new System.Drawing.Point(e.X, e.Y);
label.Width = 50;
label.Height = 40;
label.Name = user.name;
label.Text = user.name;
this.mainPanel.Controls.Add(label);
}
private void Label_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen p = new Pen(Color.Black, 2);
Label label = (Label)sender;
g.DrawEllipse(p, label.Location.X, label.Location.Y, 300, 300);
}
My goal is to draw a oval around the label and show it on my mainPanel. The label shows up in my mainPanel but no oval is being drawn.
I'm using WinForm. Any help would be appreciated.
I have created a custom control and bind it to Form. I have draw graphics text in the control and added to Form. But it was not displaying the Form. This is my code.
//Create a custom control
public class DrawTextImage : Control
{
public void DrawBox(PaintEventArgs e, Size size)
{
e.Graphics.Clear(Color.White);
int a = 0;
SolidBrush textColor = new SolidBrush(Color.Black);
using (SolidBrush brush = new SolidBrush(Color.Red))
{
e.Graphics.FillRectangle(brush, new Rectangle(a, a, size.Width, size.Height));
e.Graphics.DrawString("Text", Font, textColor, new PointF(50, 50));
}
}
}
//Load Form1
public Form1()
{
InitializeComponent();
DrawTextImage call = new DrawTextImage();
call.Text = "TextControl";
call.Name = "TextContrl";
Size siz = new Size(200, 100);
call.Location = new Point(0, 0);
call.Visible = true;
call.Size = siz;
call.DrawBox(new PaintEventArgs(call.CreateGraphics(), call.ClientRectangle), siz);
this.Controls.Add(call);
}
Any help on this, what did I do wrong?
You should be using the control's own Paint event, not a custom method that you have to call manually.
public class DrawTextImage : Control
{
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.Clear(Color.White);
int a = 0;
SolidBrush textColor = new SolidBrush(Color.Black);
using (SolidBrush brush = new SolidBrush(Color.Red))
{
//Note: here you might want to replace the Size parameter with e.Bounds
e.Graphics.FillRectangle(brush, new Rectangle(a, a, Size.Width, Size.Height));
e.Graphics.DrawString("Text", Font, textColor, new PointF(50, 50));
}
}
}
Remove the call to DrawBox, it's unnecessary.
The Paint event is fired automatically whenever a redraw of the control surface is required. You can ask for this yourself in code by using the control's Invalidate() or Refresh() methods.