繁体   English   中英

Winforms控件

[英]Winforms Control

我有一个按钮,一直以来都用作组合框旁边的一个小选择按钮。 单击按钮时,我会打开一个较大的完整列表。 这方面的工作很好,我对此没有问题。

我的问题在于,当有人对我说可以将您选择的丑陋图标更改为我的漂亮图标时。

我胡扯,我在许多表单上都有数百个这样的按钮。 所以我想我将创建一个名为PickButton的自定义控件(这是一个标准按钮和一组默认属性),并将它们放置在窗体上的任何地方。 在PickButton自定义控件的代码中,我将一些属性和图像设置为客户漂亮的图标。

因此,我从工具箱中将PickButton拖放到窗体上,到目前为止,一切看起来都还不错,并且我感觉有点聪明。 现在我想我自己将回到我的漂亮图标,而不是客户选择的笨拙图标,并更改PickButton自定义控件中的代码。 但是我不能摆脱那个客户图标,因为运行PickButton时的代码发生在具有客户图标的设计器文件中的代码之前。

因此,我的目标是拥有一个PickButton控件,并能够在一处更改图标和其他属性,并且在创建控件的实例并将其显示在表单上时将设置所有属性。

我不是很聪明,以错误的方式去完成任务吗???

这是我的PickButton自定义控件类

public class PickButton : Button
{

    public PickButton()
    {
        InitialiseButton();
    }

    internal void InitialiseButton()
    {
        this.ImageAlign = ContentAlignment.MiddleCenter;
        this.Image = WindowsFormsApplication1.Properties.Resources.Cancel.ToBitmap();
        this.Size = new Size( 28, 28 );
        this.Dock = DockStyle.Fill;
        this.Margin = new Padding( 0, 2, 2, 0 );
        this.Text = string.Empty;
    }
}

现在我将其放到表单上,设计器中的代码如下

 // 
        // pickButton1
        // 
        this.pickButton1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.pickButton1.Image = ((System.Drawing.Image)(resources.GetObject("pickButton1.Image")));
        this.pickButton1.Location = new System.Drawing.Point(0, 0);
        this.pickButton1.Margin = new System.Windows.Forms.Padding(0, 2, 2, 0);
        this.pickButton1.Name = "pickButton1";
        this.pickButton1.Size = new System.Drawing.Size(284, 262);
        this.pickButton1.TabIndex = 0;
        this.pickButton1.Text = "pickButton1";
        this.pickButton1.UseVisualStyleBackColor = true;

现在我想更改图像,所以我将PickButton代码更改为使用其他图标

this.Image = WindowsFormsApplication1.Properties.Resources.Browse.ToBitmap();

运行该应用程序,并且由于设计器文件中的这一行代码,第一个图标仍在显示中

        this.pickButton1.Image = ((System.Drawing.Image)(resources.GetObject("pickButton1.Image")));

将所有属性设置在一个地方的想法是一个好主意,只是实施不正确。 我会让此类从UserControl继承,而不是从Button继承。 通过将其设置为UserControl,可以使用设计器设置所需的所有属性,例如按钮的默认Image。 在设计器中进行设置,然后将UserControl从工具箱拖放到窗体上。 如果只将“ PickButton”控件与组合框一起使用,我也将组合框放在UserControl上。 如果您将来想更改按钮图像(或与此相关的任何其他属性),则可以在ctlPickButton中进行更改,这将对整个项目中使用的所有实例进行更改。

ctlPickButton:

public partial class ctlPickButton : UserControl
{
    public event EventHandler pickButtonClicked;

    public ctlPickButton()
    {
        InitializeComponent();
    }

    //Allows buttons image to be set in code if necessary
    public Image Image
    {
        get
        {
            return button1.Image;
        }
        set
        {
            if (Image != null)
            {
                button1.Image = value;
            }
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (pickButtonClicked != null)
        {
            pickButtonClicked(sender, e);
        }
    }
}

演示表格:

    public Form1()
    {
        InitializeComponent();

        ctlPickButton1.pickButtonClicked += new EventHandler(ctlPickButton1_pickButtonClicked);
        ctlPickButton2.pickButtonClicked += new EventHandler(ctlPickButton2_pickButtonClicked);
    }

    void ctlPickButton2_pickButtonClicked(object sender, EventArgs e)
    {
        if (comboBox2.SelectedItem != null)
        {
            MessageBox.Show(comboBox2.SelectedItem.ToString());
        }
    }

    void ctlPickButton1_pickButtonClicked(object sender, EventArgs e)
    {
        if (comboBox1.SelectedItem != null)
        {
            MessageBox.Show(comboBox1.SelectedItem.ToString());
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        comboBox1.Items.Add("French");
        comboBox1.Items.Add("Spanish");
        comboBox1.Items.Add("English");
        comboBox1.Items.Add("German");

        comboBox2.Items.Add("Pizza");
        comboBox2.Items.Add("Hamburger");
        comboBox2.Items.Add("Potato");
        comboBox2.Items.Add("Chicken");

        //Shows how the default image set in the designer can be overwritten for a 
        //specific instance using the "Image" property
        ctlPickButton2.Image = Testbed.Properties.Resources.searchIcon2;
    }
}

ctlPickButton在设计器中的图像

在此处输入图片说明

我想我找到了一个简单,干净的解决方案:

CustomButton类(继承自System.Windows.Forms.Button )中,重写Refresh()方法,然后将按钮的图像设置为您要查看的图像:

public class CustomButton : Button
{
  public override void Refresh()
  {
    Image = MyResources.HappyFace;
  }
}

在保存CustomButton实例的形式中,只需在InitializeComponent()之后构造函数中调用customButton.Refresh() InitializeComponent()

public partial class MainForm : Form
{
  public MainForm()
  {
    InitializeComponent();

    customButton.Refresh();
  }
}

我已经在Github上放置了一个演示应用程序

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM