Saturday, August 22, 2020

Programming a Class to Create a Custom VB.NET Control

Programming a Class to Create a Custom VB.NET Control Building total custom parts can be an exceptionally propelled venture. Yet, you can fabricate a VB.NET class that has huge numbers of the upsides of a tool stash segment with considerably less exertion. Heres how! To get a kind of what you have to do to make a total custom part, attempt this examination: - Open another Windows Application venture in VB.NET.- Add a CheckBox from the Toolbox to the structure.- Click the Show All Files button at the highest point of Solution Explorer. This will show the records that Visual Studio makes for your venture (so you dont need to). As a verifiable commentary, The VB6 compiler did a great deal of very similar things, yet you never could get to the code since it was covered in assembled p-code. You could create custom controls in VB6 as well, yet it was significantly increasingly troublesome and required an uncommon utility that Microsoft provided only for that reason. In the Form Designer.vb document, you will find that the code underneath has been included consequently in the correct areas to help the CheckBox part. (On the off chance that you have an alternate adaptation of Visual Studio, your code may be somewhat extraordinary.) This is the code that Visual Studio composes for you. Required by the Windows Form Designer Private segments _ As System.ComponentModel.IContainerNOTE: The accompanying technique is requiredby the Windows Form DesignerIt can be changed utilizing the Windows Form Designer.Do not alter it utilizing the code editor.System.Diagnostics.DebuggerStepThrough() _Private Sub InitializeComponent() Me.CheckBox1 New System.Windows.Forms.CheckBox() Me.SuspendLayout() CheckBox1 Me.CheckBox1.AutoSize True Me.CheckBox1.Location New System.Drawing.Point(29, 28) Me.CheckBox1.Name CheckBox1. . . etc ... This is the code that you need to add to your program to make a custom control. Remember that all the techniques and properties of the real CheckBox control are in a class provided by the .NET Framework: System.Windows.Forms.CheckBox. This isnt part of your task since its introduced in Windows for every .NET program. In any case, theres a great deal of it. Another point to know about is that if youre utilizing WPF (Windows Presentation Foundation), the .NET CheckBox class originates from a totally extraordinary library named System.Windows.Controls. This article just works for a Windows Forms application, however the principals of legacy here work for any VB.NET venture. Assume your task needs a control that is a lot of like one of the standard controls. For instance, a checkbox that changed shading, or showed a small cheerful face as opposed to showing the little check realistic. Were going to construct a class that does this and tell you the best way to add it to your venture. While this may be helpful without anyone else, the genuine objective is to exhibit VB.NETs legacy. Lets Start Coding To begin, change the name of the CheckBox that you just added to oldCheckBox. (You should quit showing Show All Files again to disentangle Solution Explorer.) Now add another class to your task. There are a few different ways to do this including right-tapping the venture in Solution Explorer and choosing Add at that point Class or choosing Add Class under the Project menu thing. Change the document name of the new class to newCheckBox to keep things straight. At last, open the code window for the class and include this code: Open Class newCheckBox Inherits CheckBox Private CenterSquareColor As Color Color.Red Protected Overrides Sub OnPaint( ByVal pEvent _ As PaintEventArgs) Dim CenterSquare _ As New Rectangle(3, 4, 10, 12) MyBase.OnPaint(pEvent) If Me.Checked Then pEvent.Graphics.FillRectangle( New SolidBrush( CenterSquareColor ), CenterSquare) End If End SubEnd Class (In this article and in others on the website, a ton of line continuations are utilized to keep lines short so they will fit into the space accessible on the site page.) The main thing to see about your new class code is the Inherits watchword. That implies that all the properties and strategies for a VB.NET Framework CheckBox are naturally part of this one. To acknowledge how much work this spares, you must have had a go at programming something like a CheckBox part without any preparation. There are two key things to see in the code above: The first is the code utilizes Override to supplant the norm .NET conduct that would occur for an OnPaint occasion. An OnPaint occasion is activated at whatever point Windows sees that piece of your presentation must be reproduced. A model would be the point at which another window reveals some portion of your presentation. Windows refreshes the showcase naturally, however then calls the OnPaint occasion in your code. (The OnPaint occasion is additionally considered when the structure is at first made.) So in the event that we Override OnPaint, we can change the manner in which things look on the screen. The second is the manner in which Visual Basic makes the CheckBox. At whatever point the parent is Checked (that is, Me.Checked is True) at that point the new code we give in our NewCheckBox class will recolor the focal point of the CheckBox as opposed to drawing a checkmark. The rest is what is called GDI code. This code chooses a square shape precisely the same size as the focal point of a Check Box and hues it in with GDI strategy calls. The enchantment numbers to situate the red square shape, Rectangle(3, 4, 10, 12), were resolved tentatively. I simply transformed it until it looked right. There is one significant advance that you need to ensure you dont keep separate from Override systems: MyBase.OnPaint(pEvent) Abrogate implies that your code will give the entirety of the code to the occasion. However, this is only here and there what you need. So VB gives an approach to run the typical .NET code that would have been executed for an occasion. This is the explanation that does that. It passes exactly the same parameter-pEvent-to the occasion code that would have been executed on the off chance that it hadnt been superseded, MyBase.OnPaint. Utilizing the New Control Since our new control isn't in our tool stash, it must be made in the structure with code. The best spot to do that is in the structure Load occasion strategy. Open the code window for the structure load occasion system and include this code: Private Sub frmCustCtrlEx_Load( ByVal sender As System.Object, ByVal e As System.EventArgs ) Handles MyBase.Load Dim customCheckBox As New newCheckBox() With customCheckBox .Text Custom CheckBox .Left oldCheckBox.Left .Top oldCheckBox.Top oldCheckBox.Height .Size New Size( oldCheckBox.Size.Width 50, oldCheckBox.Size.Height) End With Controls.Add(customCheckBox)End Sub To put the new checkbox on the structure, weve exploited the way that there is as of now one there and simply utilized the size and position of that one (balanced so the Text property will fit). Else we would need to code the position physically. When MyCheckBox has been added to the structure, we at that point add it to the Controls assortment. Be that as it may, this code isnt entirely adaptable. For instance, the shading Red is hardcoded and changing the shading requires changing the program. You may likewise need a realistic rather than a check mark. Heres another, improved CheckBox class. This code tells you the best way to step toward VB.NET object arranged programming. Open Class betterCheckBox Inherits CheckBox Private CenterSquareColor As Color Color.Blue Private CenterSquareImage As Bitmap Private CenterSquare As New Rectangle( 3, 4, 10, 12) Protected Overrides Sub OnPaint _ (ByVal pEvent As _ System.Windows.Forms.PaintEventArgs) MyBase.OnPaint(pEvent) If Me.Checked Then If CenterSquareImage Is Nothing Then pEvent.Graphics.FillRectangle( New SolidBrush( CenterSquareColor), CenterSquare) Else pEvent.Graphics.DrawImage( CenterSquareImage, CenterSquare) End If End If End Sub Public Property FillColor() As Color Get FillColor CenterSquareColor End Get Set(ByVal Value As Color) CenterSquareColor Value End Set End Property Public Property FillImage() As Bitmap Get FillImage CenterSquareImage End Get Set(ByVal Value As Bitmap) CenterSquareImage Value End Set End PropertyEnd Class Why The BetterCheckBox Version Is Better One of the fundamental enhancements is the expansion of two Properties. This is something the old class didnt do by any stretch of the imagination. The two new properties presented are FillColor what's more, FillImage To get a kind of how this functions in VB.NET, attempt this basic test. Add a class to a standard extend and afterward enter the code: Open Property Whatever Get At the point when you press Enter subsequent to composing Get, VB.NET Intellisense fills in the whole Property code square and you should simply code the particulars for your task. (The Get and Set squares arent consistently required beginning with VB.NET 2010, so you need to at any rate disclose to Intellisense this a lot to begin it.) Open Property Whatever Get End Get Set(ByVal esteem) End SetEnd Property These squares have been finished in the code above. The reason for these squares of code is to permit property estimations to be gotten to from different pieces of the framework. With the expansion of Methods, you would be well en route to making a total segment. To see a straightforward case of a Method, include this code beneath the Property assertions in the betterCheckBox class: Open Sub Emphasize() Me.Font New System.Drawing.Font( _ Microsoft Sans Serif, 12.0!, _ System.Drawing.FontStyle.Bold) Me.Size New System.Drawing.Size(200, 35) CenterSquare.Offset( CenterSquare.Left - 3, CenterSquare.Top 3)End Sub Notwithstanding altering the Font showed in a CheckBox, this strategy additionally modifies the size of the container and the area of the checked square shape to represent the new size. To utilize the new technique, simply code it a similar way you would any strategy: MyBetterEmphasizedBox.Emphasize() What's more, much the same as Properties, Visual Studio consequently includes the enhanced me

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.