如何在C#中创建按钮

时间:2014-01-25 08:56:24

标签: c# class button inheritance

拜托,你能帮我创建按钮吗?我有问题,当我运行应用程序,所以我没有看到我的代码中的任何按钮。在我的代码中的某处是bug。

步骤1)(按钮属性类)

class GraphicClassStructure : GraphicPosition
{
    public Button menu = new Button();
    public Button classBackround = new Button();
    public Button resetTree = new Button();

    public void CreateClassButtons()
    {
        switch (UniqueValue.character)
        {
            case "sorcerer":
                this.menu.Name = "sorcererText";
                this.menu.BackgroundImage = BuildResource.sorcerer;
                this.classBackround.BackgroundImage = BuildResource.sorcerer_skill_tree;
                break;
            case "dragonknight":
                this.menu.Name = "dragonKnightText";
                this.menu.BackgroundImage = BuildResource.dragonknight;
                this.classBackround.BackgroundImage = BuildResource.dragonknight_skill_tree;
                break;
            case "templar":
                this.menu.Name = "templarText";
                this.menu.BackgroundImage = BuildResource.templar;
                this.classBackround.BackgroundImage = BuildResource.templar_skill_tree;
                break;
            case "nightblade":
                this.menu.Name = "nightbladeText";
                this.menu.BackgroundImage = BuildResource.nightblade;
                this.classBackround.BackgroundImage = BuildResource.nightblade_skill_tree;
                break;
        }

        // Menu
        this.menu.BackColor = System.Drawing.Color.Transparent;
        this.menu.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
        this.menu.FlatAppearance.BorderSize = 0;
        this.menu.FlatAppearance.MouseDownBackColor = System.Drawing.Color.Transparent;
        this.menu.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Transparent;
        this.menu.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
        this.menu.Location = new System.Drawing.Point(Location[0][0][0], Location[0][1][0]);
        this.menu.Size = new System.Drawing.Size(Size[0][0][0], Size[0][1][0]);
        this.menu.TabIndex = 3;
        this.menu.UseVisualStyleBackColor = false;

        // Class Backround
        this.classBackround.BackColor = System.Drawing.Color.Transparent;
        this.classBackround.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
        this.classBackround.FlatAppearance.BorderSize = 0;
        this.classBackround.FlatAppearance.MouseDownBackColor = System.Drawing.Color.Transparent;
        this.classBackround.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Transparent;
        this.classBackround.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
        this.classBackround.Location = new System.Drawing.Point(Location[0][0][1], Location[0][1][1]);
        this.classBackround.Name = "classBackround";
        this.classBackround.Size = new System.Drawing.Size(Size[0][0][1], Size[0][1][1]);
        this.classBackround.TabIndex = 17;
        this.classBackround.UseVisualStyleBackColor = false;

        // Reset tree
        this.resetTree.BackColor = System.Drawing.Color.Transparent;
        this.resetTree.BackgroundImage = BuildResource.reset;
        this.resetTree.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
        this.resetTree.FlatAppearance.BorderSize = 0;
        this.resetTree.FlatAppearance.MouseDownBackColor = System.Drawing.Color.Transparent;
        this.resetTree.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Transparent;
        this.resetTree.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
        this.resetTree.Name = "resetTree";
        this.resetTree.Size = new System.Drawing.Size(Size[0][0][2], Size[0][1][2]);
        //this.resetTree.TabIndex = 18;
        this.resetTree.UseVisualStyleBackColor = false;
    }
}

步骤2)(按钮位置和尺寸的类别)

class GraphicPosition
{
    // new int[type][x,y][value]
    // Location
    public int[][][] Location;

    // Size
    public int[][][] Size;

    public GraphicPosition()
    {
        Location = new int[][][]
        {
            new int[][]
            {
                // Menu
                new int[89],
                // Class
                new int[325],
                // Reset Tree
                new int[410],
                new int[615],
                new int[823]
            },
            new int[][]
            {
                // Menu
                new int[223],
                // Class
                new int[200],
                // Reset Tree
                new int[169],
                new int[169],
                new int[169]
            }
        };
        Size = new int[][][]
        {
            new int[][]
            {
                // Menu
                new int[133],
                // Class
                new int[619],
                // Reset Tree
                new int[28]
            },
            new int[][]
            {
                // Menu
                new int[26],
                // Class
                new int[484],
                // Reset Tree
                new int[25]
            }
        };
    }
}

步骤3)(表单初始化组件中的脚本)

    Collection collection = new Collection();
    GraphicClassStructure classStructure = new GraphicClassStructure();

    public buildEditor()
    {
        InitializeComponent();
        this.Cursor = NativeMethods.LoadCustomCursor(Path.Combine(collection.source, collection.cursor));
        maxSkillPoint.Text = collection.maxSkill.ToString();
        classStructure.CreateClassButtons();
        this.Controls.Add(classStructure.classBackround);

        for (int i = 0; i < 3; i++)
        {
            switch (i)
            {
                case 0:
                    classStructure.resetTree.Location = new System.Drawing.Point(classStructure.Location[0][0][2], classStructure.Location[0][1][2]);
                    break;
                case 1:
                    classStructure.resetTree.Location = new System.Drawing.Point(classStructure.Location[0][0][3], classStructure.Location[0][1][3]);
                    break;
                case 2:
                    classStructure.resetTree.Location = new System.Drawing.Point(classStructure.Location[0][0][4], classStructure.Location[0][1][4]);
                    break;
            }
            classStructure.resetTree.Click += new EventHandler(resetTreekOneEvent_Click);
            classStructure.resetTree.Tag = i;
            this.Controls.Add(classStructure.resetTree);
        }

        switch (UniqueValue.character)
        {
            case "sorcerer":
                MessageBox.Show("sorcerer");
                classStructure.menu.Click += new System.EventHandler(sorcerer_Click);
                classStructure.menu.MouseEnter += new System.EventHandler(sorcerer_MouseEnter);
                classStructure.menu.MouseLeave += new System.EventHandler(sorcerer_MouseLeave);
                break;
            case "dragonknight":
                MessageBox.Show("dragonknight");
                classStructure.menu.Click += new System.EventHandler(dragonKnightText_Click);
                classStructure.menu.MouseEnter += new System.EventHandler(dragonKnightText_MouseEnter);
                classStructure.menu.MouseLeave += new System.EventHandler(dragonKnightText_MouseLeave);
                break;
            case "templar":
                MessageBox.Show("templar");
                classStructure.menu.Click += new System.EventHandler(templar_Click);
                classStructure.menu.MouseEnter += new System.EventHandler(templar_MouseEnter);
                classStructure.menu.MouseLeave += new System.EventHandler(templar_MouseLeave);
                break;
            case "nightblade":
                MessageBox.Show("nightblade");
                classStructure.menu.Click += new System.EventHandler(nightblade_Click);
                classStructure.menu.MouseEnter += new System.EventHandler(nightblade_MouseEnter);
                classStructure.menu.MouseLeave += new System.EventHandler(nightblade_MouseLeave);
                break;
        }
        this.Controls.Add(this.classStructure.menu);
    }

不显示任何按钮。求你帮帮我。

1 个答案:

答案 0 :(得分:2)

所有按钮的大小(0,0)和位置(0,0) 您错误地使用数组。例如。代码new int[89]创建一个包含89个元素的数组,所有元素都为零。但看起来你想要存储一个int值89 您需要以这样的方式更改代码:
声明

// 2 pairs of square brackets instead of 3
public int[][] Location;
public int[][] Size;

<强>初始化

Location = new int[][]
{
    new int[]
    {
        // Menu
        89,
        // Class
        325,
        // Reset Tree
        410,
        615,
        823
    },
...

使用:

    this.menu.Location = new System.Drawing.Point(Location[0][0], Location[1][0]);

但更好的方法是以这种方式重新设计您的位置和大小数据结构:

Point[] Location; // instead of int[][] Location
Size[] Size; // instead of int[][] Size