3

我有一个按钮,我一直将其用作组合框旁边的小选择按钮。当我单击按钮时,我会打开一个更大的完整列表。事情的这一方面运作良好,我对此没有问题..

我的问题在于有人对我说你能把你选择的那个丑陋的图标改成我的漂亮图标吗?

我胡说八道,我在许多表单上都有数百个这样的按钮。所以我想我将创建一个名为 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")));
4

2 回答 2

0

将所有属性设置在一个地方的概念是一个好主意,只是实施得不太正确。我会让这个类继承自 UserControl 而不是 Button。通过将其设置为 UserControl,您可以使用设计器设置所需的所有属性,例如按钮的默认图像。在设计器中设置它,然后只需将您的 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 的图像

在此处输入图像描述

于 2013-10-24T13:54:42.693 回答
0

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

CustomButton类(继承自System.Windows.Forms.Button)中,重写该Refresh()方法,并将按钮的图像设置为您希望看到的图像:

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

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

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

    customButton.Refresh();
  }
}

在 Github 上放了一个演示应用程序

于 2013-10-25T09:28:49.363 回答