![](/img/trans.png)
[英]C#: How can i change the text of a label thats in form1 from another class?
[英]C# How do i change a label text from another form
所以我有2種形式。
表單1是我的主要表單,表單2是我在文本框中輸入要在表單1的標簽上顯示的文本的地方。“確認”並實際更改標簽的輸入文本的按鈕在表單2上,需要保持這種方式。
由於某種原因,這不起作用。
窗體2有一個文本框和一個按鈕,當我按該按鈕時,它將更改指定字符串的字符串值。
該字符串鏈接到表格1上的標簽。該字符串正在更改,因此這不是問題,我通過添加一個按鈕來彈出一個顯示新字符串值的消息框,從而確認了這一點。
在尋找答案時,我發現這一定是令人耳目一新的問題,但我嘗試了許多方法,但均未成功。 只有在那些將我的按鈕放在表格1而不是表格2上的方法才有效。
我已經連續搜索了三個小時,以解決該問題,但是方法不起作用或者它們將我的按鈕從表格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();
}
}
}
PlayerInfo.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;
}
}
}
Refs.cs
namespace _2pGame
{
public partial class gm
{
public static string
P1Class,
P2Class,
P1Name,
P2Name;
}
}
解決這種眾所周知的情況的方法是通過代表。
在您的PlayerInfo表單中聲明
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();
}
}
然后在您的GameScreen表單中,就在顯示PlayerInfo表單之前,將公共PlayerInfo.PlayerConfirmed
設置為GameScreen表單類中的方法
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;
}
這種方法的優點是避免了GameScreen和PlayerInfo之間的耦合。 無需知道PlayerInfo內是否存在GameScreen表單及其屬性的名稱。 您只需發布一個委托人,該委托人可以注冊以了解更改,並讓其按照自己的代碼行事。
您需要引用主表單,並在每次需要更新文本框值時分配它們。
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;
}
}
創建PlayerInfo表單時,您還需要傳遞引用
private void PlayerInfoButton_Click(object sender, EventArgs e)
{
PlayerInfo playerinfoload = new PlayerInfo(this); //pass ref to self
playerinfoload.Show();
}
請注意,還有其他更好的方法可以執行此操作,但這是我能想到的最簡單的方法。 如果您想要更好的東西,則可以查看事件或調解器模式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.