user3638277
user3638277

Reputation:

C# How do i change a label text from another form

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

Upvotes: 2

Views: 7301

Answers (2)

Steve
Steve

Reputation: 216283

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.

Upvotes: 3

failedprogramming
failedprogramming

Reputation: 2522

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

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.

Upvotes: 1

Related Questions