2014-01-25 67 views
-1

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

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

不显示任何按钮。请帮帮我。

+0

'某处在我的代码是bug.'调试它的话,它可能会帮助你。尽量减少你正在处理的代码量,你很快就会找到一个罪魁祸首。 –

+0

从您发布的代码中,我看不到按钮添加到窗体的位置。使用设计器为开始添加按钮。动态创建按钮是一项高级任务。 – PMF

回答

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 
+0

您的解决方案帮助了我,真的很棒,我使用您的意见的点和大小。 –