C #
Customizing user Controls
Here is the reprint article, used to record the self-learning process, the original link address http://blog.csdn.net/xiongxuanwen/article/details/2605109
Previous article: Control makingThis example is to make a simple custom control, and then use a simple test program, for beginners, this example is relatively simple, can only play the effect of throwing stone jade. I am also in the study, in the future will be my study gradually written to share with you. Step one: Create a new Control Library project: MyControl The second step: Drag 1 PictureBox, 1 buttons, 6 lable controls to the user interface from the Toolbox, with the following layout: For example, set PictureBox name to Picbox and background to white , the button's name is Btnopen, and the Text property of the left three lable is: file name, file size, file size, right of the three lable name: Lblname, Lbllength, Lblsize, respectively. Step Three: Add handler code to write code in Btnopen's Click event, open an Open File dialog box, select a graphics file, open it and display it on Picbox.
Private voidBtnopen_click (Objectsender, EventArgs e) {OpenFileDialog Ofdpic=NewOpenFileDialog (); Ofdpic.filter="JPG (*. jpg;*. JPEG); GIF file (*. GIF) |*.jpg;*.jpeg;*.gif"; Ofdpic.filterindex=1; Ofdpic.restoredirectory=true; Ofdpic.filename="";if(Ofdpic.showdialog () = =DialogResult.OK) {stringSpicpaht =ofdPic.FileName.ToString (); FileInfo Fipicinfo=NewFileInfo (spicpaht);LongLpiclong = fipicinfo.length/1024x768;stringSpicname =Fipicinfo.name;stringSpicdirectory =fiPicInfo.Directory.ToString ();stringSpicdirectorypath =Fipicinfo.directoryname; Bitmap Bmpic=NewBitmap (spicpaht);if(Lpiclong > -) {MessageBox.Show ("This file size is"+ Lpiclong +"K ; has been over the maximum limit of K-van! ");}Else{point ptloction=NewPoint (bmpic.size);if(Ptloction.x > PicBox.Size.Width | | ptloction.y >picBox.Size.Height) {Picbox.sizemode=Pictureboxsizemode.zoom;}Else{Picbox.sizemode=pictureboxsizemode.centerimage;}} Picbox.loadasync (SPICPAHT); Lblname.text=Spicname;lbllength.text= lpiclong.tostring () +"KB"; Lblsize.text= bmPic.Size.Width.ToString () +"x"+bmPic.Size.Height.ToString ();}}
Fourth step: Test the control press F5 to start debugging, pop up the following form: Click the Open button, the Open File dialog box: Select a picture, click Open, you can see the open picture on the Picbox: Step Fifth: View the genetic control file, to the bin-> of the project file directory Can be found in Debug.
Next: Control testingThe first step: Create a new C # Windows application named Testmybutton. Step Two: Add a custom user control right-click on any of the controls in the toolbox, and the popup context menu is as follows: Click "Select Item" and the following dialog box pops up: Click "Browse" to open the dialog box: Select the control file Mybutton.dll, click the "Open" button to go back to the custom toolbox, The control you just checked is checked by default. Return to the VS editor and see a UserControl in the Toolbox: Step three: Drag a custom control to the Test window Fourth Step test program click the Open button: Select a picture, open, display the diagram: Test succeeded.
C # Custom control Authoring and usage instances (WinForm)