gpt4 book ai didi

c# - 窗体控件

转载 作者:太空宇宙 更新时间:2023-11-03 16:09:56 25 4
gpt4 key购买 nike

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

我的问题在于当有人对我说你能把你选择的那个丑陋的图标换成我漂亮的图标时。

我搞砸了,我在许多表单上都有数百个这样的按钮。所以我想我将创建一个名为 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,您可以使用设计器设置所需的所有属性,例如按钮的默认图像。在设计器中设置它,然后只需将您的 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 在设计器中的图像

enter image description here

关于c# - 窗体控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17877046/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com