C# custom control creation

If you need to view more articles, please search the public account csharp programming Daquan on WeChat. If you need to join the C# communication group, please add WeChat z438679770, and make a note to enter the group. I invite you to join the group! ! !

---------------------------------------------------------------------------------------------------------------------------------------------------------

In the programming process, the existing c# controls are far from meeting our needs, so we need to develop the controls ourselves. I took some detours in the development of custom controls, and wrote this article, hoping to give some help to friends in need, and also to deepen my impression.

1. Create custom controls

 

 

2. Add controls and combine them into a new control

Custom control function: open a picture, display the picture in the pictureBox control, and display the name, size and size of the picture

The controls are as follows:

pictureBox1: named picBox

label1~label6 : the left three display text, the right three are named: lblName lblLength lblSize

button1: named btnOpen

 

  code show as below:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsControlLibrary1
{
    public partial class UserControl1: UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofdPic = new OpenFileDialog();
            ofdPic.Filter = "JPG(*.JPG;*.JPEG);gif document(*.GIF);PNG(*.PNG)|*.jpg;*.jpeg;*.gif;*.png";
            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 bmPic = new Bitmap(sPicPaht);
                if (lPicLong > 400)
                {
                    MessageBox.Show("This file size is" + lPicLong + "K;has exceeded the maximum limit K scope!");
                }
                else
                {
                    Point ptLoction = new Point(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() + "×" + bmPic.Size.Height.ToString();
            }

        }
    }
}

  

Click [Solution], right-click the pop-up window, and click [Generate Solution]

At this point, the creation of the custom control has been completed!

 

The generated control path is in the Debug folder, the dll file

 

3. Custom control test

 

Create a new windows forms application

 

I found that there is no custom control just now in the control toolbar on the left, don't worry! !

 

Select [Select Toolbox Items] under Tools

 

 

Browse, select the dll file path, note that the path cannot contain Chinese characters, remember! Otherwise it will go wrong!

 

After the addition is successful, you will find that the control just defined appears in the toolbox.

 

  Test Results:

 

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

If you need to view more articles, please search the public account csharp programming Daquan on WeChat. If you need to join the C# communication group, please add WeChat z438679770, and make a note to enter the group. I invite you to join the group! ! !

Tags: C#

Posted by darksniperx on Thu, 12 May 2022 20:38:00 +0300