How to create and use a custom control in C #

Source: Internet
Author: User

Article 1: control creation
This example is to create a simple custom control and then use a simple test program. For Beginners, this example is relatively simple and can only play the effect of throwing stones.
I am also studying. In the future, I will gradually write what I have learned and share it with you.
Step 1: Create a control library project: myControl

Step 2: drag one PictureBox, one Button, and six Lable controls from the toolbox to the user interface. The layout is as follows:

For example, set the Name of pictureBox to picBox, the background to white, the Name of the Button to btnOpen, and the Text attributes of the three Lable on the left to file Name, file size, and file size, the names of the three Lable on the right are: lblName, lblLength, and lblSize.
Step 3: add the handler code
Write code to the Click event of btnOpen. In the open file dialog box, select a graphic file and open it and display it on picBox.Copy codeThe Code is as follows: private void btnOpen_Click (object sender, EventArgs e)
{
OpenFileDialog ofdPic = new OpenFileDialog ();
OfdPic. Filter = "JPG (*. JPG; *. JPEG); gif file (*. GIF) | *. jpg; *. jpeg; *. gif ";
OfdPic. FilterIndex = 1;
OfdPic. RestoreDirectory = true;
OfdPic. FileName = "";
If (ofdPic. ShowDialog () = DialogResult. OK)
{
String sPicPaht = ofdPic. FileName. ToString ();
FileInfo fiPicInfo = new FileInfo (sPicPaht );
Long lPicLong = fiPicInfo. Length/1024;
String sPicName = fiPicInfo. Name;
String sPicDirectory = fiPicInfo. Directory. ToString ();
String sPicDirectoryPath = fiPicInfo. DirectoryName;
Bitmap BMP Ic = new Bitmap (sPicPaht );
If (lPicLong> 400)
{
MessageBox. Show ("the file size is" + lPicLong + "K; K has exceeded the maximum limit! ");
}
Else
{
Point ptLoction = new Point (bmp ic. 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 = bmp ic. Size. Width. ToString () + "×" + bmp ic. Size. Height. ToString ();
}
}

Step 4: test the control
Press F5 to start debugging. the following form is displayed:

Click the OPEN button. The open file dialog box is displayed. Select an image and click open. The opened image is displayed in picBox. Step 5: view the generated control file, go to bin-> debug under the project file directory. Part 2: Control TestStep 1: Create a new C # Windows application named TestMyButton. step 2: Add a Custom User Control, right-click any control in the toolbox, and right-click the control as follows: click "select item". The following dialog box is displayed: click "Browse ", in the displayed dialog box, select the control file mybutton. dll, click the "open" button, and return to the custom toolbox. By default, the system will check the control you just selected. Return to the vs editor. You can see that there is an additional UserControl in the toolbox. Step 3: drag a custom control to the test window. Step 4: click "open" to select an image, open to show the figure: the test is successful.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.