New to making controls..a few questions

Blake81

Well-known member
Joined
Feb 23, 2006
Messages
304
Location
Georgia, USA
Programming Experience
1-3
I'd like to make an LED to add to the toolbox, but I don't know where to begin. I'll probably make it so that it actually just changes the picture depending on if the LED is lit or not. How would I make this control, and can I make it work so that I can do something like this with it in the VB code?
VB.NET:
if LED1.Lit = True.....

Thanks for any help. I looked at that one link a few posts down, but I didn't find anything on it about making controls.
 
Making controls is a lot fun. and it's a ay for me to unwind if i get distracted from the daily grind. What you are proposing is absolutley possible, and probably not as much work as you might think.

Start by creating a new windows control library project, delete the usercontrol that the ide provides you with and add a new custom control to your project. This is where it all begins, and once you get into it you'll find that GDI+ is quite powerful.
Here's where to start.

Add a new windws forms project to your library solution and then click add reference on the new windows project. Click the projects tab and you library project should be in there. Double click it add it will add a project reference to your windows project. This means that when you rebuild you control library after making alterations. The changes will immediately be reflected in the windows forms app. So we need to add it to the toolbox so, first build you control library project, then right click the toolbox tab hit addremove tool box items, then hit browse navigate to bin directory of the control library project double click the DLL you will find in there and it will add your control to the toolbox. Drag it onto the form and you will see a big pile of nothing, because we haven't done anything yet.
Go back to your custom control code file and open the windows forms generated bit and add the following after the call to initialize component..

SetStyle(controlstyles.AllPaintingInWmPaint,True)
SetStyle(ConrtrolStyles.DoubleBuffer,True)
Setstyle(ControlStyles.UserPaint,True)
Setstyle(ControlStyles.Resize/Redraw,true)


Now you are ready to start painting your led. If you need any help give me a shout.
 
Thanks, that was very helpful. I haven't used the GDI before though, so I'm not sure how/where to paint it. I added those few lines of code you gave, but some of them are giving me errors. Well, never mind about the errors. I just fixed it. One was a typo, and the other was that Resize/Redraw is supposed to be ResizeRedraw. What happens now that I put that code in? Where do I draw the control, and how? Is it freehand? Thanks.
 
I found something about how to draw. Am I on the right track doing this programmatically? Is there any way to see what I have so far without building it and dropping it on a form? I'm doing something like this, as far as programming it.

VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] g [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Graphics = pe.Graphics
g.DrawEllipse(Pens.DarkRed, 10, 5, 10, 10)
[/SIZE]


I found some good pictures of LEDs online, but they don't have them in their lit and unlit states. Is it possible to use GDI to make something as good as this?

red.jpg
 
Last edited:
Sorry for all the posting. I just have a quick question. Is this what you meant by putting the code in where the control is initialized? I tried this and didn't see anything when I added this to the toolbox and dropped it on a form.
VB.NET:
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE][SIZE=2] CustomControl1
[/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] CustomControl1_Load([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Graphics, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] boundingrectangle [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Rectangle)
SetStyle(ControlStyles.AllPaintingInWmPaint, [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE][SIZE=2])
SetStyle(ControlStyles.DoubleBuffer, [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE][SIZE=2])
SetStyle(ControlStyles.UserPaint, [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE][SIZE=2])
SetStyle(ControlStyles.ResizeRedraw, [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] G [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Graphics
G = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].CreateGraphics
G.Clear([/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].BackColor)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] path [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Drawing.Drawing2D.GraphicsPath
path.AddEllipse([/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Rectangle(20, 20, 20, 20))
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] lBrush [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Drawing.Drawing2D.LinearGradientBrush([/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] RectangleF(0, 0, 20, 20), Color.White, Color.Green, System.Drawing.Drawing2D.LinearGradientMode.Vertical)
G.FillPath(lBrush, path)
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE]

 
No, the only thing that neds to go in the constructor is the setstyle commands. All of the rest of the painting ned to go into the overriden on paint event. So select overrides and then open up the paint event handler. All of the painting should be done in there. To answer your question yes something like that can be done with GDI+ and without too much code. Your paint event should look like this


VB.NET:
Protected Overrides sub yourcontrol Paint(byval e as system.windows.forms. painteventargs)
 
e.graphics.drawellipse(0,0,me.width,me.height)
e.graphics.fillellipse(brushes.red)
 
end sub
Start there. Have a look into lineargradient brushes, and pathgradient brushes also. Or you can even just use that bitmap you have posted above and then we could use a matrix to alter it's colors.
 
Back
Top