简体   繁体   English

如何从C#中的其他按钮更改标签文本

[英]How can i change label text from different button in C#

I descripted my problem in script comments. 我在脚本注释中描述了我的问题。 Function GraphicClassStructure is only for create buttons and label. 函数GraphicClassStructure仅用于创建按钮和标签。 But mainly function is plusButton_click. 但主要功能是plusButton_click。 I need that if i clicked on first plus button so i need change text for first added label. 我需要如果我单击第一个加号按钮,那么我需要为第一个添加的标签更改文本。

Script: (Problem is here: plusButton_click, i descripted problem to case) 脚本:(问题在这里: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;
        }
    }
}

very simple to do here what you need 在这里做您需要的东西非常简单

 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();

and to retrieve what you need do this 并检索您所需的内容

 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;
        }
    }

You are using a global reference to the label which you have overwritten in each iteration of your loop. 您正在使用对循环的每次迭代中覆盖的标签的全局引用。 Therefore it always references the last label. 因此,它始终引用最后一个标签。 Since you always add the label after the button and have the reference to the button you can probably get the correct label like this: 由于您总是在按钮后添加标签并获得对按钮的引用,因此您可能会获得正确的标签,如下所示:

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

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

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