Click here to Skip to main content
15,911,896 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear Team,

While creating custom controls, I want to override some property values like button height, width, font, etc, also label, textbox controls.
But when override onPaint event not working all properties while creating button, label, textbox controls.

please provide code to generate custom control for us.

Tags: windows application,windows control library.

What I have tried:

While calling override onpaint event, properties value cant be changing, when execute the our controls.
Posted
Updated 5-Feb-19 3:57am
Comments
OriginalGriff 1-Feb-19 5:04am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with. So show us the code that isn't working, and try to explain what it does that you didn't expect or doesn't do that your did.

Use the "Improve question" widget to edit your question and provide better information.
Mohankumar.Engain 1-Feb-19 5:14am    
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
g.Clear(BackColor);
g.SmoothingMode = SmoothingMode.HighQuality;
Pen p = new Pen(Color.Black, 2);
Rectangle fillRect = new Rectangle(10, 10, 40, 40);
Brush b = new SolidBrush(Color.FromArgb(240, 240, 240));

//AutoSize = false;
g.FillRectangle(b, fillRect);
//g.FillRectangle(new SolidBrush(Color.Black), 100, 50, 100, 100);
g.DrawRectangle(p, e.ClipRectangle);

g.DrawString(Text, new Font("Segoe UI", 10, FontStyle.Regular), new SolidBrush(Color.Black), new Point(4, 4));

b.Dispose(); //ADD THIS
p.Dispose(); //ADD THIS TOO

}

For above Created in => Windows Form Control Library,
I have used button control to change the property values for custom control.
Button width and height property values are not changed.
Ralf Meier 1-Feb-19 6:25am    
I'm sorry - I didn't got you ...
Please explain again (perhaps with an example in words) what you want to achieve ...
Mohankumar.Engain 4-Feb-19 1:37am    
If try above code using constructor then properties which we are used, those properties shows in .designer.cs file too.I want to restrict that too. pls if we using override only we will restrict .designer.cs file. but still not get expected result ..

Quote:
I have used button control to change the property values for custom control.
Button width and height property values are not changed.
Well ... that code doesn't use any variable values except the ClipRectangle you are passed which is the total size of the control drawing area. Everything else in there is a constant value, so I wouldn't expect anything to change, unless your external Button code is changing the actual width and height of the whole control.

So what does your button code do, and what effect did you expect it to have on your control?

BTW: If you use a using block, your Dispose is called for you when teh object goes out of scope:
C#
using (Pen p = new Pen(Color.Black, 2))
    {
    Rectangle fillRect = new Rectangle(10, 10, 40, 40);
    using (Brush b = new SolidBrush(Color.FromArgb(240, 240, 240)))
       {
        g.FillRectangle(b, fillRect);
        g.DrawRectangle(p, e.ClipRectangle);
        using (Brush black = new SolidBrush(Color.Black))
            {
            using (Font myFont = new Font("Segoe UI", 10, FontStyle.Regular))
                {
                g.DrawString(Text, myFont, black, new Point(4, 4));
                }
            }
        }
    }
But ... I'd create them outside the method in private static class level variables so there is only ever one of them created - it'll save time in your Paint method which is important as it has an effect of total performance of your app. And they don't need to be Disposed at all, so no Handles leaking around like your code has!
 
Share this answer
 
Comments
Mohankumar.Engain 4-Feb-19 1:38am    
If try above code using constructor then properties which we are used, those properties shows in .designer.cs file too.I want to restrict that too. pls if we using override only we will restrict .designer.cs file. but still not get expected result ..
All controls in System.Windows.Form namespace inherits from Control class[^].

What this means to you?

You can use somthing like this to change some properties of your custom control:

C#
public class MyCustomButton : Button
{
    public MyCustomButton()
    {
        this.Width = 180
	    this.Height = 64
	    this.Font = New System.Drawing.Font("Segoe UI", 10, System.Drawing.FontStyle.Regular)
	    this.Text = "My Custom Button"
    }
}

//usage:
MyCustomButton mcb = new MyCustomButton();
mcb.Location = new System.Drawing.Point(20, 20);
mcb.Parent = this; //Form

VB.NET
Public Class MyCustomButton
	Inherits Button
	
	Public Sub New()
		With Me
			.Width = 180
			.Height = 64
			.Font = New System.Drawing.Font("Segoe UI", 10, System.Drawing.FontStyle.Regular)
			.Text = "My Custom Button"
		End With
	End Sub
	
End Class

'usage:
Dim mcb As MyCustomButton = New MyCustomButton()
With mcb
    .Location = New System.Drawing.Point(20, 20)
    .Parent = Me 'Form
End With



For further details, please see:
Classes - C# Programming Guide | Microsoft Docs[^]
Developing Custom Windows Forms Controls with the .NET Framework | Microsoft Docs[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900