拜托,您能帮我吗?如何创建更多相同外观的按钮。我在一个单独的类中有按钮的图形结构。用于创建我在Windows窗体中的按钮的功能。
步骤1) 图形按钮结构类:
class GraphicClassStructure : GraphicPosition
{
public Button resetTree = new Button();
public void CreateClassButtons()
{
// Reset tree
resetTree.BackColor = Color.Transparent;
resetTree.BackgroundImage = BuildResource.reset;
resetTree.BackgroundImageLayout = ImageLayout.Stretch;
resetTree.FlatAppearance.BorderSize = 0;
resetTree.FlatAppearance.MouseDownBackColor = Color.Transparent;
resetTree.FlatAppearance.MouseOverBackColor = Color.Transparent;
resetTree.FlatStyle = FlatStyle.Flat;
resetTree.Name = "resetTree";
resetTree.Size = Size[0][5];
resetTree.UseVisualStyleBackColor = false;
}
}
控制按钮的Win表单功能。
private void classMenu_Click(object sender, EventArgs e)
{
classStructure.ClassAllButtonsVisible();
classStructure.CreateClassButtons();
this.Controls.Add(classStructure.classBackround);
for (int i = 0; i < 3; i++)
{
switch (i)
{
case 0:
classStructure.resetTree.Location = classStructure.Location[0][2];
break;
case 1:
classStructure.resetTree.Location = classStructure.Location[0][3];
break;
case 2:
classStructure.resetTree.Location = classStructure.Location[0][4];
break;
}
classStructure.resetTree.Click += new EventHandler(resetTreeOneEvent_Click);
classStructure.resetTree.MouseEnter += new EventHandler(resetTree_MouseEnter);
classStructure.resetTree.MouseLeave += new EventHandler(resetTree_MouseLeave);
classStructure.resetTree.Tag = i;
this.Controls.Add(classStructure.resetTree);
}
}
当前的问题是按钮的创建在“GraphicStructure”类中,但在类中它只创建一个按钮。虽然我可以将windows形式嵌入到该功能中,但我想分别使用图形按钮和功能。你能帮我吗?。
控制:
private void resetTreeOneEvent_Click(object sender, EventArgs e)
{
Button button = sender as Button;
if (button != null)
{
switch ((int)button.Tag)
{
case 0:
// First Button Clicked
break;
case 1:
// Second Button Clicked
break;
case 2:
// Third Button Clicked
break;
}
}
}
或者第二个想法。我创建了一个什么都不做但没有图形结构的按钮。然后创建三个已经在做某事的额外按钮,但又没有图形,只需要让这些按钮做一些克隆的图形按钮,什么都不做。
我尝试使用此功能,但我仍然只看到一个按钮:
private void classMenu_Click(object sender, EventArgs e)
{
classStructure.ClassAllButtonsVisible();
classStructure.CreateClassButtons();
this.Controls.Add(classStructure.classBackround);
for (int i = 0; i < 3; i++)
{
Button button = new Button();
button = classStructure.resetTree;
switch (i)
{
case 0:
button.Location = classStructure.Location[0][2];
break;
case 1:
button.Location = classStructure.Location[0][3];
break;
case 2:
button.Location = classStructure.Location[0][4];
break;
}
button.Click += new EventHandler(resetTreeOneEvent_Click);
button.MouseEnter += new EventHandler(resetTree_MouseEnter);
button.MouseLeave += new EventHandler(resetTree_MouseLeave);
button.Tag = i;
this.Controls.Add(button);
}
}
第二个想法工作(我看到三个按钮),如果我评论这个:
button = classStructure.resetTree;
但如果我评论这部分代码,那么所有三个按钮都没有图形结构。
答案 0 :(得分:0)
如果您需要具有相同外观的按钮,请创建继承自Button
的自定义按钮,并使用它而不是标准Button
:
public class TreeButton : Button
{
public TreeButton()
{
BackColor = Color.Transparent;
BackgroundImage = /* set some image */
BackgroundImageLayout = ImageLayout.Stretch;
FlatAppearance.BorderSize = 0;
FlatAppearance.MouseDownBackColor = Color.Transparent;
FlatAppearance.MouseOverBackColor = Color.Transparent;
FlatStyle = FlatStyle.Flat;
Size = /* set size */
UseVisualStyleBackColor = false;
}
}
如果您希望按钮的行为不同,请将不同的点击事件处理程序分配给不同的按钮。