C #. NET encapsulate custom components (Controls) Dll
It is very easy to encapsulate custom controls with no technical content. Here we will summarize the examples of encapsulating custom digital text boxes:
[1] create a custom Control Library-Windows Forms Control Library
[2] Add a custom Component-Component Class
[3] inherit TextBox and add a KeyPress event. The Code is as follows:
Using System; using System. collections. generic; using System. componentModel; using System. diagnostics; using System. linq; using System. text; using System. windows. forms; namespace WinForms. selfControl {////// Numeric text box -- if the generated Dll cannot be imported in the toolbox, you can drag the Dll directly ///Public partial class TextBoxNumber: TextBox {public TextBoxNumber () {InitializeComponent ();} public TextBoxNumber (IContainer container) {container. add (this); InitializeComponent (); this. keyPress + = TextBoxNumber_KeyPress ;}////// Only numbers can be entered ///Void TextBoxNumber_KeyPress (object sender, KeyPressEventArgs e) {// if the input is neither a numeric key nor a carriage return or Backspace key, cancel the input if (! (Char. IsNumber (e. KeyChar) & e. KeyChar! = (Char) 13 & e. KeyChar! = (Char) 8) {e. Handled = true ;}}}}
[4] Add the generated Dll to the toolbox
[5] test the custom control to verify that only numbers can be entered
[6] Notes
It must be compiled using AnyCPU. If the generated Dll is imported into the toolbox, you can drag the file directly...
Source code:
Http://download.csdn.net/detail/aoshilang2249/8172891