Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
3.00/5 (4 votes)
I am working on the book Pro .NET 2.0 Windows Forms and Custom Controls.
on chapter 12 a gradient panel is created. I try to replicate the process but can not.

if I create a project as windows control then I will have a file named as GradientPanel.cs. this file is assoicated with GradientPanel.Designer.cs.

I downloaded the online source and find this project. we have GradientPanel.cs file,
but this file is assoicated with GradientPanel.resx.

Can anybody tell me how to replicate this custom control as online source code example? what is the difference between the two projects?

Thanks a million.


here is the link for the book and source code.
http://www.apress.com/9781590594391 [^]
you can look at the source code tab in the bottom section of this page.

you can download the source code and look at chapter 12 - GradientPanel project.
Thanks.
Posted
Updated 27-May-11 14:00pm
v3
Comments
Ali Al Omairi(Abu AlHassan) 27-May-11 18:02pm    
where is the online code exsample?

I don't have your book's code, but I think it does not really matter. Perhaps it helps when you understand the purpose of the files a little better. I assume you use a version of Visual Studio. Your book is about Win Forms 2.0, so you will need at least VS2005. Any version, even VS2005 Express will do.

When you add an item to a project, Visual Studio offers templates from which you can choose. In your case this would be the template for a UserControl or a CustomControl. Consult your book to find out which is the right one.

Select the template for your control and give it a name. Visual Studio will then add the files for an empty control to the project. Some of the files are optional (like xxx.designer.cs) and it depends on the template wether they are added right away or not. Do not get confused by that. For a control with the name xxx you would eventually get the following files in the project folder:

xxx.cs: This is the source code file where the class which represents your control is declared. It is meant to be edited by you. Here you should fill in the code from your book, usually consisting of the classes constructors, helper methods, event handlers, properties and member variables. Just remember: This is your playground. Any code you write has to go into this file.

xxx.designer.cs: This file is optional in the sense that you could just as well write all the code which is in here into xxx.cs and it would work. Most important: You rarely have to look at this file and are not supposed to write anything into it at all. When you use the designer to add further controls to your control, the designer automaticcly writes the needed code. This automatically generated code goes into this file. This way the designer does not interfere with the code you have written in xxx.cs and, to avoid trouble, you should not change anything in this file. Use the design view, the toolbox and the properties window to recreate the control's design from the book. Later you can try to modify it or even come up with a design of your own.

Edit: For starters, you should replicate the design from the book precisely. Especially this applies to the types of controls and their Names. It will obviously not work if you substitute a control for another type or if you give it another name than the one used in the code.

xxx.resx: Some of the settings you make in the designer are impractical to implement in code. For now, just regard this file as a list of properties and their values which is maintained by the designer. This stuff becomes really important when you design your control to support different languages. Then you will have a resource file for every supported language and the right one will be selected at runtime. For now, just change the properties of the controls in your layout with the designer and the properties view and let the designer worry about the contents of this file.


I hope this helps a little, but I wonder that your book does not mention any of this. Have you skipped some 'uninteresting' parts perhaps?
 
Share this answer
 
v2
Comments
Southmountain 30-May-11 0:15am    
Thank you for your time to share this. It really helps.
Designer.CS file is associate with the controls / forms to keep the properties and values that are serialized in the designer, For instance you added a button to the form and change its DockStyle,

In this case the designer will serialize the DockStyle when the value is changed and is not the default value of the control.

This designer file can be removed by moving the Initialize component method to the corresponding form.

Designer.resx : For instance you create a control and change its Image / Icon, this information will be serialized as a stream and kept in resx files.

Not all the controls requires a designer / resources, for instance a simple gradient panel could be the following one!.

C#
public class GradientPanel : Panel
{
    public Color GradientBegin { get; set; }
    public Color GradientEnd { get; set; }

    public GradientPanel()
        : base()
    {
        this.GradientBegin = Color.Yellow;
        this.GradientEnd = Color.Green;
    }

    protected override void OnPaintBackground(PaintEventArgs e)
    {
        using (LinearGradientBrush brush = new                          
                       LinearGradientBrush(this.ClientRectangle, 
                       this.GradientBegin,
                       this.GradientEnd,
                       LinearGradientMode.Horizontal))
        {
            e.Graphics.FillRectangle(brush, this.ClientRectangle);
        }
    }
}


Hope this helps!

Vallarasu S.
 
Share this answer
 
v2
Comments
Southmountain 30-May-11 0:10am    
Thank you, Vallarasu. It really helps to validate my understanding for custom control.
VallarasuS 31-May-11 13:03pm    
Happy to be a help!

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