[英]C# How do i change a label text from another form
So i have 2 forms. 所以我有2种形式。
Form 1 is my main form, and form 2 is where I enter text in a textbox to be displayed on a label on form 1. Also the button to 'confirm' and actually change the entered text of my label is on form 2 which needs to stay that way. 表单1是我的主要表单,表单2是我在文本框中输入要在表单1的标签上显示的文本的地方。“确认”并实际更改标签的输入文本的按钮在表单2上,需要保持这种方式。
for some reason this does not work. 由于某种原因,这不起作用。
Form 2 has a text-box and a button, when I press the button, it changes the string value of the designated string. 窗体2有一个文本框和一个按钮,当我按该按钮时,它将更改指定字符串的字符串值。
This string is linked to a label on form 1. the string is being changed so that is not the problem I confirmed this by a adding a button which pops up a message box showing the new string value. 该字符串链接到表格1上的标签。该字符串正在更改,因此这不是问题,我通过添加一个按钮来弹出一个显示新字符串值的消息框,从而确认了这一点。
While searching for an answer I found that is must be some sort of refreshing problem, I tried a lot of methods with no success. 在寻找答案时,我发现这一定是令人耳目一新的问题,但我尝试了许多方法,但均未成功。 Only methods that did work where those who would put my button onto form 1 instead of 2. 只有在那些将我的按钮放在表格1而不是表格2上的方法才有效。
I've been googling for 3 hours straight on how to fix this problem but either the methods don't work or they change my button from form 2 to my main form (form 1). 我已经连续搜索了三个小时,以解决该问题,但是方法不起作用或者它们将我的按钮从表格2更改为我的主要表格(表格1)。
Please don't call me lazy I really can't find a method that works! 请不要叫我懒,我真的找不到有效的方法!
EDIT: 编辑:
Code 码
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 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 Refs.cs
namespace _2pGame
{
public partial class gm
{
public static string
P1Class,
P2Class,
P1Name,
P2Name;
}
}
An approach to this very well know situation is through delegates.... 解决这种众所周知的情况的方法是通过代表。
In your PlayerInfo form declare 在您的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();
}
}
Then in your GameScreen form, just before showing the PlayerInfo form, set the public PlayerInfo.PlayerConfirmed
to a method into the GameScreen form class 然后在您的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;
}
This approach has the advantage to avoid a coupling between the GameScreen and the PlayerInfo. 这种方法的优点是避免了GameScreen和PlayerInfo之间的耦合。 No need to know inside the PlayerInfo the existance of a GameScreen form and the name of its properties. 无需知道PlayerInfo内是否存在GameScreen表单及其属性的名称。 You just publish a delegate that a subscriber could register to be informed of the changes and let the subscriber acts on its own code. 您只需发布一个委托人,该委托人可以注册以了解更改,并让其按照自己的代码行事。
You need a reference to your main form and assign the textbox values each time they need to be updated. 您需要引用主表单,并在每次需要更新文本框值时分配它们。
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;
}
}
You also need to pass the reference when the PlayerInfo form is created 创建PlayerInfo表单时,您还需要传递引用
private void PlayerInfoButton_Click(object sender, EventArgs e)
{
PlayerInfo playerinfoload = new PlayerInfo(this); //pass ref to self
playerinfoload.Show();
}
Note that there are other better ways to do this, but this is the easiest that I can think of. 请注意,还有其他更好的方法可以执行此操作,但这是我能想到的最简单的方法。 You can probably look at events or Mediator pattern if you want something better. 如果您想要更好的东西,则可以查看事件或调解器模式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.