C#如何从另一个表单更改标签文本 [英] C# How do i change a label text from another form

查看:169
本文介绍了C#如何从另一个表单更改标签文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



表单1是我的主要表单,表单2是我在文本框中输入文本以显示在表单上的标签上1.另外按钮'确认',实际上改变我的标签输入的文本是在表格2,这需要保持这种方式。



由于某种原因,这不工作。

表格2有一个文本框和一个按钮,当我按下按钮时,它会更改指定字符串的字符串值。

这个字符串链接到表单1上的一个标签。字符串正在被改变,所以这不是问题,我通过添加一个按钮弹出一个显示新字符串值的消息框来证实这一点。



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



我一直在搜索3个小时,直接找到如何解决这个问题,但或者这些方法不起作用,或者他们将我的按钮从表单2更改为我的主表单(表单1)。



请不要叫我懒惰我真的可以' t

编辑:



代码

  GameScreen.cs 

命名空间_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

 命名空间_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

 命名空间_2pGame 
{
public partial class gm
{
public static string
P1Class,
P2Class,
P1Name,
P2Name;


$ / code>


解决方案

通过委托来解决这个问题。
//定义委托类型(无参数方法,不返回任何内容)
public delegate void OnConfirmPlayer();

//声明该委托类型的公共变量
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;

//检查是否有人有兴趣知道这个变化
//如果有人为public delegate变量赋值,那么
//你必须调用这个方法让用户知道
if(PlayerConfirmed!= null)
PlayerConfirmed();


$ / code>

然后在你的GameScreen表单中,在显示PlayerInfo表单,将公共 PlayerInfo.PlayerConfirmed 设置为GameScreen表单类的一个方法

  private void PlayerInfoButton_Click(object sender,EventArgs e)
{
PlayerInfo playerinfoload = new PlayerInfo();

//订阅来自PlayerInfo实例的通知
playerinfoload.PlayerConfirmed + = PlayerHasBeenConfirmed;

playerinfoload.Show();
}

//从PlayerInfo接收通知的方法
private void PlayerHasBeenConfirmed()
{
P1NameLabel.Text = gm.P1Name;
P1ClassLabel.Text = gm.P1Class;
P2NameLabel.Text = gm.P2Name;
P2ClassLabel.Text = gm.P2Class;
}

这种方法有利于避免GameScreen和PlayerInfo之间的耦合。无需知道PlayerInfo内部是否存在GameScreen表单及其属性的名称。您只需发布一个委托人,即订阅者可以注册以通知更改并让订阅者执行自己的代码。


So i have 2 forms.

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.

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.
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.

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.

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).

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

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

解决方案

An approach to this very well know situation is through delegates....

In your PlayerInfo form declare

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

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. No need to know inside the PlayerInfo the existance of a GameScreen form and the name of its properties. 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.

这篇关于C#如何从另一个表单更改标签文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆