【问题标题】:How can i change label text from different button in C#如何从 C# 中的不同按钮更改标签文本
【发布时间】:2014-01-27 16:23:50
【问题描述】:

我在脚本 cmets 中描述了我的问题。函数 GraphicClassStructure 仅用于创建按钮和标签。但主要功能是plusButton_click。如果我点击第一个加号按钮,我需要它,所以我需要更改第一个添加标签的文本。

脚本:(问题在这里:plusButton_click,我将问题描述为案例)

class GraphicClassStructure : GraphicPosition
{
    // Button
    public Button plus;
    public PictureBox classBackround = new PictureBox();
    // Label
    Label points;

    public void CreateSpellsButton()
    {
        for (int j = 0; j < 3; j++)
        {
            for (int i = 0; i < 10; i++)
            {
                plus = new Button();
                points = new Label();

                switch (j)
                {
                    case 0:
                        points.Location = Location[1][i];
                        points.Location = new Point(points.Location.X + 8, points.Location.Y + 45);
                        plus.Location = Location[2][i];
                        break;
                    case 1:
                        plus.Location = Location[2][i];
                        plus.Location = new Point(plus.Location.X + 205, plus.Location.Y);
                        points.Location = Location[1][i];
                        points.Location = new Point(points.Location.X + 213, points.Location.Y + 45);
                        break;
                    case 2:
                        plus.Location = Location[2][i];
                        plus.Location = new Point(plus.Location.X + 410, plus.Location.Y);
                        points.Location = Location[1][i];
                        points.Location = new Point(points.Location.X + 418, points.Location.Y + 45);
                        break;
                }

                // Labels for point

                points.BackColor = Color.Transparent;
                points.ForeColor = Color.FromArgb(((int)(((byte)(193)))), ((int)(((byte)(196)))), ((int)(((byte)(181)))));
                points.BackgroundImageLayout = ImageLayout.Stretch;
                points.FlatStyle = FlatStyle.Flat;
                points.Name = "points";
                points.Name = spells.Name + i.ToString() + "_" + j.ToString();
                if (i >= 6)
                    points.Text = "0 / 2";
                else
                    points.Text = "0 / 1";
                points.VisibleChanged += new EventHandler(classUniqueButtons_VisibleChanged);

                // Plus
                plus.BackColor = Color.Transparent;
                plus.BackgroundImage = BuildResource.plus;
                plus.BackgroundImageLayout = ImageLayout.Stretch;
                plus.FlatAppearance.BorderSize = 0;
                plus.FlatAppearance.MouseDownBackColor = Color.Transparent;
                plus.FlatAppearance.MouseOverBackColor = Color.Transparent;
                plus.FlatStyle = FlatStyle.Flat;
                plus.Name = "plus";
                plus.Size = Size[0][9];
                plus.UseVisualStyleBackColor = false;
                plus.Name = plus.Name + i.ToString() + "_" + j.ToString();
                plus.Click += new EventHandler(plusButton_click);
                plus.VisibleChanged += new EventHandler(classUniqueButtons_VisibleChanged);
                plus.MouseEnter += new EventHandler(this.classButton_MouseEnter);
                plus.MouseLeave += new EventHandler(this.classButton_MouseLeave);

                classBackround.Controls.Add(plus);
                classBackround.Controls.Add(points);
            }
        }
    }

    private void plusButton_click(object sender, EventArgs e)
    {
        var currentButton = sender as Button;
        var name = currentButton.Name;

        switch (name)
        {
            // Ultimate
            case "plus0_0":
                // If i clicked on this button so i need change text for first added label
                // I try this, but it changed only last added labe
                points.Text = "test";
                break;
            case "plus0_1":
                // If i clicked on this button so i need change text for second added label
                break;
        }
    }

    private void minusButton_click(object sender, EventArgs e)
    {
        var currentButton = sender as Button;
        var name = currentButton.Name;

        switch (name)
        {
            // Ultimate
            case "minus0_0":
                // If i clicked on this button so i need change text for first added label
                // I try this, but it changed only last added labe
                points.Text = "test";
                break;
            case "minus0_1":
                // If i clicked on this button so i need change text for second added label
                break;
        }
    }
}

【问题讨论】:

  • 什么是咒语?它包含什么?

标签: c# winforms button label


【解决方案1】:

在这里做你需要的很简单

 for (int j = 0; j < 3; j++)
        {
            for (int i = 0; i < 10; i++)
            {
                plus = new Button();
                //tag is an object and can be used to reference any  other object 
                plus.Tag = new Label();

并检索您需要的内容

 private void plusButton_click(object sender, EventArgs e)
    {
        var currentButton = sender as Button;
        var name = currentButton.Name;

        switch (name)
        {
            // Ultimate
            case "plus0_0":

                (currentButton.Tag as Label).Text = "test";
                break;
            case "plus0_1":
                (currentButton.Tag as Label).Text ="test2"
                break;
        }
    }

【讨论】:

    【解决方案2】:

    您正在使用对您在循环的每次迭代中覆盖的标签的全局引用。因此它总是引用最后一个标签。由于您总是在按钮后添加标签并引用按钮,因此您可能会得到正确的标签,如下所示:

    Label pointsLabel = (Label)classBackround.Controls[classBackround.Controls.IndexOf(currentButton) + 1]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-12
      • 2014-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-28
      • 2015-11-24
      • 1970-01-01
      相关资源
      最近更新 更多