C# 如何从另一个窗体更改标签文本

本文关键字:标签 文本 窗体 另一个 | 更新日期: 2023-09-27 18:32:22

所以我有 2 种形式。

表单 1 是

我的主要表单,表单 2 是我在文本框中输入文本以显示在表单 1 的标签上的地方。此外,"确认"并实际更改我的标签输入文本的按钮位于表单 2 上,需要保持这种状态。

由于某种原因,这不起作用。

表单 2 有一个文本框和一个按钮,当我按下按钮时,它会更改指定字符串的字符串值。
此字符串链接到表单 1 上的标签。字符串正在更改,因此这不是问题,我通过添加一个按钮来确认这一点,该按钮会弹出一个显示新字符串值的消息框。

在寻找答案时,我发现一定是某种令人耳目一新的问题,我尝试了很多方法都没有成功。只有那些将我的按钮放在表格 1 而不是 2 上的人才能起作用的方法。

我已经连续 3 个小时在谷歌上搜索了如何解决这个问题,但要么这些方法不起作用,要么它们将我的按钮从表单 2 更改为我的主表单(表单 1)。

请不要说我懒惰,我真的找不到有效的方法!

编辑:

法典

GameScreen.cs
namespace _2pGame
{   
    public partial class GameScreen : Form
    {     
        public  GameScreen()
        {
            InitializeComponent();
            P1NameLabel.Text = gm.P1Name;
            P1ClassLabel.Text = gm.P1Class;
            P2NameLabel.Text = gm.P2Name;
            P2ClassLabel.Text = gm.P2Class;                
        }       
        private void PlayerInfoButton_Click(object sender, EventArgs e)
        {
            PlayerInfo playerinfoload = new PlayerInfo();
            playerinfoload.Show();
        }
   }    

}

玩家信息.cs

namespace _2pGame 
{       
    public partial class PlayerInfo : Form 
    { 
        public PlayerInfo() 
        {
            InitializeComponent();        
        }
        public void ConfirmPlayerInfo_Click(object sender, EventArgs e)
        {
            gm.P1Class = P1ClassChoice.Text;
            gm.P1Name = P1TextBox.Text;
            gm.P2Class = P2ClassChoice.Text;
            gm.P2Name = P2TextBox.Text;                     
        }   
    }
}

参考文献.cs

namespace _2pGame
{    
    public partial class gm
    {        
        public static string 
        P1Class,
        P2Class,
        P1Name,
        P2Name;        
    }
}

C# 如何从另一个窗体更改标签文本

解决这种众所周知的情况的方法是通过代表......

在您的玩家信息表单中声明

public partial class PlayerInfo : Form 
{ 
    // define the delegate type (a parameterless method that returns nothing)
    public delegate void OnConfirmPlayer();
    // declare a public variable of that delegate type
    public OnConfirmPlayer PlayerConfirmed;
    .....
    public void ConfirmPlayerInfo_Click(object sender, EventArgs e)
    {
        gm.P1Class = P1ClassChoice.Text;
        gm.P1Name = P1TextBox.Text;
        gm.P2Class = P2ClassChoice.Text;
        gm.P2Name = P2TextBox.Text;
        // Check is someone is interested to be informed of this change
        // If someone assign a value to the public delegate variable then
        // you have to call that method to let the subscriber know 
        if (PlayerConfirmed != null) 
            PlayerConfirmed();
    }
}

然后在游戏屏幕窗体中,就在显示玩家信息窗体之前,将公共PlayerInfo.PlayerConfirmed设置为游戏屏幕窗体类中的方法

private void PlayerInfoButton_Click(object sender, EventArgs e)
{
    PlayerInfo playerinfoload = new PlayerInfo();
    // Subscribe to the notification from PlayerInfo instance
    playerinfoload.PlayerConfirmed += PlayerHasBeenConfirmed;
    playerinfoload.Show();
}
// Method that receives the notification from PlayerInfo 
private void PlayerHasBeenConfirmed()
{
     P1NameLabel.Text = gm.P1Name;
     P1ClassLabel.Text = gm.P1Class;
     P2NameLabel.Text = gm.P2Name;
     P2ClassLabel.Text = gm.P2Class; 
}

此方法的优点是避免了游戏屏幕和玩家信息之间的耦合。无需在玩家信息中知道游戏屏幕表单的存在及其属性的名称。您只需发布一个委托,订阅者可以注册该委托以通知更改,并让订阅者根据自己的代码进行操作。

您需要引用主窗体,并在每次需要更新文本框值时分配文本框值。

public partial class PlayerInfo : Form 
{ 
    private readonly GameScreen _main;
    public PlayerInfo(GameScreen main) 
    {
        _main = main;
        InitializeComponent();        
     }
    public void ConfirmPlayerInfo_Click(object sender, EventArgs e)
    {
        gm.P1Class = P1ClassChoice.Text;
        gm.P1Name = P1TextBox.Text;
        gm.P2Class = P2ClassChoice.Text;
        gm.P2Name = P2TextBox.Text;
        main.P1NameLabel.Text = gm.P1Name;
        main.P1ClassLabel.Text = gm.P1Class;
        main.P2NameLabel.Text = gm.P2Name;
        main.P2ClassLabel.Text = gm.P2Class;  
    }
}

您还需要在创建玩家信息表单时传递引用

private void PlayerInfoButton_Click(object sender, EventArgs e)
    {
        PlayerInfo playerinfoload = new PlayerInfo(this); //pass ref to self
        playerinfoload.Show();
    }

请注意,还有其他更好的方法可以做到这一点,但这是我能想到的最简单的方法。如果你想要更好的东西,你可以看看事件或调解员模式。