简体   繁体   English

如何创建更多具有相同外观的按钮(C#)

[英]How can I create more buttons of the same appearance (C#)

Please, can you help me how can I create more buttons of the same appearance. 拜托,您能帮我怎样创建更多外观相同的按钮。 I have graphics structure for buttons in a separate class. 我在单独的类中具有按钮的图形结构。 A function for creating buttons I have in windows form. 用于在Windows窗体中创建按钮的功能。

Step 1) Class for graphic button structure: 步骤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 forms function for control buttons. 赢窗体功能的控制按钮。

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);
    }
}

The current problem is that the creation of the buttons is in the class "GraphicStructure" but in class it creates only one button. 当前的问题是按钮的创建在“ GraphicStructure”类中,但是在类中它仅创建一个按钮。 Although I can embed into windows form to the function, but I'd like to have graphic buttons and functions separately. 虽然我可以将窗口形式嵌入到该功能中,但是我想分别具有图形按钮和功能。 Can you help me please?. 你能帮我吗?。

Control: 控制:

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;
        }
    }
}

Or second idea. 还是第二个想法。 I have created a button that does nothing, but has a graphical Structure. 我创建了一个不执行任何操作但具有图形结构的按钮。 Then create three additional buttons that are already doing something, but again have no graphics and just need to get those buttons to do something cloned from graphic button that does nothing. 然后创建三个已经在做某事但又没有图形的按钮,只需要让这些按钮执行从图形按钮克隆的操作即可。

I try use this but i still saw only one button: 我尝试使用它,但是我仍然只看到一个按钮:

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);
    }
}

Second idea work (i saw three buttons) if i commented this: 第二个想法的工作(我看到了三个按钮),如果我对此进行了评论:

button = classStructure.resetTree;

But if i commented this part code so all three button is again without graphic structure. 但是,如果我注释了此零件代码,则所有三个按钮再次都没有图形结构。

If you need buttons with same appearance, then create your custom button inherited from Button and use it instead of standard Button : 如果您需要外观相同的按钮,请创建继承自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;
    }
}

If you want buttons to behave differently, then assign different click event handlers to different buttons. 如果希望按钮的行为不同,则将不同的click事件处理程序分配给不同的按钮。

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

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