How do you add a label/text to a manually created button? - c#

I have created a button using the code below. Adding the start.Text="Start"; statement does nothing. How do I add a label to my button?
public MainForm()
{
InitializeComponent();
myButtonObject start = new myButtonObject();
EventHandler myHandler = new EventHandler(start_Click);
start.Click += myHandler;
start.Location = new System.Drawing.Point(200, 500);
start.Size = new System.Drawing.Size(101, 101);
start.Text="Start";
// start.TextAlign = new System.Drawing.ContentAlignment.MiddleCenter;
this.Controls.Add(start);
}
public class myButtonObject : UserControl
{
// Draw the new button.
protected override void OnPaint(PaintEventArgs e)
{
Graphics graphics = e.Graphics;
Pen myPen = new Pen(Color.Black);
// Draw the button in the form of a circle
graphics.FillEllipse(Brushes.Goldenrod, 0, 0, 100, 100);
graphics.DrawEllipse(myPen, 0, 0, 100, 100);
myPen.Dispose();
}
}

You have implemented your own button as a user control. Since you are implementing OnPaint to provide your own draw functionality, you need to implement all the functionality like drawing the text too.
If you want to go down this route then you also need to add the logic to draw the text on the control in your OnPaint method. This can be done using the graphics.DrawString method.
See http://msdn.microsoft.com/en-us/library/system.drawing.graphics.drawstring.aspx
You also need to call graphics.dispose.
If you aren't familiar with this, then it might be simpler to use a UserControl and add a label to it, or something similar, and then to draw your circle shape on the top of that.

You should draw the button's text yourself in the OnPaint method:
TextRenderer.DrawText(graphics, Text, Font, new Point(5, 5), SystemColors.ControlText);
Where new Point(5, 5) - is a top left position of the text.

You paint the button ok but you don't draw the text.
A usercontrol doesn't do this by itself.
Just add something like this to the paint commands:
graphics.DrawString(Text, yourfont, yourBrush, x, y);
you must and may have to decide freely on x, y font and brush.

Related

Visual Studio C# pictureBox - drawing in From constructor doesn't show up

I have the problem that I want to draw on a pictureBox with an image loaded before the user gets to see it. The drawing in principle works, but if I do it in the constructor it doesn't show up. It does work if I call the same function with a button.
public Form1()
{
InitializeComponent();
draw();
}
public void draw()
{
pictureBox1.Refresh();
Graphics g = pictureBox1.CreateGraphics();
Rectangle rect = new Rectangle(new Point(20, 20), new Size(40, 40));
rect.Offset(8, 8);
g.DrawEllipse(new Pen(Brushes.Black, 2), rect);
g.Dispose();
}
private void button1_Click(object sender, EventArgs e)
{
draw();
}
I would have assumed that the circle would show up when the Form loads but it only does when I press the button. The code itself gets executed, as tested with a Message box.
The control paints itself in its Paint event, so as soon as it's shown on screen (ie, after your code), it will paint itself in dull gray as normal.
If you want to custom paint a control, any control, you either override its OnPaint function or subscribe to its exposed Paint event.
A hint should be the fact that you had to create your own Graphics object, as opposed to using the object constructed during normal painting operations, that also includes proper clipping handling.

How to make a section of panel transparent

I happen to be trying to do the following:
A side menu with white background color, semi-transparent, but for example, if you click on an option (if selected) it becomes completely transparent allowing you to see the background image through it.
In the image, the Label1 option is selected, where the label box must be transparent, Label1 must continue to be displayed and the rest of the panel must continue with its corresponding semi-transparent white color ...
Well, to achieve this I first tried drawing two boxes on the Form, but it turns out that the transparent is never drawn:
public partial class Form1 : Form
{
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
var brush = new SolidBrush(Color.FromArgb(25, Color.White));
e.Graphics.FillRectangle(brush, new Rectangle(0, 0, 50, this.Height));
var brush2 = new SolidBrush(Color.Transparent);
e.Graphics.FillRectangle(brush2, new Rectangle(0, 50, 50, 20));
}
}
In addition to the previous problem, another problem with this method is that whenever I want to move the transparent position box, I would be forced to invalidate the control using this.Invalidate();
Then I decided to use a panel:
public partial class Form1 : Form
{
protected override void OnLoad(EventArgs ev)
{
base.OnLoad(ev);
this.panel1.Paint += (s, e) =>
{
var brush = new SolidBrush(Color.FromArgb(25, Color.White));
e.Graphics.FillRectangle(brush, new Rectangle(0, 0, 50, this.Height));
var brush2 = new SolidBrush(Color.Transparent);
e.Graphics.FillRectangle(brush2, new Rectangle(0, 50, 50, 20));
};
}
}
BUT... The result is the same...
In summary, the selected option must be completely transparent allowing to see the background image but leaving the label visible, and the rest must be semi-transparent white.
How can I do this?

Custom control onPaint event not working

Hey people I have a problem I am writing a custom control. My control inherits from Windows.Forms.Control and I am trying to override the OnPaint method. The problem is kind of weird because it works only if I include one control in my form if I add another control then the second one does not get draw, however the OnPaint method gets called for all the controls. So what I want is that all my custom controls get draw not only one here is my code:
If you run the code you will see that only one red rectangle appears in the screen.
public partial class Form1 : Form
{
myControl one = new myControl(0, 0);
myControl two = new myControl(100, 0);
public Form1()
{
InitializeComponent();
Controls.Add(one);
Controls.Add(two);
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
public class myControl:Control
{
public myControl(int x, int y)
{
Location = new Point(x, y);
Size = new Size(100, 20);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Pen myPen = new Pen(Color.Red);
e.Graphics.DrawRectangle(myPen, new Rectangle(Location, new Size(Size.Width - 1, Size.Height - 1)));
}
}
I'm guessing you are looking for something like this:
e.Graphics.DrawRectangle(Pens.Red, new Rectangle(0, 0,
this.ClientSize.Width - 1,
this.ClientSize.Height - 1));
Your Graphic object is for the interior of your control, so using Location isn't really effective here. The coordinate system starts at 0,0 from the upper-left corner of the client area of the control.
Also, you can just use the built-in Pens for colors, otherwise, if you are creating your own "new" pen, be sure to dispose of them.
LarsTech beat me to it, but you should understand why:
All drawing inside of a control is made to a "canvas" (properly called a Device Context in Windows) who coordinates are self-relative. The upper-left corner is always 0, 0.
The Width and Height are found in ClientSize or ClientRectangle. This is because a window (a control is a window in Windows), has two areas: Client area and non-client area. For your borderless/titlebar-less control those areas are one and the same, but for future-proofing you always want to paint in the client area (unless the rare occasion occurs where you want to paint non-client bits that the OS normally paints for you).

Draw a line on a TabPage in C#

I am having trouble drawing a line on a TabPage.
I actually have a TabControl inside a TabControl. I have drawn a number of labels which I am using as boxes. I want to draw lines to join them together.
I have tried:
Pen P = new Pen(System.Drawing.Color.Black, 10);
tabname.CreateGraphics().DrawLine(P, 10, 10, 100, 100);
and
Pen P = new Pen(System.Drawing.Color.Black, 10);
tabcontrolname.TabPages[0].CreateGraphics().DrawLine(P, 10, 10, 100, 100);
Neither are displaying the line. I assume the line is being placed somewhere as there are no errors.
Any ideas how I can get it to display on the correct TabPage?
Thank you!
You probably need to override the OnPaint method (or handle the Paint event) to get this to work properly. If you don't your controls will just end up drawing over your lines.
Here's a link to the relevant docs.
Where do you try these codes, in which function? If you are doing it once in the initialization or construction, they will not be displayed as you expect. Whenever the control needs to be redrawn, you need to draw this line too, again. Either override the OnPaint method of the control or register for the Paint event and do the line drawing there.
I was able to get the arrow to show up using the following code:
TabPage.Paint += new PaintEventHandler(TabPage_Paint);
and
protected void TabPage_Paint(object sender, PaintEventArgs e)
{
base.OnPaint(e);
Pen arrow = new Pen(Brushes.Black, 4);
arrow.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
e.Graphics.DrawLine(arrow, 10, 10, 100, 100);
arrow.Dispose();
}
However, when scrolling is initiated the Paint messes up :(

How to assign a click event handler to part of a drawn rectangle?

Imagine I use the .NET graphic classes to draw a rectangle.
How could I then assign an event so that if the user clicks a certain point, or a certain point range, something happens (a click event handler)?
I was reading CLR via C# and the event section, and I thought of this scenario from what I had read.
A code example of this would really improve my understanding of events in C#/.NET.
Thanks
You can assign Click event handler to control whose surface will be used to draw rectangle.
Here is a small example:
When you click on form inside of rectangle it will be drawn with red border when you click outside it will be drawn with black border.
public partial class Form1 : Form
{
private Rectangle rect;
private Pen pen = Pens.Black;
public Form1()
{
InitializeComponent();
rect = new Rectangle(10, 10, Width - 30, Height - 60);
Click += Form1_Click;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.DrawRectangle(pen, rect);
}
void Form1_Click(object sender, EventArgs e)
{
Point cursorPos = this.PointToClient(Cursor.Position);
if (rect.Contains(cursorPos))
{
pen = Pens.Red;
}
else
{
pen = Pens.Black;
}
Invalidate();
}
}
PointToClient method translates cursor coordinates to control-relative coordinates. I.e. if you cursor is at (screenX, screenY) position on the screen it can be at (formX, formY) position relatively to form's top-left corner. We need to call it to bring cursor position into coordinate system used by our rectangle.
Invalidate method makes control to redraw itself. In our case it triggers OnPaint event handler to redraw rectangle with a new border color.

Categories