Pass txtBox value to Label (between Windows Form Forms) - However, string is null

Asked

Viewed 58 times

2

Good afternoon, I’m a few days cracking my head here. I’m developing an app with 2 Forms (screens). On the home screen is login, and the second screen is the app itself. I want to take the name entered by the user in the Txtbox of the home screen, and move to a label of the second screen, getting: Welcome(a) {usersname}!

I debugged the code, and for some reason, the labelWelcome variable initially receives the value, however, it does not receive the variable, the correct mode would be: labelWelcome.Text = $"Welcome(a) {Username}";

But she receives only: labelWelcome.Text = $"Welcome(to)";

DETAIL: The Username var does receive the value of FORM1, why is it not going on the FORM2 label?

It follows logic I used to pass the values:

**FORM1 (LOGIN):**
                // Método para abrir a nova tela
                private void FormLogado()
                {
                Application.Run(new CronogramaLogado(btn_login.Text));
                }

                *// Codificação para abrir nova tela
                Logado = new Thread(FormLogado);
                Logado.SetApartmentState(ApartmentState.STA);
                
                // String que recebe o nome do usuário da txtbox
                string nomeusuario = btn_login.Text;
                // instanciação da tela 2 chamada CronogramaLogado, e passando o valor do nome do user
                CronogramaLogado nome = new CronogramaLogado(nomeusuario);

                // "this.Close() fecha a tela anterior, e mantém somente a nova aberta"
                this.Close();

                // Abre a nova tela
                Logado.Start();*

FORM 2 (APP AFTER LOGGING):

public partial class CronogramaLogado : Form
{
    public string Username;
    // Obrigando passar o valor username quando chamar a segunda tela
    public CronogramaLogado(string username)
    {
        InitializeComponent();
        Username = username;
        labelWelcome.Text = $"Seja bem-vindo(a) {Username}";
        // Método para gerar as colunas assim que a tela é aberta
        GerarColunas();
    }
}

2 answers

0

You can create a property in Form and set this value when instantiating:

public class CronogramaLogado
{
   public string UsuarioLogado { get; set; }
}

private void FormLogado() 
{ 
    var cronograma = new CronogramaLogado
    {
       UsuarioLogado = btn_login.Text;
    };
    Application.Run(cronograma); 
}

Then you can read the Press in the form "Chronologated".

private void CronogramaLogado_Load(object sender, EventArgs e)
{
    labelWelcome.Text = $"Seja bem-vindo(a) {UsuarioLogado}";
}
  • Unfortunately it hasn’t worked yet. In " var schedule = Timelogged " says I can’t use Timelogged in the given context. The way I made up the Form2 vars get value, however, in the =/

  • ah in my example missed the new :) already edited the question

0


One way I use to pass Objects between screens, is to pass Form1 in form2.

                public string nomeusuario = "";

                private void FormLogado()
                {
                   Application.Run(new CronogramaLogado(btn_login.Text));
                }

                *// Codificação para abrir nova tela
                Logado = new Thread(FormLogado);
                Logado.SetApartmentState(ApartmentState.STA);
                
                // String que recebe o nome do usuário da txtbox
                nomeusuario = btn_login.Text;
                // instanciação da tela 2 chamada CronogramaLogado, e passando o valor do nome do user
                CronogramaLogado nome = new CronogramaLogado(this); // aqui eu passo o 
                                                                    // form 1

                this.Close();

                // Abre a nova tela
                Logado.Start();

HERE FORM 2

public partial class CronogramaLogado : Form
{
    // Obrigando passar o valor username quando chamar a segunda tela
    public CronogramaLogado(FormLogado form1)
    {
        InitializeComponent();

        labelWelcome.Text = $"Seja bem-vindo(a) {form1.nomeusuario }";
        // Método para gerar as colunas assim que a tela é aberta
        GerarColunas();
    }
} 
  • Thank you so much for your attention! Problem has been solved.

  • this can solve, but it is an exaggeration to pass an entire object being that only needs a string, it would be much easier to pass only the string

  • Could you give me an example of how I’d look passing only the string?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.