C#中具有不同值和不同协调的标签

时间:2014-05-06 05:51:59

标签: c# winforms

我已完成编码,标签出现在右侧坐标中。有一个标签的问题多于我发布的号码。此外,只有一个标签显示价值。我想要所有显示不同的价值观?

//set the globals 

Random coor = new Random();
Label nodeLabel = new Label();
int xCoor;
int yCoor;

//set the button to show  the labels 

private void btnRun_Click(object sender, EventArgs e)
{
    for (int x = 0; x < 15; x++)
    {
        //X Coordinate 
        for (int i = 0; i < 1; i++)
        {
            xCoor = coor.Next(0, 750);
        }

        //Y Coordinate
        for (int u = 0; u < 1; u++)
        {
            yCoor = coor.Next(0, 500);
        }

        //Set up the labels
        for (int l = 0; l < value; l++)
        {
            nodeLabel.Text = value + " ";
            nodeLabel.AutoSize = true;

            nodeLabel.Location = new Point(xCoor + 10, yCoor + 5);
        }
        this.Controls.Add(nodeLabel);
    }

}

查看正在运行的程序图片的行 http://i.stack.imgur.com/J9Xn9.png

2 个答案:

答案 0 :(得分:1)

假设您需要15个标签,那么:

Random rand = new Random();
int label_amount = 15;
int xCoor;
int yCoor;

private void btnRun_Click(object sender, EventArgs e)
{
    //set the button to show  the labels 
    for (int x = 0; x < label_amount; x++)
    {
        xCoor = coor.Next(0, 750);
        yCoor = coor.Next(0, 500);

        Label nodeLabel = new Label();  
        nodeLabel.Text = value + " " + xCoor + "," + yCoor;
        nodeLabel.AutoSize = true;
        nodeLabel.Location = new Point(xCoor + 10, yCoor + 5);
        // Add your label to whatever you're adding it
    }
}

在您的代码中,您只是运行奇怪的循环,没有任何意义。

答案 1 :(得分:0)

您正在定义错误的循环变量。它应该是我或我或您选择的任何其他名称而不是1。

在您定义值变量的地方,它已丢失。