Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to develop Custom controls by VB

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces VB how to develop custom controls, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

Our custom class is generated by inheriting the UserControl class, and since UserControl is also generated by inheriting the Control class, our custom class will inherit all useful methods, properties, and events of the Control class. For example, since it is generated by inheriting the Control class, our custom class will automatically have event handlers.

A particularly important issue when developing custom controls in VB is how to display the user interface of the custom controls. No matter how you organize custom controls, it is important to note that custom controls are sometimes redisplayed. Therefore, when the custom control is redrawn, the user interface must be redrawn. Considering that the OnPaint method of the Control class is called every time the control is redrawn, overriding this method with the new OnPaint method of drawing the user interface of the custom control can ensure that the custom control maintains a certain appearance.

The code in Table 1 is a control named RoundButton. In figure 1, there is a RoundButton custom control on the form, and Table 2 is its code. Basically what we need to do is to override the OnPaint method. The system passes a PaintEventArgs object to this method, from which we can get the System.Drawing.Graphics object of the control, and then use its method to draw the user interface of the custom control.

Table 1:RoundButton control

Imports System.Windows.Forms Imports System.Drawing Public Class RoundButton: Inherits UserControl Public BackgroundColor As ColorColor = Color.Blue Protected Overrides Sub OnPaint (ByVal e As PaintEventArgs) Dim graphics As Graphics = e.Graphics Dim penWidth As Integer = 4 Dim pen As Pen = New Pen (Color.Black, 4) Dim fontHeight As Integer = 10 Dim font As Font = New Font ("Arial", fontHeight) Dim brush As SolidBrush = New SolidBrush (BackgroundColor) graphics.FillEllipse (brush, 0,0, Width, Height) Dim textBrush As SolidBrush = New SolidBrush (Color.Black) graphics.DrawEllipse (pen CInt (penWidth / 2), _ CInt (penWidth / 2), Width-penWidth, Height-penWidth) graphics.DrawString (Text, font, textBrush, penWidth, _ Height / 2-fontHeight) End Sub End Class

The code in Table 1 is too simple to believe. Our custom class has only one method: OnPaint. Simply put, this method passes a PaintEventArgs object from which we can get the System.Drawing.Graphics object. This Graphics object represents the drawing area of our custom control, and no matter what is drawn on the Graphics object, it is displayed as the interface of the custom user control.

Invocation of table 2:RoundButton control

Public Class MyForm Inherits System.Windows.Forms.Form # Region "Windows Form Designer generated code" Private WithEvents roundButton As RoundButton Public Sub New () MyBase.New () 'this call is the InitializeComponent () required by Windows Form Designer' after the InitializeComponent () call, you can add any instantiation code End Sub 'form override Organize the list of components Protected Overloads Overrides Sub Dispose (ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose () End If End If MyBase.Dispose (disposing) End Sub 'Windows Form Designer required Private components As System.ComponentModel.IContainer' Note: the following procedure is required by Windows Form Designer,'it can be modified using Windows Form Designer 'but do not use a software editor to modify Private Sub InitializeComponent ()''MyForm' Me.AutoScaleBaseSize = New System.Drawing.Size (5,13) Me.ClientSize = New System.Drawing.Size (292,273) Me.Name = "MyForm" Me.Text = "Using Custom Control" roundButton = New RoundButton () AddHandler roundButton.Click, AddressOf roundButton_Click roundButton.Text = "Click Here!" RoundButton.BackgroundColor = System.Drawing.Color.White roundButton.Size = New System.Drawing.Size (80,80) roundButton.Location = New System.Drawing.Point (100,30) Me.Controls.Add (roundButton) End Sub # End Region Private Sub roundButton_Click (ByVal source As Object, ByVal e As EventArgs) MessageBox.Show ("Thank you.") End Sub Public Shared Sub Main () Dim form As MyForm = New MyForm () Application.Run (form) End Sub End Class Thank you for reading this article carefully. I hope the article "how to develop custom controls in VB" shared by the editor will be helpful to you. At the same time, I hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report