Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

[ GDI Development ] #1 : Control Designer

Intro

Maybe most of you noticed that some default controls that are already exist in VS has some properties while others don't, for i.e : Panel is a common control that maybe everyone of us used even for once, it inherts ScrollableControl that has some properties different than control properties, in short when you add a panel to you project forms you simply see a small traingle in right top corner of the panel which is "Dock in Parent Container" property, have you ever thought how can do this in Custom for our controls ?, in 3 lessons I'll try to tech you some GDI Techniques, and I'll start with control designer class, which has a lot of benefits for GDI Developers , unfortunately I don't saw anyone here use it even for once (includes me).




[ GDI Development ]

Contains Three Lessons :

Spoiler (Click to View)




Control Designer

From MSDN : ControlDesigner provides a base class for designers of components that derive from Control. In addition to the methods and functionality inherited from the ComponentDesigner class, ControlDesigner provides additional methods to support extending and altering the behavior of an associated Control at design time.

As I said Control Designer has a lot of benefits and lot of features that can be found here.

in this tutorial I will show one of the methods of Control Designer effect which really important for GDI Developers and the users also and its PostFilterProperties

with PostFilterProperties we can change and remove any properties of a custom control.

Q : what is the need of removing properties ?
- for the most custom controls you just inherit the control base class which has a lot of none-needed properties , like we don't need the "Background Image" property in evrey control's PropertyGrid , by this we can simply remove the un-wanted properties.




Let's turn this into the code

guess we have a custom button

C#.NET

Spoiler (Click to View)



Visual Basic.NET

Spoiler (Click to View)



Let's say we want to remove 4 properties of this control.
  1. BackgroundImage

  2. BackgroundImageLayout

  3. Visible

  4. Dock



we will use "PostFilterProperties" method.

for being much easy we will store all those 4 properties above in array.

C#.NET

Code
private readonly string[] _PropertiesToRemove = { "BackgroundImage" , "BackgroundImageLayout", "Visible", "Dock" };

Visual Basic.NET

Code
Private ReadOnly _PropertiesToRemove As String() = new String(){ "BackgroundImage" , "BackgroundImageLayout", "Visible", "Dock" }




now we will create a class for the designer and inherit the ControlDsigner

Note : You need you import "System.Design" as referense.

C#.NET

Spoiler (Click to View)


Visual Basic.NET

Spoiler (Click to View)




now let's join this class with the Custom Control above. for that we will go to the top of custom control and add this line :

C#.NET

Code
[System.ComponentModel.Designer(typeof(NButtonDesigner))]

Visual Basic.NET

Code
<System.ComponentModel.Designer(GetType(NButtonDesigner))>





The Whole Code :

C#.NET

Spoiler (Click to View)


Visual Basic.NET

Spoiler (Click to View)


Result

Spoiler (Click to View)




you can remove all properties except those you want to remain

C#.NET

Spoiler (Click to View)


Visual Basic.NET

Spoiler (Click to View)


Result

Spoiler (Click to View)


There are a lot of useful usings of ControlDsigner but this is enough for now.

Enjoy!
[Image: narwinsig.gif]
Who the hell still uses winforms lol
(Sep 6, 2017 12:12 PM)tweety Wrote:  Who the hell still uses winforms lol

What's wrong with WinForms ?
[Image: narwinsig.gif]
thanks for sharing nice stuff mate .
i hope if u do more gdi posts would be great
(Sep 6, 2017 12:12 PM)tweety Wrote:  Who the hell still uses winforms lol

WinForms is the best for quick GUI development.
Could you recommend something better?
For the latest crimes linked to marijuana see: AttackerSmokedCannabis.com
(Sep 6, 2017 12:33 PM)Cuthbert Wrote:  WinForms is the best for quick GUI development.
Could you recommend something better?

Personally I love to fuck with WPF it''s so easy to design something simple.

About 5-10mins of design work and thats cause I kept getting lost in a Netflix show :)
Spoiler (Click to View)

But to op nice tut keep up the good work
Hi
(Sep 6, 2017 12:44 PM)sdk Wrote:  Personally I love to fuck with WPF it''s so easy to design something simple.

About 5-10mins of design work and thats cause I kept getting lost in a Netflix show :)
Spoiler (Click to View)

But to op nice tut keep up the good work

Isn't WPF quite time consuming to learn and develop, it's essentially learning a whole different language just for a GUI?
For the latest crimes linked to marijuana see: AttackerSmokedCannabis.com
(Sep 6, 2017 01:14 PM)Cuthbert Wrote:  Isn't WPF quite time consuming to learn and develop, it's essentially learning a whole different language just for a GUI?

It's just XAML, took me a day or two to pick it up. I used to be big into Android dev so the whole XML view based development wasn't new to me.

In my eyes it's worth it to be able to make such easy designs and they are unique, especially if you do work for people, you can provide unique "themes" per person
Hi
I usually prefer to avoid ControlDesigner, you can achieve the same effect using the EditorBrowsable (MSDN) and Browsable (MSDN) attributes without over-complicating your code with long string arrays. This also allows you more functionality for runtime handling and such.
(Sep 6, 2017 02:01 PM)uint32 Wrote:  I usually prefer to avoid ControlDesigner, you can achieve the same effect using the EditorBrowsable (MSDN) and Browsable (MSDN) attributes without over-complicating your code with long string arrays. This also allows you more functionality for runtime handling and such.

if you use EditorBrowsable or just Browsable you need to declare every property you want to hide , which is make the job more difficult as I see.

and also there is another useful trick when you use ControlDesigner (Action Lists) which I mention it in the next tutorial.
[Image: narwinsig.gif]
(Sep 6, 2017 02:15 PM)Narwin Wrote:  if you use EditorBrowsable or just Browsable you need to declare every property you want to hide , which is make the job more difficult as I see.

and also there is another useful trick when you use ControlDesigner (Action Lists) which I mention it in the next tutorial.

It isn't much more difficult, just takes some more space. I do prefer to take the long path because if you need to rename a member and it's placed in a string literal, IntelliSense won't be able to find the reference and won't perform the rename in the string and that can lead to other errors that are harder to trace.
Recreating the property also allows you to control it's functionality at runtime; throwing exceptions, controlling input...
The only advantage I see to using ControlDesigner is to conditionally hide members based on user input and such.
(Sep 6, 2017 02:41 PM)uint32 Wrote:  It isn't much more difficult, just takes some more space. I do prefer to take the long path because if you need to rename a member and it's placed in a string literal, IntelliSense won't be able to find the reference and won't perform the rename in the string and that can lead to other errors that are harder to trace.
Recreating the property also allows you to control it's functionality at runtime; throwing exceptions, controlling input...
The only advantage I see to using ControlDesigner is to conditionally hide members based on user input and such.

let's you have a lot of custom controls and which everyone of them inherit a Control and you want to hide some shared properties in every control, you need to declare them separetely or try to create a base class in for then inherit it but it still makes no differense with ControlDesigner, every property you hide inside it you can use it in runtime or change it, this just clearify the ProperyGrid from un-wanted properties, and as I said you need the ControlDesigner for implementing the ActionLists with make the jobs much easier and the user dosn't need to go for ProperyGrid to change a property value.
[Image: narwinsig.gif]
(Sep 6, 2017 02:41 PM)uint32 Wrote:  It isn't much more difficult, just takes some more space. I do prefer to take the long path because if you need to rename a member and it's placed in a string literal, IntelliSense won't be able to find the reference and won't perform the rename in the string and that can lead to other errors that are harder to trace.
Recreating the property also allows you to control it's functionality at runtime; throwing exceptions, controlling input...
The only advantage I see to using ControlDesigner is to conditionally hide members based on user input and such.

Just as a note, this is why C# has nameof().
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
Bookmarked!

Nice tutorial would be great if you post more =p

Thanks for sharing man.
Very nice tut. keep up the good work.
thank you very much. ;)


Possibly Related Threads...
Thread / Author Replies Views Last Post
[.NET] by ๖ۣۜMephobiaNov 14, 2025 07:54 PM 171 21,478 Nov 14, 2025 07:54 PM
Last Post: ProxyLand
[.NET] by QuobiAug 6, 2022 02:23 PM 0 407 Aug 6, 2022 02:23 PM
Last Post: Quobi
[.NET] by PolarisGangJun 21, 2022 10:09 AM 1 353 Jun 21, 2022 10:09 AM
Last Post: gigajew