Results 1 to 14 of 14

Thread: VS2012 Express Custom and User Control

  1. #1

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    VS2012 Express Custom and User Control

    There are a lot of uber-awesome software developers on this site that have created some spectacular and extremely helpful posts for the codebank. However, I have found that there are a lot of really simple questions that really don’t produce many search results, either on this site or Google. It’s not always a student or hobbyist that is looking to learn something new. Take my situation, for example. I started learning VB.NET back in 2003 with the .NET framework 1.1. It wasn’t until 2006 that I was actually employed in a position where I could use my VB skills professionally. In fact, if you look at my historical posts, you will find quite a few threads from 2006 and 2007 where I was asking some pretty basic questions about binding controls to a database.

    Fast forward to 2013 and I have become quite proficient with MDI data driven applications. Recently I was given a project that is taking me far beyond my comfort zone with VB.NET. I find myself asking some very basic questions on a daily basis that you wouldn’t think someone with ten years of experience would be asking. With this in mind, while I am on this journey to learning new things and expanding my repertoire , any time I have difficulty finding good examples or solid explanations I will create a post in the codebank for anyone that might be looking for the same information.
    Now for the task at hand; I’m sure some people may find this utterly appalling, but in the ten years that I have been writing VB.NET applications, I have never once had the need to create a custom or user control. My current project will require quite a few user controls, so it’s time to get my feet wet.

    For clarification, I am using Visual Studio 2012 Express for Windows Desktop with .NET Framework 4.5.

    For anyone that is new to this subject, there are two types of controls that you can create in Visual Studio: User Controls and Custom Controls.

    User Controls are the easiest to learn and the most basic. Think of them as a collection of existing controls such as buttons, text boxes and labels that you frequently place together to do similar tasks. By creating a user control, you only have to create their functionality once for use in multiple locations within your project. There are a few cons to a user control: Once you situate the child controls into your user control they remain static, which means you can’t change the layout later on. You also cannot enable themes in a user control. The biggest thing to remember is that User Controls will not show up in your toolbox for use in other projects. A user control is derived from System.Windows.Controls.UserControl.

    Custom Controls are the most flexible, but also have a longer learning curve. Custom controls can implement themes and can be dynamically configured for each application. They can also be displayed in your toolbox for easy drag and drop implementation in any project. Custom Controls are derived from System.Windows.Controls.Control.

    For my first project, I need to make a custom onscreen keyboard control for a touchscreen application. There are already a few examples of this such as this one by jmcilhinney:

    http://www.vbforums.com/showthread.p...ard&highlight=

    As much as I like John’s example, there are a few differences in my application that require me to make my own, plus making my own will help me understand the process better. However, there are a few features in jmcilhinney’s onscreen keyboard that I want to use in my own, so I promise I will give credit for any of your code that is used John!

    This project will evolve in a series of posts as I get further and further along.

    Now for the first obstacle:

    While researching user controls, I repeatedly encountered instructions that told me to start a new Visual Studio project based on the Windows Control Library template. Here’s the catch, if you are using the Express Edition of Visual Studio, you don’t have that template!

    Not to worry though. Making your own template is actually quite easy.

    Step 1: Start a new project using the Class Library template, which is available in the Express Edition. Name the project “WindowsControlLibrary”.
    Name:  Template1.png
Views: 8332
Size:  74.5 KB

    Step 2: Right-click on the project name in the Solution Explorer and select Add > New Item. Then select User Control.
    Name:  Template2.png
Views: 8298
Size:  57.2 KB

    Step 3: Save and build the project.
    Name:  Template3.png
Views: 7718
Size:  9.7 KB

    Step 4: Export the project as a template.
    Name:  Template4.png
Views: 7690
Size:  17.2 KB

    Step 5: In this example we’re going to create a project template.
    Name:  Template5.png
Views: 7827
Size:  40.7 KB
    Last edited by Siddharth Rout; Apr 7th, 2013 at 02:59 AM. Reason: Spelling Updated as requested by circuits2
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  2. #2

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: VS2012 Expres Custom and User Control

    Step 6: Give the template a description and icons if you so desire and then click Finish.
    Name:  Template6.png
Views: 8042
Size:  37.8 KB

    Step 7: Close the solution and start a new project, you will notice that WindowsControlLibrary is now an optional template.
    Name:  Template7.png
Views: 7669
Size:  53.9 KB




    More posts to follow!
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  3. #3

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: VS2012 Expres Custom and User Control

    With your new project, set the size of the control to 782 x 262. Next, set the background color to black, or whatever color you like best. Then, drag a button onto the control and position it at location 2, 2. Set the button's size to 50 x 50. Each additional button's position will be the original button width/height plus two.

    In my example, I have set the button's to FlatStyle.Flat with a backcolor of DimGrey, a bordercolor of Silver, and a ForeColor of White. I am also using Tahoma 12pt Bold as my font.

    The bottom left and right corners are blank filler panels that I am using to complete the image of a keyboard.

    Here are the sizes:

    General Button: 50 x 50
    Backspace: 102 x 50
    Backslash: 76 x 50
    Enter/Caps: 102 x 50
    Shift: 128 x 50
    Space: 362 x 50
    Fillers: 206 x 50

    Notice I am using the un-shifted values of each key for the button text, such as lower-case letters, numbers, etc. I don't have a need for the tab key, so I have omitted it in my project.

    Name:  Control1.png
Views: 7716
Size:  14.8 KB
    Last edited by circuits2; Apr 6th, 2013 at 01:39 AM.
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  4. #4

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: VS2012 Expres Custom and User Control

    After your buttons and panels are in place, name each control to something that is meaningful to you. In my case, I have named each button based on it's un-shifted value. For instance: BackTickBTN, OneBTN, TwoBTN, ThreeBTN, BackSpaceBTN, BackSlashBTN, LshiftBTN, RshiftBTN, etc.
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  5. #5
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: VS2012 Expres Custom and User Control

    I have informed the mods of the spelling error in the title.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  6. #6

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: VS2012 Expres Custom and User Control

    Thank you Nightwalker!

    Ok, let's start adding some code to our User Control. Now, there are many ways of doing this and I am going to give one way as an example. To those advanced programmers that may be reviewing this thread, I would appreciate any "Best Practice" tips that you may have with regards to the code I post.

    In my User Control, I have renamed the control class KeyboardCNTRL. There are two basic functions of a keyboard that we need to keep track of, Shift and Caps Lock. We can keep track of these two scenarios by assigning them a boolean:

    vb.net Code:
    1. Public Class KeyboardCNTRL
    2.  
    3.     ' ADD A VARIABLE TO CHECK IF CAPS LOCK IS ON
    4.     Private _CapsLock As Boolean = False
    5.  
    6.     ' ADD A VARIABLE TO CHECK IF SHIFT WAS PRESSED LAST
    7.     Private _Shift As Boolean = False
    8.  
    9. End Class


    Next, we need to add some code to keep track of what happens when the Caps Lock button is pressed. For the sake of this example I added two Subs, CapsLockOn and CapsLockOff, along with a button click event to determine which Sub needs to be called.

    vb.net Code:
    1. #Region "CAPS LOCK EVENTS"
    2.  
    3.     ' THIS SUB SIMULATES THE CAPS LOCK ON
    4.     Private Sub CapsLockOn()
    5.  
    6.         Me.QBTN.Text = "Q"
    7.         Me.WBTN.Text = "W"
    8.         Me.EBTN.Text = "E"
    9.         Me.RBTN.Text = "R"
    10.         Me.TBTN.Text = "T"
    11.         Me.YBTN.Text = "Y"
    12.         Me.UBTN.Text = "U"
    13.         Me.IBTN.Text = "I"
    14.         Me.OBTN.Text = "O"
    15.         Me.PBTN.Text = "P"
    16.         Me.ABTN.Text = "A"
    17.         Me.SBTN.Text = "S"
    18.         Me.DBTN.Text = "D"
    19.         Me.FBTN.Text = "F"
    20.         Me.GBTN.Text = "G"
    21.         Me.HBTN.Text = "H"
    22.         Me.JBTN.Text = "J"
    23.         Me.KBTN.Text = "K"
    24.         Me.LBTN.Text = "L"
    25.         Me.ZBTN.Text = "Z"
    26.         Me.XBTN.Text = "X"
    27.         Me.CBTN.Text = "C"
    28.         Me.VBTN.Text = "V"
    29.         Me.BBTN.Text = "B"
    30.         Me.NBTN.Text = "N"
    31.         Me.MBTN.Text = "M"
    32.  
    33.     End Sub
    34.  
    35.     ' THIS SUB SIMULATES THE CAPS LOCK OFF
    36.     Private Sub CapsLockOff()
    37.  
    38.         Me.QBTN.Text = "q"
    39.         Me.WBTN.Text = "w"
    40.         Me.EBTN.Text = "e"
    41.         Me.RBTN.Text = "r"
    42.         Me.TBTN.Text = "t"
    43.         Me.YBTN.Text = "y"
    44.         Me.UBTN.Text = "u"
    45.         Me.IBTN.Text = "i"
    46.         Me.OBTN.Text = "o"
    47.         Me.PBTN.Text = "p"
    48.         Me.ABTN.Text = "a"
    49.         Me.SBTN.Text = "s"
    50.         Me.DBTN.Text = "d"
    51.         Me.FBTN.Text = "f"
    52.         Me.GBTN.Text = "g"
    53.         Me.HBTN.Text = "h"
    54.         Me.JBTN.Text = "j"
    55.         Me.KBTN.Text = "k"
    56.         Me.LBTN.Text = "l"
    57.         Me.ZBTN.Text = "z"
    58.         Me.XBTN.Text = "x"
    59.         Me.CBTN.Text = "c"
    60.         Me.VBTN.Text = "v"
    61.         Me.BBTN.Text = "b"
    62.         Me.NBTN.Text = "n"
    63.         Me.MBTN.Text = "m"
    64.  
    65.     End Sub
    66.  
    67.     ' THIS SUB HANDLES THE CAPS LOCK BUTTON CLICK EVENT
    68.     Private Sub Caps_Click(ByVal sender As Object, e As EventArgs) Handles CapsBTN.Click
    69.         If _CapsLock Then
    70.             CapsLockOff()
    71.             _CapsLock = False
    72.         Else
    73.             CapsLockOn()
    74.             _CapsLock = True
    75.         End If
    76.     End Sub
    77.  
    78. #End Region


    And for the Shift behavior, I have added ShiftOn and ShiftOff as well as the Click event for each of the shift keys:

    vb.net Code:
    1. #Region "SHIFT EVENTS"
    2.  
    3.     ' THIS SUB SIMULATES THE SHIFT KEY ON
    4.     Private Sub ShiftOn()
    5.  
    6.         ' SET BUTTON TEXT VALUES TO SHIFT CHARACTERS
    7.         Me.BackTickBTN.Text = Chr(152).ToString()
    8.         Me.OneBTN.Text = Chr(33).ToString()
    9.         Me.TwoBTN.Text = Chr(64).ToString()
    10.         Me.ThreeBTN.Text = Chr(35).ToString()
    11.         Me.FourBTN.Text = Chr(36).ToString()
    12.         Me.FiveBTN.Text = Chr(37).ToString()
    13.         Me.SixBTN.Text = Chr(94).ToString()
    14.         Me.SevenBTN.Text = "&&"
    15.         Me.EightBTN.Text = Chr(42).ToString()
    16.         Me.NineBTN.Text = Chr(40).ToString()
    17.         Me.ZeroBTN.Text = Chr(41).ToString()
    18.         Me.HyphenBTN.Text = Chr(95).ToString()
    19.         Me.EqualsBTN.Text = Chr(43).ToString()
    20.         Me.LbracketBTN.Text = Chr(123).ToString()
    21.         Me.RbracketBTN.Text = Chr(125).ToString()
    22.         Me.BackSlashBTN.Text = Chr(124).ToString()
    23.         Me.SemiColonBTN.Text = Chr(58).ToString()
    24.         Me.ApostropheBTN.Text = Chr(34).ToString()
    25.         Me.CommaBTN.Text = Chr(60).ToString()
    26.         Me.PeriodBTN.Text = Chr(62).ToString()
    27.         Me.FwdSlashBTN.Text = Chr(63).ToString()
    28.  
    29.         If Not _CapsLock Then
    30.             Me.QBTN.Text = "Q"
    31.             Me.WBTN.Text = "W"
    32.             Me.EBTN.Text = "E"
    33.             Me.RBTN.Text = "R"
    34.             Me.TBTN.Text = "T"
    35.             Me.YBTN.Text = "Y"
    36.             Me.UBTN.Text = "U"
    37.             Me.IBTN.Text = "I"
    38.             Me.OBTN.Text = "O"
    39.             Me.PBTN.Text = "P"
    40.             Me.ABTN.Text = "A"
    41.             Me.SBTN.Text = "S"
    42.             Me.DBTN.Text = "D"
    43.             Me.FBTN.Text = "F"
    44.             Me.GBTN.Text = "G"
    45.             Me.HBTN.Text = "H"
    46.             Me.JBTN.Text = "J"
    47.             Me.KBTN.Text = "K"
    48.             Me.LBTN.Text = "L"
    49.             Me.ZBTN.Text = "Z"
    50.             Me.XBTN.Text = "X"
    51.             Me.CBTN.Text = "C"
    52.             Me.VBTN.Text = "V"
    53.             Me.BBTN.Text = "B"
    54.             Me.NBTN.Text = "N"
    55.             Me.MBTN.Text = "M"
    56.         End If
    57.  
    58.     End Sub
    59.  
    60.     ' THIS SUB SIMULATES THE SHIFT KEY OFF
    61.     Private Sub ShiftOff()
    62.  
    63.         ' SET BUTTON TEXT VALUES TO NO-SHIFT CHARACTERS
    64.         Me.BackTickBTN.Text = Chr(96).ToString()
    65.         Me.OneBTN.Text = "1"
    66.         Me.TwoBTN.Text = "2"
    67.         Me.ThreeBTN.Text = "3"
    68.         Me.FourBTN.Text = "4"
    69.         Me.FiveBTN.Text = "5"
    70.         Me.SixBTN.Text = "6"
    71.         Me.SevenBTN.Text = "7"
    72.         Me.EightBTN.Text = "8"
    73.         Me.NineBTN.Text = "9"
    74.         Me.ZeroBTN.Text = "0"
    75.         Me.HyphenBTN.Text = Chr(45).ToString()
    76.         Me.EqualsBTN.Text = Chr(61).ToString()
    77.         Me.LbracketBTN.Text = Chr(91).ToString()
    78.         Me.RbracketBTN.Text = Chr(93).ToString()
    79.         Me.BackSlashBTN.Text = Chr(92).ToString()
    80.         Me.SemiColonBTN.Text = Chr(59).ToString()
    81.         Me.ApostropheBTN.Text = Chr(39).ToString()
    82.         Me.CommaBTN.Text = Chr(130).ToString()
    83.         Me.PeriodBTN.Text = Chr(46).ToString()
    84.         Me.FwdSlashBTN.Text = Chr(47).ToString()
    85.  
    86.         If Not _CapsLock Then
    87.  
    88.             Me.QBTN.Text = "q"
    89.             Me.WBTN.Text = "w"
    90.             Me.EBTN.Text = "e"
    91.             Me.RBTN.Text = "r"
    92.             Me.TBTN.Text = "t"
    93.             Me.YBTN.Text = "y"
    94.             Me.UBTN.Text = "u"
    95.             Me.IBTN.Text = "i"
    96.             Me.OBTN.Text = "o"
    97.             Me.PBTN.Text = "p"
    98.             Me.ABTN.Text = "a"
    99.             Me.SBTN.Text = "s"
    100.             Me.DBTN.Text = "d"
    101.             Me.FBTN.Text = "f"
    102.             Me.GBTN.Text = "g"
    103.             Me.HBTN.Text = "h"
    104.             Me.JBTN.Text = "j"
    105.             Me.KBTN.Text = "k"
    106.             Me.LBTN.Text = "l"
    107.             Me.ZBTN.Text = "z"
    108.             Me.XBTN.Text = "x"
    109.             Me.CBTN.Text = "c"
    110.             Me.VBTN.Text = "v"
    111.             Me.BBTN.Text = "b"
    112.             Me.NBTN.Text = "n"
    113.             Me.MBTN.Text = "m"
    114.            
    115.         End If
    116.  
    117.     End Sub
    118.  
    119.     ' THIS SUB HANDLES THE SHIFT BUTTON CLICK EVENTS
    120.     Private Sub Shift_Click(ByVal sender As Object, e As EventArgs) Handles LshiftBTN.Click, RshiftBTN.Click
    121.         If _Shift Then
    122.             ShiftOff()
    123.             _Shift = False
    124.         Else
    125.             ShiftOn()
    126.             _Shift = True
    127.         End If
    128.     End Sub
    129.  
    130. #End Region


    Run your project and make sure the right keys change to their appropriate values when Caps Lock and Shift are used.
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  7. #7

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: VS2012 Express Custom and User Control

    If you run your project at this point, you will notice that the only properties available in the Property Explorer on the right are those of the underlying inherited control, which in our case is System.Windows.Forms.Controls.UserControl.

    What if we wanted to allow a little more flexibility during design time, such as the ability to change the colors of the buttons and panels? The properties of the child controls within our User Control are hidden because they are members of the User Control and are not properties of the control itself. In order to expose the properties of our buttons and panels we need to create some properties in our User Control that can manage the changes.

    I like to keep my properties towards the top of my class code. Create a new region below the Shift and Caps booleans called "PUBLIC PROPERTIES" and add the following code.

    Hint: Adding properties and descriptions is extremely fast! To add a property simply start typing property, when "Property" is selected by intellisense you can simply hit "Tab" twice and Visual Studio will add the new property template for you. To create the description, position your cursor at the first line immediately before your new property declaration and type three single quotes ('). Visual Studio will then enter the summary template for you.

    vb.net Code:
    1. #Region "PUBLIC PROPERTIES"
    2.  
    3.     ''' <summary>
    4.     ''' Gets or Sets the BackColor of the Keyboard Buttons.
    5.     ''' </summary>
    6.     ''' <remarks></remarks>
    7.     Private _ButtonBackColor As System.Drawing.Color = Drawing.Color.DimGray
    8.     Public Property ButtonBackColor() As System.Drawing.Color
    9.         Get
    10.             Return _ButtonBackColor
    11.         End Get
    12.         Set(ByVal value As System.Drawing.Color)
    13.             _ButtonBackColor = value
    14.             For Each c As System.Windows.Forms.Control In Me.Controls()
    15.                 If TypeOf (c) Is System.Windows.Forms.Button Then
    16.                     c.BackColor = value
    17.                 End If
    18.             Next
    19.         End Set
    20.     End Property
    21.  
    22.     ''' <summary>
    23.     ''' Gets or Sets the ForeColor of the Keyboard Buttons.
    24.     ''' </summary>
    25.     ''' <remarks></remarks>
    26.     Private _ButtonForeColor As System.Drawing.Color = Drawing.Color.White
    27.     Public Property ButtonForeColor() As System.Drawing.Color
    28.         Get
    29.             Return _ButtonForeColor
    30.         End Get
    31.         Set(ByVal value As System.Drawing.Color)
    32.             _ButtonForeColor = value
    33.             For Each c As System.Windows.Forms.Control In Me.Controls()
    34.                 If TypeOf (c) Is System.Windows.Forms.Button Then
    35.                     c.ForeColor = value
    36.                 End If
    37.             Next
    38.         End Set
    39.     End Property
    40.  
    41.     ''' <summary>
    42.     ''' Gets or Sets the BorderColor of the Keyboard Buttons.
    43.     ''' </summary>
    44.     ''' <remarks></remarks>
    45.     Private _ButtonBorderColor As System.Drawing.Color = Drawing.Color.White
    46.     Public Property ButtonBorderColor() As System.Drawing.Color
    47.         Get
    48.             Return _ButtonBorderColor
    49.         End Get
    50.         Set(ByVal value As System.Drawing.Color)
    51.             _ButtonBorderColor = value
    52.             For Each c As System.Windows.Forms.Control In Me.Controls()
    53.                 If TypeOf (c) Is System.Windows.Forms.Button Then
    54.                     DirectCast(c, System.Windows.Forms.Button).FlatAppearance.BorderColor = value
    55.                 End If
    56.             Next
    57.         End Set
    58.     End Property
    59.  
    60.     ''' <summary>
    61.     ''' Gets or Sets the BackColor of the Keyboard Panels.
    62.     ''' </summary>
    63.     ''' <remarks></remarks>
    64.     Private _PanelBackColor As System.Drawing.Color = Drawing.Color.White
    65.     Public Property PanelBackColor() As System.Drawing.Color
    66.         Get
    67.             Return _PanelBackColor
    68.         End Get
    69.         Set(ByVal value As System.Drawing.Color)
    70.             _PanelBackColor = value
    71.             For Each c As System.Windows.Forms.Control In Me.Controls()
    72.                 If TypeOf (c) Is System.Windows.Forms.Panel Then
    73.                     c.BackColor = value
    74.                 End If
    75.             Next
    76.         End Set
    77.     End Property
    78.  
    79. #End Region

    Now, these are just a few properties that you can create to expose your buttons and panels. You can go absolutely crazy here and expose all of the properties if you so desire. However, for the sake of this example, I will stop with these.

    If you run your project now you will find the new properties at the bottom of the Property Explorer. Play around with them a little so you can see the effects.
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  8. #8

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: VS2012 Express Custom and User Control

    Up to this point we have only been working on the "look and feel" of our keyboard control. It wouldn't be of much use to us without some actual functionality.

    If you look at the arguments listed in a normal button click event you will see something like this:

    vb.net Code:
    1. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    2.  
    3. End Sub

    I want to bring your attention to the "e As EventArgs" portion. In many cases you won't have any use for the data provided by EventArgs. However, in some cases there can be a treasure trove of useful information.

    For our keyboard control, we want to be able to extract the characters that are pressed by using an event. With a User Control that we have created, we need to provide any information that might be needed later on down the road.

    You might not understand why this is needed yet, but in a moment you will see its importance.

    Right-click on your project name in the Solution Explorer and select Add > Class. Name your new class KeyboardKeyPressEventArgs.

    Now add the following code to your class:

    vb.net Code:
    1. Public Class KeyboardKeyPressEventArgs
    2.     Inherits EventArgs
    3.  
    4.     ''' <summary>
    5.     ''' Gets or Sets the Character sent by the Keyboard
    6.     ''' </summary>
    7.     ''' <remarks></remarks>
    8.     Private _KeyChar As Char
    9.     Public Property KeyChar() As Char
    10.         Get
    11.             Return _KeyChar
    12.         End Get
    13.         Set(ByVal value As Char)
    14.             _KeyChar = value
    15.         End Set
    16.     End Property
    17.  
    18.     ' SETS THE KEYCHAR VALUE WHEN A NEW INSTANCE IS CALLED
    19.     Public Sub New(ByVal SendChar As Char)
    20.         KeyChar = SendChar
    21.     End Sub
    22.  
    23. End Class


    Now that we have done a little prep work, we need to add an event.

    Events are very much like Properties in that the only Events that are available during design time are those of the underlying control itself, not the ones raised by the child controls. If we want our keyboard to be useful, then we need to expose an event that is raised by our keyboard buttons when they are clicked.

    Add a new region to the bottom of your keyboard control code and call it "BUTTON CLICK EVENTS". Create a new event inside this region called KeyboardKeyPressed, like this:

    vb.net Code:
    1. #Region "BUTTON CLICK EVENTS"
    2.  
    3.     ' THIS IS THE EVENT THAT IS RAISED WHEN A CHARACTER KEY IS PRESSED
    4.     Public Event KeyboardKeyPressed(ByVal sender As Object, ByVal e As KeyboardKeyPressEventArgs)
    5.  
    6. #End Region

    Notice the arguments for our event. We are using e As KeyboardKeyPressEventArgs, which is the class we just created.

    With our event and arguments in place, let's put them to use. You could very easily put all of this into one single sub, but I have broken mine into pieces to make it easier to understand what is going on.

    The names of your buttons may not be the same as mine, but you should be able to understand what buttons I am talking about by the names I have used.

    Add this code to your BUTTON CLICK EVENTS region below your KeyboardKeyPressed event:

    vb.net Code:
    1. ' HANDLE THE BUTTON CLICK EVENT FOR KEYS WITH CHARACTERS
    2. Private Sub CharacterButtons_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BackTickBTN.Click, _
    3.                                                     OneBTN.Click, TwoBTN.Click, ThreeBTN.Click, FourBTN.Click, _
    4.                                                     FiveBTN.Click, SixBTN.Click, SevenBTN.Click, EightBTN.Click, _
    5.                                                     NineBTN.Click, ZeroBTN.Click, HyphenBTN.Click, EqualsBTN.Click, _
    6.                                                     QBTN.Click, WBTN.Click, EBTN.Click, RBTN.Click, TBTN.Click, _
    7.                                                     YBTN.Click, UBTN.Click, IBTN.Click, OBTN.Click, PBTN.Click, _
    8.                                                     LbracketBTN.Click, RbracketBTN.Click, BackSlashBTN.Click, _
    9.                                                     ABTN.Click, SBTN.Click, DBTN.Click, FBTN.Click, GBTN.Click, _
    10.                                                     HBTN.Click, JBTN.Click, KBTN.Click, LBTN.Click, SemiColonBTN.Click, _
    11.                                                     ApostropheBTN.Click, ZBTN.Click, XBTN.Click, CBTN.Click, _
    12.                                                     VBTN.Click, BBTN.Click, NBTN.Click, MBTN.Click, CommaBTN.Click, _
    13.                                                     PeriodBTN.Click, FwdSlashBTN.Click
    14.  
    15.         ' RAISE THE KEYBOARDKEYPRESSED EVENT AND SEND THE CHARACTER IN THE BUTTON'S TEXT
    16.         RaiseEvent KeyboardKeyPressed(sender, New KeyboardKeyPressEventArgs(CChar(DirectCast(sender, System.Windows.Forms.Button).Text)))
    17.  
    18.         ' RESET THE SHIFT BOOLEAN IF A KEY IS PRESSED
    19.         _Shift = False
    20.         ShiftOff()
    21.  
    22. End Sub
    23.  
    24. ' HANDLE THE BUTTON CLICK EVENT FOR THE BACKSPACE BUTTON
    25. Private Sub BackSpaceBTN_Click(sender As Object, e As EventArgs) Handles BackSpaceBTN.Click
    26.  
    27.         ' RAISE THE KEYBOARDKEYPRESSED EVENT AND SEND THE CHARACTER IN THE BUTTON'S TEXT
    28.         RaiseEvent KeyboardKeyPressed(sender, New KeyboardKeyPressEventArgs(Microsoft.VisualBasic.ChrW(System.Windows.Forms.Keys.Back)))
    29.  
    30.         ' RESET THE SHIFT BOOLEAN IF A KEY IS PRESSED
    31.         _Shift = False
    32.         ShiftOff()
    33.  
    34. End Sub
    35.  
    36. ' HANDLE THE BUTTON CLICK EVENT FOR THE ENTER BUTTON
    37. Private Sub EnterBTN_Click(sender As Object, e As EventArgs) Handles EnterBTN.Click
    38.  
    39.         ' RAISE THE KEYBOARDKEYPRESSED EVENT AND SEND THE CHARACTER IN THE BUTTON'S TEXT
    40.         RaiseEvent KeyboardKeyPressed(sender, New KeyboardKeyPressEventArgs(Microsoft.VisualBasic.ChrW(System.Windows.Forms.Keys.Return)))
    41.  
    42.         ' RESET THE SHIFT BOOLEAN IF A KEY IS PRESSED
    43.         _Shift = False
    44.         ShiftOff()
    45.  
    46. End Sub
    47.  
    48. ' HANDLE THE BUTTON CLICK EVENT FOR THE SPACEBAR BUTTON
    49. Private Sub SpaceBTN_Click(sender As Object, e As EventArgs) Handles SpaceBTN.Click
    50.  
    51.         ' RAISE THE KEYBOARDKEYPRESSED EVENT AND SEND THE CHARACTER IN THE BUTTON'S TEXT
    52.         RaiseEvent KeyboardKeyPressed(sender, New KeyboardKeyPressEventArgs(Microsoft.VisualBasic.ChrW(System.Windows.Forms.Keys.Space)))
    53.  
    54.         ' RESET THE SHIFT BOOLEAN IF A KEY IS PRESSED
    55.         _Shift = False
    56.         ShiftOff()
    57.  
    58. End Sub

    Notice how our button events are used to raise the public event that we have created. This is how we can get information out of our control and into the program that is using the keyboard.

    When we drop this keyboard into a Form to use, we can handle the KeyboardKeyPressed event to extract the appropriate e.KeyChar from the KeyboardKeyPressEventArgs.
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  9. #9

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: VS2012 Express Custom and User Control

    Let's see if our keyboard is working like it should. Remember to build your Keyboard Control project one last time so all of your changes are reflected in the DLL.

    Start a new Windows Forms Application project.

    Right-click on the project in the solution explorer and select Add > Existing Project:

    Name:  Add1.png
Views: 8124
Size:  27.5 KB

    Navigate to your Keyboard Control project and click the Open button.

    Your Keyboard Control project should now be added to your new WindowsApplication project:

    Name:  Add2.png
Views: 7361
Size:  11.5 KB

    You will also see it in your toolbox:

    Name:  Add3.png
Views: 7516
Size:  15.1 KB

    Drag the keyboard control from your toolbox onto Form1. Add a textbox above the keyboard. It should look something like this:

    Name:  Add4.png
Views: 9926
Size:  20.9 KB

    Finally, handle the KeyboardKeyPressed event that we created in the form's code:

    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Private Sub KeyboardCNTRL1_KeyboardKeyPressed(sender As Object, e As AndysKeyboard.KeyboardKeyPressEventArgs) Handles KeyboardCNTRL1.KeyboardKeyPressed
    4.  
    5.         Me.TextBox1.AppendText(e.KeyChar.ToString())
    6.  
    7.     End Sub
    8.  
    9. End Class

    Save and run your project. Try pressing the buttons to see if it works.... Mine did!

    Name:  Add5.png
Views: 7376
Size:  51.9 KB
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  10. #10
    New Member
    Join Date
    Mar 2015
    Posts
    1

    Re: VS2012 Express Custom and User Control

    Hi...this works great for my application, but the backspace doesn't trigger and so returns the ASCII character instead. Also for the "Enter" button as well. How do I solve this?

  11. #11
    Hyperactive Member
    Join Date
    Sep 2014
    Posts
    404

    Re: VS2012 Express Custom and User Control

    Just a small change i would personally make, when changing the case of your letters instead of

    Code:
     Me.QBTN.Text = "Q"
            Me.WBTN.Text = "W"
            Me.EBTN.Text = "E"
            Me.RBTN.Text = "R"
            Me.TBTN.Text = "T"
            Me.YBTN.Text = "Y"
            Me.UBTN.Text = "U"
            Me.IBTN.Text = "I"
            Me.OBTN.Text = "O"
            Me.PBTN.Text = "P"
            Me.ABTN.Text = "A"
            Me.SBTN.Text = "S"
            Me.DBTN.Text = "D"
            Me.FBTN.Text = "F"
            Me.GBTN.Text = "G"
            Me.HBTN.Text = "H"
            Me.JBTN.Text = "J"
            Me.KBTN.Text = "K"
            Me.LBTN.Text = "L"
            Me.ZBTN.Text = "Z"
            Me.XBTN.Text = "X"
            Me.CBTN.Text = "C"
            Me.VBTN.Text = "V"
            Me.BBTN.Text = "B"
            Me.NBTN.Text = "N"
            Me.MBTN.Text = "M"
    why not do this:

    - set either a custom property to identify the type of key you are working with or use the tag property to identify alphabetic keys

    then do this
    Code:
    For Each cntl As Button In Me.Controls.OfType(Of Button)()
    
    If cntl.Tag = "Alpha" then
    cntl.Text.ToUpper()
    
    Next
    on .ToLower for the other way round?

  12. #12
    New Member
    Join Date
    Sep 2015
    Posts
    1

    Re: VS2012 Express Custom and User Control

    Great demo of how to make control. I have one question. I get a dot in a black square when i press the back key from the on screen keyboard. Just wondering if I have done something wrong. I have copied and pasted your code. I have just altered the button names.

    Thanks
    Last edited by PeterC; Sep 24th, 2015 at 07:04 AM.

  13. #13
    New Member
    Join Date
    Nov 2018
    Posts
    1

    Re: VS2012 Express Custom and User Control

    theirs an error it is not working help me please.
    my problem is in the last

    Private Sub KeyboardCNTRL1_KeyboardKeyPressed(sender As Object, e As AndysKeyboard.KeyboardKeyPressEventArgs) Handles KeyboardCNTRL1.KeyboardKeyPressed



    TextBox1.AppendText(e.KeyChar.ToString())
    End Sub

    Name:  212121.jpg
Views: 1805
Size:  25.0 KBName:  212121.jpg
Views: 1805
Size:  25.0 KB

  14. #14
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: VS2012 Express Custom and User Control

    The overloads in your signature are different,

    e As AndysKeyboard.KeyboardKeyPressEventArgs

    your image

    e as eventarghs

    My Github - 1d3nt

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width