如何让自定义控件属性值更改后,值立即显示到界面上 Invalidate()

在自定义控件(复合控件)后,有些属性想立即显示到界面上,而不是编译之后才能,显示到界面上,这个时候,应该在控件属性的set操作里面添加相应的函数处理。

 

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

namespace MyControl
{
    public partial class ColorForGridControl : UserControl
    {
        public ColorForGridControl()
        {
            InitializeComponent();
            DisplayText = "已成交";
        }

        private string displayText;

        public string DisplayText
        {
            get { return displayText; }
            set
            {
                displayText = value;
                this.lblDisplayText.Text = DisplayText;
                this.Invalidate();
            }
        }
        private Color color;

        public Color Color
        {
            get { return color; }
            set
            {
                color = value;
                this.pnlColor.BackColor = Color;
                this.lblDisplayText.ForeColor = Color;
                this.Invalidate();
            }
        }
        private void ColorForGridControl_Load(object sender, EventArgs e)
        {
            if (Color == Color.Empty)
                Color = Color.Green;
            if (DisplayText == string.Empty)
                DisplayText = "已成交";
            this.pnlColor.BackColor = Color;
            this.lblDisplayText.ForeColor = Color;
            this.lblDisplayText.Text = DisplayText;
        }
    }
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在 C# 中,可以使用 PictureBox 控件或者自定义控件显示图片,以下是两种方法的示例: 1. 使用 PictureBox 控件 PictureBox 控件是 Windows 窗体应用程序中用于显示图片的常用控件。你可以通过以下步骤在窗体中添加一个 PictureBox 控件: - 打开 Visual Studio,创建一个新的 Windows 窗体应用程序项目。 - 在“工具箱”中找到 PictureBox 控件,拖动它到窗体上。 - 右键单击 PictureBox 控件,在“属性”窗口中设置其 Image 属性为要显示的图片。 下面是一个简单的示例代码: ```csharp using System; using System.Windows.Forms; namespace PictureBoxExample { public partial class Form1 : Form { public Form1() { InitializeComponent(); // 设置 PictureBox 的 Image 属性为要显示的图片 pictureBox1.Image = Image.FromFile("path/to/image.jpg"); } } } ``` 2. 使用自定义控件 如果你需要更多的图片显示特性,可以考虑使用自定义控件。以下是一个简单的示例代码: ```csharp using System; using System.Drawing; using System.Windows.Forms; namespace CustomControlExample { public class ImageControl : Control { private Image image; public Image Image { get { return image; } set { image = value; Invalidate(); } } protected override void OnPaint(PaintEventArgs e) { // 在控件上绘制图片 if (image != null) { e.Graphics.DrawImage(image, 0, 0); } } } public partial class Form1 : Form { public Form1() { InitializeComponent(); // 创建自定义控件并设置其 Image 属性为要显示的图片 var imageControl = new ImageControl(); imageControl.Image = Image.FromFile("path/to/image.jpg"); // 将自定义控件添加到窗体中 Controls.Add(imageControl); } } } ``` 在上面的代码中,我们创建了一个名为 ImageControl 的自定义控件,并在其中添加了一个 Image 属性来设置要显示的图片。在 OnPaint 方法中,我们通过 Graphics.DrawImage 方法来绘制图片。 在窗体的构造函数中,我们创建了一个 ImageControl 实例并设置其 Image 属性为要显示的图片,然后将该控件添加到窗体中。 注意,自定义控件的绘制逻辑可能比 PictureBox 控件复杂,需要处理一些更多的特性,比如图片的缩放、移动等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值