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 segments can be an exceptionally propelled venture. Be that as it may, you can fabricate a VB.NET class that has huge numbers of the upsides of a tool stash part with considerably less exertion. Heres how! To get a kind of what you have to do to make a total custom segment, attempt this investigation: - 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 task (so you dont need to). As a verifiable reference, The VB6 compiler did a ton of very similar things, however you never could get to the code since it was covered in accumulated p-code. You could create custom controls in VB6 as well, however it was much increasingly troublesome and required an exceptional utility that Microsoft provided only for that reason. In the Form Designer.vb record, you will find that the code beneath has been included consequently in the correct areas to help the CheckBox segment. (On the off chance that you have an alternate adaptation of Visual Studio, your code may be marginally unique.) This is the code that Visual Studio composes for you. Required by the Windows Form Designer Private parts _ As System.ComponentModel.IContainerNOTE: The accompanying methodology is requiredby the Windows Form DesignerIt can be altered utilizing the Windows Form Designer.Do not change 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 strategies and properties of the genuine CheckBox control are in a class provided by the .NET Framework: System.Windows.Forms.CheckBox. This isnt part of your venture since its introduced in Windows for every single .NET program. Be that as it may, 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 undertaking 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 upbeat 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 undertaking. There are a few different ways to do this including right-tapping the undertaking in Solution Explorer and choosing Add at that point Class or choosing Add Class under the Project menu thing. Change the record 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 webpage, a great deal of line continuations are utilized to keep lines short so they will fit into the space accessible on the website page.) The primary thing to see about your new class code is the Inherits catchphrase. That implies that all the properties and techniques 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 segment 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 remade. A model would be the point at which another window reveals some portion of your showcase. 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 on the off chance 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 methodology: MyBase.OnPaint(pEvent) Supersede implies that your code will give the entirety of the code to the occasion. Be that as it may, this is only sometimes 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 abrogated, MyBase.OnPaint. Utilizing the New Control Since our new control isn't in our tool kit, it must be made in the structure with code. The best spot to do that is in the structure Load occasion system. Open the code window for the structure load occasion methodology 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 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. In any case, this code isnt truly 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 principle upgrades is the expansion of two Properties. This is something the old class didnt do by any means. The two new properties presented are FillColor also, FillImage To get a kind of how this functions in VB.NET, attempt this straightforward trial. Add a class to a standard extend and afterward enter the code: Open Property Whatever Get At the point when you press Enter in the wake of composing Get, VB.NET Intellisense fills in the whole Property code square and you should simply code the particulars for your undertaking. (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 headed to making a total segment. To see an extremely straightforward case of a Method, include this code beneath the Property affirmations 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 changing the Font showed in a CheckBox, this technique likewise alters the size of the case and the area of the checked square shape to represent the new size. To utilize the new strategy, simply code it a similar way you would any technique: MyBetterEmphasizedBox.Emphasize() Also, much the same as Properties, Visual Studio naturally includes the enhanced me

No comments:

Post a Comment

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