簡體   English   中英

如何從C#中的其他按鈕更改標簽文本

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

我在腳本注釋中描述了我的問題。 函數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;
        }
    }
}

在這里做您需要的東西非常簡單

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

您正在使用對循環的每次迭代中覆蓋的標簽的全局引用。 因此,它始終引用最后一個標簽。 由於您總是在按鈕后添加標簽並獲得對按鈕的引用,因此您可能會獲得正確的標簽,如下所示:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM