C#自定义控件,在项目工具箱中加入自定义控件,调用自定义控件

  1. 新建一个windows自定义控件的项目
    在这里插入图片描述
  2. 在界面上拖入:一个picturebox1 , 6个label,1个button
    picturebox1命名为picBox,左边三个label的text改为:文件名称、文件大小、文件尺寸;右边的三个label的name改为lblName、lblLength、lblSize
    button的name属性改为btnOpen.
    在这里插入图片描述
    代码如下:
namespace 自定义控件
{
    public partial class UserControl1: UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofdPic = new OpenFileDialog();
            ofdPic.Filter = "JPG(*.JPG;*.JPEG);gif文件(*.GIF)|*.jpg;*.jpeg;*.gif";
            ofdPic.FilterIndex = 1;
            ofdPic.RestoreDirectory = true;
            ofdPic.FileName = "";
            if (ofdPic.ShowDialog() == DialogResult.OK)
            {
                string sPicPaht = ofdPic.FileName.ToString();
                System.IO.FileInfo fiPicInfo = new System.IO.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("此文件大小為" + lPicLong + "K;已超過最大限制的K范圍!");
                }
                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();
            }
          
        }
    }
}

3、鼠标右键点击项目名,重新生成下
在这里插入图片描述
4、新建一个常规winform界面项目,打开自定义控件的文件,找到控件项目想的Debug里面的dll文件,拖进当前项目的工具箱中

在这里插入图片描述
5、在当前的界面中就可以调用这个控件了
在这里插入图片描述
参考链接:https://blog.csdn.net/xiongxuanwen/article/details/2605109

  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值