Page 1 of 1

Your Own Control Library Rate Topic: -----

#1 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,352
  • Joined: 29-May 08

Posted 26 January 2011 - 03:34 PM

Writing Your Own Control Library.

The basic procedure to create a library of

Create a new Windows Forms Application. (Applicable to WPF also)
  • Under the Projects Properties change the Application Type to Class Library (A DLL).
    Note: It's is also a good idea to change the name of the Assembly and Root namespace also. (see why later)
  • Delete the Form1.
  • Add a new UserControl
  • Design and Code the Control
  • Save the Project.


Example

Let's create a DigitPad Control.

Here's what mine looks like.

Attached Image

Public Class DigitKeyPad

  Private Sub Digit0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Digit0.Click, Digit1.Click, Digit2.Click,
    Digit3.Click, Digit4.Click, Digit5.Click, Digit6.Click, Digit7.Click, Digit8.Click, Digit9.Click
    Dim c As Button = TryCast(sender, Button)
    If c IsNot Nothing Then RaiseEvent DigitPressed(New DigitPressedArgs(Integer.Parse(c.Text)))
  End Sub


Create an Event which is fired every time a button is pressed.
Public Event DigitPressed(ByVal e As DigitPressedArgs)


The code which enables the value to be passed as a read-only property of the EventArgs.
 
  Public Class DigitPressedArgs
    Inherits EventArgs
    Protected mValue As Integer = 0
    Public ReadOnly Property Digit() As Integer
      Get
        Return mValue
      End Get
    End Property
    Friend Sub New(ByVal value As Integer)
      mValue = value
    End Sub
  End Class
End Class



Using the Library in a Project

After creating our control library let's using in a separate project.

Create a new Windows Forms Application.

Project -> Add Reference
Under the Browse Tab, locate and select the previously created generated DLL.


Public Class Form1
  Dim dp2 As DigitKeyPad

  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Programmatically 
    dp2 = New DigitKeyPad
    dp2.Left = 0
    dp2.Top = 0
    AddHandler dp2.DigitPressed, Sub(ee As DigitKeyPad.DigitPressedArgs)
                                   MessageBox.Show("DigitPad1: " & ee.Digit.ToString)
                                 End Sub
    Me.Controls.Add(dp2)
  End Sub
End Class



Tools -> Choose Items
Browse -> Select the previously create DLL
Now the control appears in the toolbox.

Public Class Form1
  Dim dp2 As DigitKeyPad

  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Programmatically 
    dp2 = New DigitKeyPad
    dp2.Left = 0
    dp2.Top = 0
    AddHandler dp2.DigitPressed, Sub(ee As DigitKeyPad.DigitPressedArgs)
                                   MessageBox.Show("DigitPad1: " & ee.Digit.ToString)
                                 End Sub
    Me.Controls.Add(dp2)
' Via Toolbox
    AddHandler Me.DigitKeyPad1.DigitPressed, Sub(ee As DigitKeyPad.DigitPressedArgs)
                                               MessageBox.Show("DigitPad2: " & ee.Digit.ToString)
                                             End Sub
  End Sub


End Class



Now you are Control Library creating coding ninja :shuriken:

Is This A Good Question/Topic? 3
  • +

Replies To: Your Own Control Library

#2 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1533
  • View blog
  • Posts: 4,919
  • Joined: 25-September 09

Posted 27 January 2011 - 05:37 AM

Nice one Adam, and very well written too.
Was This Post Helpful? 0
  • +
  • -

#3 Shealladh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 22-June 11

Posted 22 June 2011 - 12:11 AM

Is it me or is there a step by step guide to this?

For newcomers, this is not very helpful!
Was This Post Helpful? 0
  • +
  • -

#4 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 571
  • View blog
  • Posts: 2,979
  • Joined: 19-May 09

Posted 24 June 2011 - 02:31 PM

Nah, it's you. Beginners gotta start somewhere.
Was This Post Helpful? 0
  • +
  • -

#5 Douggie0001  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 03-November 11

Posted 21 November 2011 - 11:32 AM

you left out a line continuation character...
Was This Post Helpful? 0
  • +
  • -

#6 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,352
  • Joined: 29-May 08

Posted 21 November 2011 - 12:44 PM

Douggie0001: Where? In the vs2010 release of the language, line continuation character were made implicit, for nearly all the cases.
Was This Post Helpful? 0
  • +
  • -

#7 Douggie0001  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 03-November 11

Posted 21 November 2011 - 03:04 PM

View PostAdamSpeight2008, on 21 November 2011 - 12:44 PM, said:

Douggie0001: Where? In the vs2010 release of the language, line continuation character were made implicit, for nearly all the cases.


You missed a line continuation character in the first block of code right under the image of the numeric keypad. You never combined lines 3 and 4. I still use VB 2008, so I get one heck of a lot of errors with this. Is there anything for VB 2008 because VB 08 doesn't like a lot of the code and in an attempt to fix a syntax error, It gives me maximum errors.

I like VB 2008 and am probably going to stick with it for a while, maybe until the next edit for Visual Studio comes out. It has just stuck with me and I like it.

Sorry, back on topic here. I just have annoying errors while in VB 2008 and I am not inclined to switch to VB 2010 for one program, so I would like to know what refinements would need to be made to make the compiler for VB 2008 "like" the code and not spam me with 102 errors.

Note: I am actually gearing this to a create a customization dialog box accessible through a settings MenuStripItem to be able to make appearance changes to multiple projects at once. That is my final goal that I wish to achieve through this. (My way of integrating it into the project is sloppy and unsophisticated. I don't like it)
Was This Post Helpful? 0
  • +
  • -

#8 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,352
  • Joined: 29-May 08

Posted 21 November 2011 - 03:16 PM

The example usage project does use some VS2010 language features;- Implicit Line-Continuations is the major change to the language. It also utlises Multi-Statement Lambdas. Eg The Sub part of this section of code.

AddHandler dp2.DigitPressed, Sub(ee As DigitKeyPad.DigitPressedArgs)
                                   MessageBox.Show("DigitPad1: " & ee.Digit.ToString)
                                 End Sub


What has be added to different version? Read MSDN Article
You can still written vb2008 style in vb2010 version of the language.
Obtain a free edition, via the Visual Basic Express editions downloadable for Microsoft.

This post has been edited by AdamSpeight2008: 21 November 2011 - 03:22 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1