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 controls with VB.NET

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail how to develop controls in VB.NET. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

The component support of Microsoft ®VB ®has always been one of its selling points, so third-party software developers have developed a variety of visual controls with new functionality (and a few non-visual controls) for VB programmers to choose. This special form of VB development has created countless third-party controls-some shareware / free software, and some are sold over the counter. Now, people can even develop their own visual / non-visual components directly with VB. As a result, the number of components is growing rapidly, and a considerable number of them are designed by programmers (or development teams) for their own development tasks.

Note that Microsoft ActiveX controls purchased by you or your development team in the past can often be ported directly to Microsoft .NET without modification or rewriting. Specifically, as long as you enter the IDE (integrated development environment) environment of Microsoft Visual Studio ®.NET, choose from the menu: tools Tool-> Custom Toolbox Customize Toolbox), or use the .NET Framework utility Aximp.exe (ActiveX control importer), you will be able to call off-the-shelf ActiveX controls in .NET applications. However, once a control does not work properly in the .NET environment, its author should probably consider upgrading the control. Therefore, in order to use the purchased third-party ActiveX control normally in the .NET environment, you should go to the developer's Web website to see if it has an upgraded version or .NET version.

In the .NET programming world, there is still a need for custom UI components, but their creation processes are different. This article explores two questions: why create your own Microsoft Windows ®control? What is different from the previous version 5. 0 / 6. 0 when developing controls in VB.NET?

Why develop your own controls?

To limit the text type of the Windows forms TextBox control, you can add the control's KeyPress event handler to the form code to intercept each keystroke from the user and check whether the character corresponding to that key can enter the TextBox:

Private Sub TextBox1_KeyPress (ByVal sender As Object, _ ByVal e As System.Windows.Forms.KeyPressEventArgs) _ Handles TextBox1.KeyPress If Not Char.IsDigit (e.KeyChar) Then e.Handled = Tru Else e.Handled = False End If End Sub

Note that simply by capturing keystrokes, there is no guarantee that the text entered into TextBox is all numeric, because sometimes users do not type characters directly into TextBox, but paste characters to TextBox through the clipboard; besides, the initial value of TextBox text may contain illegal characters. Some other events, such as TextChanged, may be able to catch more illegal input, but I prefer to use Validating or Leave events, which do a word compliance check on TextBox after the user leaves the input control. This does give up the immediate response to user input, but allows the user to first enter a text string of "minor foul" through the clipboard, such as pasting "3425 2343 2342 2342" in an input box that forbids spaces. Then manually correct the "foul" character in the input box.

It's not too difficult to manually add event handler code to a control, but do you feel so relaxed when you're faced with more complex programming tasks, such as checking that mailing addresses or car VIN # words conform to the law? At this point, you will want to use the same event handler for multiple forms or even multiple projects, or provide it for sharing with other members of the development team. However, extracting code snippets from a form, along with installation guides and control naming conventions, is the beginning of a nightmare. Fortunately, there is no other way, as long as you release it with a custom control, you will not encounter this nightmare, because the user interface and related code are in separate components, and the release of components is much easier. Code snippets released through components are also easier to upgrade: you just need to release a new version of the component, and you no longer have to publish new code snippets through various channels for programmers to manually overwrite the original code!

How does inheritance change the development of controls?

Developing controls in VB.NET is very different from VB6.0. The fundamental reason is that .NET introduces inheritance. In VB6.0, you can only implement various functions without using controls or directly referencing off-the-shelf controls. For example, to create the custom text input box mentioned earlier, you need to create a new ActiveX control and add a TextBox to it.

Note that this programming idea is often referred to as "containment" or "delegation". In VB6.0, non-control classes used to simulate inheritance mechanisms can also use this idea.

At this point, the newly created ActiveX control will not automatically get some properties of TextBox (such as the Text property) as you would like; these properties can only be implemented by your coding. To make matters worse, you have to use a lot of code to make sure that TextBox always occupies the entire form; you also have to design resizing event handlers for new controls. Of course, after a lot of trouble, you will always complete the design task of the control, not to mention the ActiveX control interface wizard can lighten your burden. However, in the .NET environment, the whole task will be done in a completely different way.

VB6.0 provides an add-on program (add-in) that automatically adds and maps control properties to simplify control development.

Inheriting performance avoids some repetitive code in control development because it gives .NET controls the functionality of any other control directly. For example, in order to create your own TextBox control, you can inherit the existing TextBox control instead of the UserControl control. The new control inherits all the functionality of the base class control, so you only need to encode the functionality that is not in the base class control. Here is a practical example. The following code creates a custom TextBox control that allows users to enter only numeric characters:

Note that in order to run this code, you only need to create a new VB .NET project under the "Windows Application" template, and then you can experiment with the new control in the blank form automatically generated by IDE. Create a new class NumericTextBox in the project, replace the contents of the NumericTextBox class file with the following code, and compile the project. *, select tools-> Custom Toolbox from the menu, select the .exe file from the previously compiled project, and you can add the new control to the toolbox.

Public Class NumericTextBox Inherits System.Windows.Forms.TextBox Protected Overrides Sub OnKeyPress (ByVal e As System.Windows.Forms.KeyPressEventArgs) If Not Char.IsDigit (e.KeyChar) Then e.Handled = True Els e.Handled = False End If End Sub End Class

For this example, the above code is sufficient. If you still think it's not perfect, use the following code instead, which uses a wonderful Boolean logic to reduce the number of lines of code:

Public Class NumericTextBox Inherits System.Windows.Forms.TextBox Protected Overrides Sub OnKeyPress (ByVal e As _ System.Windows.Forms.KeyPressEventArgs) e.Handled = Not Char.IsDigit (e.KeyChar) End Sub End Class

Now your new control is displayed correctly in the form. It handles events like TextBox and has the same methods and properties as TextBox. You don't even need more coding to achieve data binding to the new control, because this is also one of the functionality of the base class control TextBox.

Note that this control is very demanding on user input: it only allows you to enter numbers from 0 to 9, that is, commas, decimal points, and even negative signs in numbers are illegal characters. I will introduce a more powerful input validator in the next article.

When VB.NET develops a control, the core code will be as long as this example, but so will the code to handle the control's resizing event and implement the TextBox component properties. Thus it can be seen that the inheritance provided by. NET can greatly simplify the source code. With this alone, .NET is already impressive, not to mention that it has many other advantages. What's even more amazing is that wherever a control is required, a new control that inherits it can be used instead. For example, your NumericTextBox control can be used wherever TextBox is required in a routine. Not only that, new controls that inherit from existing controls, rather than from the UserControl class, not only have all the functionality of base class controls, but also use inherited properties, methods, and events like base class controls. Therefore, any programmer who has learned standard TextBox controls will know how to use NumericTextBox controls. Allowing inheritance of existing classes / controls is a major leap from VB6.0 to .NET, but the advantages of .NET are more than that! As long as you carefully study the examples provided in this series of articles, you will find that not only do Windows forms controls have many powerful features in .NET, but they are much easier to create than in older versions of VB.

This is the end of this article on "how to develop controls in VB.NET". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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: 274

*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