简体   繁体   English

按钮文字未出现C#

[英]Button text not appearing c#

I am writing a chess game program using windows forms (System.Windows.Forms) and I made a button to start a new game. 我正在使用Windows窗体(System.Windows.Forms)编写国际象棋游戏程序,并按下了一个按钮以开始新游戏。 For some reason the text that the button is supposed to display isn't showing at all. 由于某种原因,按钮应该显示的文本根本没有显示。 Other than that, the button functions perfectly well. 除此之外,该按钮功能非常好。

How Can I fix it? 我该如何解决?

Code: 码:

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Chess.Properties;

public class Form1 : Form
{
    PictureBox[,] tiles = new PictureBox[8, 8];
    Button newGame = new Button();
    public Form1()
    {
        this.MinimumSize = new Size(560, 519);
        this.MaximumSize = new Size(560, 519);
        newGame.Click += new EventHandler(newGame_Click);
        newGame.Size = new Size(560, 519);
        newGame.Location = new Point(481, 0);
        newGame.Text = "New Game";
        newGame.Font = new Font(FontFamily.GenericSansSerif, 24.0f, FontStyle.Bold);
        this.Controls.Add(newGame);
        for (int x = 0; x < 8; x++)
            for (int y = 0; y < 8; y++)
            {
                tiles[x, y] = new PictureBox();
                tiles[x, y].Size = new Size(60, 60);
                tiles[x, y].Location = new Point(x * 60, y * 60);
                tiles[x, y].Name = "";
                this.Controls.Add(tiles[x, y]);
                if (x == 0)
                    if (y % 2 == 0)
                        tiles[x, y].BackColor = Color.Gray;
                    else tiles[x, y].BackColor = Color.White;
                else if (tiles[x - 1, y].BackColor == Color.Gray)
                    tiles[x, y].BackColor = Color.White;
                else tiles[x, y].BackColor = Color.Gray;
            }
    }

    void newGame_Click(Object sender, EventArgs e)
    {
        //Initial White Pieces
        tiles[0, 0].Image = Resources.wTower;
        tiles[0, 1].Image = Resources.wHorse;
        tiles[0, 2].Image = Resources.wRunner;
        tiles[0, 3].Image = Resources.wQueen;
        tiles[0, 4].Image = Resources.wKing;
        tiles[0, 5].Image = Resources.wRunner;
        tiles[0, 6].Image = Resources.wHorse;
        tiles[0, 7].Image = Resources.wTower;
        tiles[0, 0].Name = "wTower";
        tiles[0, 1].Name = "wHorse";
        tiles[0, 2].Name = "wRunner";
        tiles[0, 3].Name = "wQueen";
        tiles[0, 4].Name = "wKing";
        tiles[0, 5].Name = "wRunner";
        tiles[0, 6].Name = "wHorse";
        tiles[0, 7].Name = "wTower";
        for(int i = 0; i < 8; i++)
        {
            tiles[1, i].Image = Resources.wSoldier;
            tiles[1, i].Name = "wSoldier";
        }
        //Initial Black Pieces
        tiles[7, 0].Image = Resources.bTower;
        tiles[7, 1].Image = Resources.bHorse;
        tiles[7, 2].Image = Resources.bRunner;
        tiles[7, 3].Image = Resources.bQueen;
        tiles[7, 4].Image = Resources.bKing;
        tiles[7, 5].Image = Resources.bRunner;
        tiles[7, 6].Image = Resources.bHorse;
        tiles[7, 7].Image = Resources.bTower;
        tiles[7, 0].Name = "bTower";
        tiles[7, 1].Name = "bHorse";
        tiles[7, 2].Name = "bRunner";
        tiles[7, 3].Name = "bQueen";
        tiles[7, 4].Name = "bKing";
        tiles[7, 5].Name = "bRunner";
        tiles[7, 6].Name = "bHorse";
        tiles[7, 7].Name = "bTower";
        for (int i = 0; i < 8; i++)
        {
            tiles[6, i].Image = Resources.bSoldier;
            tiles[6, i].Name = "bSoldier";
        }
    }

    [STAThread]
    static void Main()
    {
        Application.Run(new Form1());
    }
}

Resources: My resources 资源: 我的资源

Your button is way to large and you are seeing just the left side portion of it , which excludes the part with the text. 您的按钮变大了,您只看到它的左侧部分,其中不包括带有文本的部分。 Try using this for your button size instead: 尝试使用此按钮来代替按钮大小:

        newGame.Size = new Size(60, 519);

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

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