Ir ao conteúdo
  • Cadastre-se

Form2 recebendo valor da Form1


nexus.tecinfo

Posts recomendados

Bom dia galera conectada...

estou com um problema,

estou montando um jogo da velha onde na form 1 os jogadores colocam seus nomes e clica no botão jogar, quando clica neste botão vai para o form2, neste form2 irá receber os nomes dos jogadores, irei declarar as variaveis como sendo jogador1 e jogador2, como faço para estas receberem o valor do form1?

Link para o comentário
Compartilhar em outros sites

então cara faz assim:

no form2 você coloca no construtor as variaveis que serao usado para passar o nome e logo depois passa os valores para as label, ficando assim


public Form2(String nome1, String nome2)
{
InitializeComponent();
nomerecebido1.Text = nome1;
nomerecebido2.Text = nome2;
}

os nomerecebido1 e nomerecebido2 sao label.

no Form1 você coloca isso no click do botao


private void button1_Click(object sender, EventArgs e)
{
Form2 fm = new Form2(nome1.Text, nome2.Text);
fm.Show();
}

os nome1 e nome2 são textbox.

esse é uma maneira mais fácil de fazer, mais é possivel fazer de outras maneiras.

obs. isso eu fiz usando c#, caso for vb.net me avise que te ajudo também. mais é praticamente a mesma coisa.

Espero ter te ajudado,

Abraços,

Francisco Edson.

Link para o comentário
Compartilhar em outros sites

Mortts

eu fiz este codigo, mais esta dando erro de mais

este é do Frm2



public FrmVelha(String Jogador1, String Jogador2)
{

InitializeComponent();
Lbl1.Text = Jogador1;
Lbl2.Text = Jogador2;
ganhador = 0;

}

private void FrmVelha_Load(object sender, EventArgs e)
{
LblJogador1.Text == "Jogador " + this.Jogador1;
}

private void Btn1_Click(object sender, EventArgs e)
{
if (ganhador == 0 && Btn1.Text == "")
{
if (Jogador1 == 1)
{
Btn1.ForeColor = Color.CornflowerBlue;
Btn1.Text = "X";
this.Jogador2 = 2;
LblJogador.Text = "";

}
else
{
Btn1.ForeColor = Color.ForestGreen;
Btn1.Text = "O";
this.Jogador1 = 1;
LblJogador.Text = "";
}
}
Resultado();
}

private void Btn2_Click(object sender, EventArgs e)
{
if (ganhador == 0 && Btn2.Text == "")
{
if (this.Jogador1 == 1)
{
Btn2.ForeColor = Color.CornflowerBlue;
Btn2.Text = "X";
this.Jogador2 = 2;
LblJogador.Text = "";

}
else if (Jogador2 == 2)
{
Btn2.ForeColor = Color.ForestGreen;
Btn2.Text = "O";
this.Jogador1 = 1;
LblJogador.Text = "";
}
}
Resultado();
}

private void Btn3_Click(object sender, EventArgs e)
{
if (ganhador == 0 && Btn3.Text == "")
{
if (this.Jogador1 == 1)
{
Btn3.ForeColor = Color.CornflowerBlue;
Btn3.Text = "X";
Jogador2 = 2;
LblJogador.Text = "";

}
else if (Jogador2 == 2)
{
Btn3.ForeColor = Color.ForestGreen;
Btn3.Text = "O";
this.Jogador1 = 1;
LblJogador.Text = "";
}
}
Resultado();
}

private void Btn4_Click(object sender, EventArgs e)
{
if (ganhador == 0 && Btn4.Text == "")
{
if (this.Jogador1 == 1)
{
Btn4.ForeColor = Color.CornflowerBlue;
Btn4.Text = "X";
this.Jogador2 = 2;
LblJogador.Text = "";

}
else if (Jogador2 == 2)
{
Btn4.ForeColor = Color.ForestGreen;
Btn4.Text = "O";
this.Jogador1 = 1;
LblJogador.Text = "";
}
}
Resultado();
}

private void Btn5_Click(object sender, EventArgs e)
{
if (ganhador == 0 && Btn5.Text == "")
{
if (this.Jogador1 == 1)
{
Btn5.ForeColor = Color.CornflowerBlue;
Btn5.Text = "X";
this.Jogador2 = 2;
LblJogador.Text = "";

}
else if (Jogador2 == 2)
{
Btn5.ForeColor = Color.ForestGreen;
Btn5.Text = "O";
this.Jogador1 = 1;
LblJogador.Text = "";
}
}
Resultado();
}

private void Btn6_Click(object sender, EventArgs e)
{
if (ganhador == 0 && Btn6.Text == "")
{
if (this.Jogador1 == 1)
{
Btn6.ForeColor = Color.CornflowerBlue;
Btn6.Text = "X";
this.Jogador2 = 2;
LblJogador.Text = "";

}
else if (Jogador2 == 2)
{
Btn6.ForeColor = Color.ForestGreen;
Btn6.Text = "O";
this.Jogador1 = 1;
LblJogador.Text = "";
}
}
Resultado();
}

private void Btn7_Click(object sender, EventArgs e)
{
if (ganhador == 0 && Btn7.Text == "")
{
if (this.Jogador1 == 1)
{
Btn7.ForeColor = Color.CornflowerBlue;
Btn7.Text = "X";
this.Jogador2 = 2;
LblJogador.Text = "";

}
else if (Jogador2 == 2)
{
Btn7.ForeColor = Color.ForestGreen;
Btn7.Text = "O";
this.Jogador1 = 1;
LblJogador.Text = "JOGADOR 1";
}
}
Resultado();
}

private void Btn8_Click(object sender, EventArgs e)
{
if (ganhador == 0 && Btn8.Text == "")
{
if (this.Jogador1 == 1)
{
Btn8.ForeColor = Color.CornflowerBlue;
Btn8.Text = "X";
this.Jogador2 = 2;
LblJogador.Text = "";

}
else if (Jogador2 == 2)
{
Btn8.ForeColor = Color.ForestGreen;
Btn8.Text = "O";
this.Jogador1 = 1;
LblJogador.Text = "";
}
}
Resultado();
}

private void Btn9_Click(object sender, EventArgs e)
{
if (ganhador == 0 && Btn9.Text == "")
{
if (this.Jogador1 == 1)
{
Btn9.ForeColor = Color.CornflowerBlue;
Btn9.Text = "X";
this.Jogador2 = 2;
LblJogador.Text = "";

}
else if (Jogador2 == 2)
{
Btn9.ForeColor = Color.ForestGreen;
Btn9.Text = "O";
this.Jogador1 = 1;
LblJogador.Text = "";
}
}
Resultado();
}

e este é do Frm1



private void BtnJogar_Click(object sender, EventArgs e)
{
FrmVelha fm = new FrmVelha(TxtJogador1.Text,TxtJogador2.Text);
fm.Show();
}

Link para o comentário
Compartilhar em outros sites

Qual erro que esta dando?

-- edit --

nessa parte



public FrmVelha(String Jogador1, String Jogador2)
{

InitializeComponent();
Lbl1.Text = Jogador1;
Lbl2.Text = Jogador2;
ganhador = 0;

}

private void FrmVelha_Load(object sender, EventArgs e)
{
LblJogador1.Text == "Jogador " + this.Jogador1;
}

você pode fazer assim



public FrmVelha(String Jogador1, String Jogador2)
{

InitializeComponent();

LblJogador1.Text = "Jogador " + Jogador1;
Lbl2.Text = Jogador2;
ganhador = 0;

}

não precisa colocar no load, pois no contrutor ele ja seta o valor na label.

so uma duvida, você coloca o nome do jogador 2 vezes no form? pois você esta setando a label Lbl1 e depois o LblJogador1 e faz o mesmo com da labal do jogador 2, acho que você poderia deixar apenas o LblJogador1 e LblJogador2.

Link para o comentário
Compartilhar em outros sites

Mortts, olha este codigo porque eu não sei mais onde consertar rsrs

FrmPrincipal // neste form as pessoas irão colocar seus nomes

Label1 (entre com o nome dos jogadores)

Lbl1 (Jogador 1)

Lbl2 (jogador 2)

TxtJogador1

TxtJogador2

Btn (jogar) // clicando nele vai abri o form do jogo


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Jogo
{
public partial class FrmPrincipal : Form
{

public static String jogador; // variável pública e estática, acessível para todos.

public FrmPrincipal()
{
InitializeComponent();
}
}
}

este outro é o FrmVelha (o jogo em si, onde irá receber os nomes do FrmPrincipal)



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Jogo
{
public partial class FrmVelha : Form
{


public FrmVelha(String Jogador1, String Jogador2)
{

private void Btn1_Click(object sender, EventArgs e)
{
InitializeComponent();

if (ganhador == 0 && Btn1.Text == "")
{
if (Jogador == 1)
{
Btn1.ForeColor = Color.CornflowerBlue;
Btn1.Text = "X";
this.Jogador = 2;
LblJogador.Text = "";

}
else
{
Btn1.ForeColor = Color.ForestGreen;
Btn1.Text = "O";
this.Jogador = 1;
LblJogador.Text = "";
}
}
Resultado();
}

private void Btn2_Click(object sender, EventArgs e)
{
if (ganhador == 0 && Btn2.Text == "")
{
if (this.Jogador == 1)
{
Btn2.ForeColor = Color.CornflowerBlue;
Btn2.Text = "X";
this.Jogador = 2;
LblJogador.Text = "";

}
else if (Jogador2 == 2)
{
Btn2.ForeColor = Color.ForestGreen;
Btn2.Text = "O";
this.Jogador = 1;
LblJogador.Text = "";
}
}
Resultado();
}

private void Btn3_Click(object sender, EventArgs e)
{
if (ganhador == 0 && Btn3.Text == "")
{
if (this.Jogador == 1)
{
Btn3.ForeColor = Color.CornflowerBlue;
Btn3.Text = "X";
Jogador = 2;
LblJogador.Text = "";

}
else if (Jogador == 2)
{
Btn3.ForeColor = Color.ForestGreen;
Btn3.Text = "O";
this.Jogador1 = 1;
LblJogador.Text = "";
}
}
Resultado();
}

private void Btn4_Click(object sender, EventArgs e)
{
if (ganhador == 0 && Btn4.Text == "")
{
if (this.Jogador == 1)
{
Btn4.ForeColor = Color.CornflowerBlue;
Btn4.Text = "X";
this.Jogador = 2;
LblJogador.Text = "";

}
else if (Jogador == 2)
{
Btn4.ForeColor = Color.ForestGreen;
Btn4.Text = "O";
this.Jogador = 1;
LblJogador.Text = "";
}
}
Resultado();
}

private void Btn5_Click(object sender, EventArgs e)
{
if (ganhador == 0 && Btn5.Text == "")
{
if (this.Jogador == 1)
{
Btn5.ForeColor = Color.CornflowerBlue;
Btn5.Text = "X";
this.Jogador = 2;
LblJogador.Text = "";

}
else if (Jogador2 == 2)
{
Btn5.ForeColor = Color.ForestGreen;
Btn5.Text = "O";
this.Jogador = 1;
LblJogador.Text = "";
}
}
Resultado();
}

private void Btn6_Click(object sender, EventArgs e)
{
if (ganhador == 0 && Btn6.Text == "")
{
if (this.Jogador == 1)
{
Btn6.ForeColor = Color.CornflowerBlue;
Btn6.Text = "X";
this.Jogador = 2;
LblJogador.Text = "";

}
else if (Jogador == 2)
{
Btn6.ForeColor = Color.ForestGreen;
Btn6.Text = "O";
this.Jogador = 1;
LblJogador.Text = "";
}
}
Resultado();
}

private void Btn7_Click(object sender, EventArgs e)
{
if (ganhador == 0 && Btn7.Text == "")
{
if (this.Jogador == 1)
{
Btn7.ForeColor = Color.CornflowerBlue;
Btn7.Text = "X";
this.Jogador = 2;
LblJogador.Text = "";

}
else if (Jogador2 == 2)
{
Btn7.ForeColor = Color.ForestGreen;
Btn7.Text = "O";
this.Jogador = 1;
LblJogador.Text = "JOGADOR 1";
}
}
Resultado();
}

private void Btn8_Click(object sender, EventArgs e)
{
if (ganhador == 0 && Btn8.Text == "")
{
if (this.Jogador == 1)
{
Btn8.ForeColor = Color.CornflowerBlue;
Btn8.Text = "X";
this.Jogador = 2;
LblJogador.Text = "";

}
else if (Jogador == 2)
{
Btn8.ForeColor = Color.ForestGreen;
Btn8.Text = "O";
this.Jogador = 1;
LblJogador.Text = "";
}
}
Resultado();
}

private void Btn9_Click(object sender, EventArgs e)
{
if (ganhador == 0 && Btn9.Text == "")
{
if (this.Jogador == 1)
{
Btn9.ForeColor = Color.CornflowerBlue;
Btn9.Text = "X";
this.Jogador = 2;
LblJogador.Text = "";

}
else if (Jogador2 == 2)
{
Btn9.ForeColor = Color.ForestGreen;
Btn9.Text = "O";
this.Jogador = 1;
LblJogador.Text = "";
}
}
Resultado();
}

private void BtnNovo_Click(object sender, EventArgs e)
{
Btn1.Text = "";
Btn1.Enabled = true;
Btn2.Text = "";
Btn2.Enabled = true;
Btn3.Text = "";
Btn3.Enabled = true;
Btn4.Text = "";
Btn4.Enabled = true;
Btn5.Text = "";
Btn5.Enabled = true;
Btn6.Text = "";
Btn6.Enabled = true;
Btn7.Text = "";
Btn7.Enabled = true;
Btn8.Text = "";
Btn8.Enabled = true;
Btn9.Text = "";
Btn9.Enabled = true;
LblJogador.Text = "";
TxtVisor.Clear();
Jogador = 1;
ganhador = 0;
}
private void Resultado()
{ //ganhou
{
if (Btn1.Text != "" &&
Btn2.Text != "" &&
Btn3.Text != "" &&
Btn4.Text != "" &&
Btn5.Text != "" &&
Btn6.Text != "" &&
Btn7.Text != "" &&
Btn8.Text != "" &&
Btn9.Text != "")

ganhador = 3;
}
//coluna horizontal
if (Btn1.Text == "X" && Btn2.Text == "X" && Btn3.Text == "X")
{
Btn1.Enabled = true;
Btn2.Enabled = true;
Btn3.Enabled = true;
Btn4.Enabled = false;
Btn5.Enabled = false;
Btn6.Enabled = false;
Btn7.Enabled = false;
Btn8.Enabled = false;
Btn9.Enabled = false;
this.ganhador = 1;




} //coluna horizontal
else if (Btn4.Text == "X" && Btn5.Text == "X" && Btn6.Text == "X")
{
Btn4.Enabled = true;
Btn5.Enabled = true;
Btn6.Enabled = true;
Btn1.Enabled = false;
Btn2.Enabled = false;
Btn3.Enabled = false;
Btn7.Enabled = false;
Btn8.Enabled = false;
Btn9.Enabled = false;
this.ganhador = 1;



} //coluna horizontal
else if (Btn7.Text == "X" && Btn8.Text == "X" && Btn9.Text == "X")
{
Btn7.Enabled = true;
Btn7.Enabled = true;
Btn8.Enabled = true;
Btn9.Enabled = true;
Btn1.Enabled = false;
Btn2.Enabled = false;
Btn3.Enabled = false;
Btn4.Enabled = false;
Btn5.Enabled = false;
Btn6.Enabled = false;
this.ganhador = 1;



} //coluna vertical
else if (Btn1.Text == "X" && Btn4.Text == "X" && Btn7.Text == "X")
{
Btn1.Enabled = true;
Btn4.Enabled = true;
Btn7.Enabled = true;
Btn2.Enabled = false;
Btn3.Enabled = false;
Btn5.Enabled = false;
Btn6.Enabled = false;
Btn8.Enabled = false;
Btn9.Enabled = false;
this.ganhador = 1;




} //coluna vertical
else if (Btn2.Text == "X" && Btn5.Text == "X" && Btn8.Text == "X")
{
Btn2.Enabled = true;
Btn5.Enabled = true;
Btn8.Enabled = true;
Btn1.Enabled = false;
Btn3.Enabled = false;
Btn4.Enabled = false;
Btn6.Enabled = false;
Btn7.Enabled = false;
Btn9.Enabled = false;
this.ganhador = 1;


} //coluna vertical
else if (Btn3.Text == "X" && Btn6.Text == "X" && Btn9.Text == "X")
{
Btn3.Enabled = true;
Btn6.Enabled = true;
Btn9.Enabled = true;
Btn1.Enabled = false;
Btn2.Enabled = false;
Btn4.Enabled = false;
Btn5.Enabled = false;
Btn7.Enabled = false;
Btn8.Enabled = false;
this.ganhador = 1;



} //coluna diagonal
else if (Btn1.Text == "X" && Btn5.Text == "X" && Btn9.Text == "X")
{
Btn1.Enabled = true;
Btn5.Enabled = true;
Btn9.Enabled = true;
Btn2.Enabled = false;
Btn3.Enabled = false;
Btn4.Enabled = false;
Btn6.Enabled = false;
Btn7.Enabled = false;
Btn8.Enabled = false;
this.ganhador = 1;


} //coluna diagonal
else if (Btn3.Text == "X" && Btn5.Text == "X" && Btn7.Text == "X")
{
Btn7.Enabled = true;
Btn5.Enabled = true;
Btn3.Enabled = true;
Btn1.Enabled = false;
Btn2.Enabled = false;
Btn4.Enabled = false;
Btn6.Enabled = false;
Btn8.Enabled = false;
Btn9.Enabled = false;
this.ganhador = 1;


}
//perdeu
//coluna horizontal
if (Btn1.Text == "O" && Btn2.Text == "O" && Btn3.Text == "O")
{
Btn1.Enabled = true;
Btn2.Enabled = true;
Btn3.Enabled = true;
Btn4.Enabled = false;
Btn5.Enabled = false;
Btn6.Enabled = false;
Btn7.Enabled = false;
Btn8.Enabled = false;
Btn9.Enabled = false;
this.ganhador = 2;

} //coluna horizontal
else if (Btn4.Text == "O" && Btn5.Text == "O" && Btn6.Text == "O")
{
Btn4.Enabled = true;
Btn5.Enabled = true;
Btn6.Enabled = true;
Btn1.Enabled = false;
Btn2.Enabled = false;
Btn3.Enabled = false;
Btn7.Enabled = false;
Btn8.Enabled = false;
Btn9.Enabled = false;
this.ganhador = 2;

} //coluna horizontal
else if (Btn7.Text == "O" && Btn8.Text == "O" && Btn9.Text == "O")
{
Btn7.Enabled = true;
Btn8.Enabled = true;
Btn9.Enabled = true;
Btn1.Enabled = false;
Btn2.Enabled = false;
Btn3.Enabled = false;
Btn4.Enabled = false;
Btn5.Enabled = false;
Btn6.Enabled = false;
this.ganhador = 2;

} //coluna vertical
else if (Btn1.Text == "O" && Btn4.Text == "O" && Btn7.Text == "O")
{
Btn1.Enabled = true;
Btn4.Enabled = true;
Btn7.Enabled = true;
Btn2.Enabled = false;
Btn3.Enabled = false;
Btn5.Enabled = false;
Btn6.Enabled = false;
Btn8.Enabled = false;
Btn9.Enabled = false;
this.ganhador = 2;

} //coluna vertical
else if (Btn2.Text == "O" && Btn5.Text == "O" && Btn8.Text == "O")
{
Btn2.Enabled = true;
Btn5.Enabled = true;
Btn8.Enabled = true;
Btn1.Enabled = false;
Btn3.Enabled = false;
Btn4.Enabled = false;
Btn6.Enabled = false;
Btn7.Enabled = false;
Btn9.Enabled = false;
this.ganhador = 2;
} //coluna vertical
else if (Btn3.Text == "O" && Btn6.Text == "O" && Btn9.Text == "O")
{
Btn3.Enabled = true;
Btn6.Enabled = true;
Btn9.Enabled = true;
Btn1.Enabled = false;
Btn2.Enabled = false;
Btn4.Enabled = false;
Btn5.Enabled = false;
Btn7.Enabled = false;
Btn8.Enabled = false;
this.ganhador = 2;
} //coluna diagonal
else if (Btn1.Text == "O" && Btn5.Text == "O" && Btn9.Text == "O")
{
Btn1.Enabled = true;
Btn5.Enabled = true;
Btn9.Enabled = true;
Btn2.Enabled = false;
Btn3.Enabled = false;
Btn4.Enabled = false;
Btn6.Enabled = false;
Btn7.Enabled = false;
Btn8.Enabled = false;
this.ganhador = 2;

} //coluna diagonal
else if (Btn3.Text == "O" && Btn5.Text == "O" && Btn7.Text == "O")
{
Btn7.Enabled = true;
Btn5.Enabled = true;
Btn3.Enabled = true;
Btn1.Enabled = false;
Btn2.Enabled = false;
Btn4.Enabled = false;
Btn6.Enabled = false;
Btn8.Enabled = false;
Btn9.Enabled = false;

this.ganhador = 2;
}
if (this.ganhador != 0 && this.ganhador != 3)
TxtVisor.Text = "O JOGADOR " + ganhador + " GANHOU!";

if (this.ganhador == 3)
TxtVisor.Text = "NÃO HOUVE GANHADOR!";

if (ganhador == 0)
LblJogador.Text = "JOGADOR " + this.Jogador;
else
LblJogador.Text = "FIM DE JOGO!";
}
}
}

Link para o comentário
Compartilhar em outros sites

Cara pensa comigo do jeito que o mortts falou esta corretissimo mas tenta de outro jeito olha vamos pensar um pouco.

Um form é um objeto, se você for criar uma instancia exemplo "frm1 instancia = new frm1();" assim você esta criando uma

instancia de um objeto essa instancia contem a mesma coisa que o form tem então você pode atribuir valor as variaveis

por ele, olha se você criar uma instancia em um form e depois criar outra instancia no outro form elas vão ter o mesmo valor

mas vão ser separada.

Exemplo - frm1 instancia1 = new frm1(); <- este esta no form1. frm1 instancia2 = new frm1(); <- este esta no form2

olha você ceriou duas instancias ambas contem o mesmo conteudo mas separado, é como se você criasse duas classes e

copiasse o mesmo codigo entende, bom ja expliquei bastante vamos aplicar.

Bom vou citar como form1 o cadastro eo form2 o jogo, no form2 você cria duas variaveis ex:

//Aqui você esta no form2

Public string nome1;

Public string nome2;

e no form1 você cria uma instancia do tipo do form2 pra você pode acessa as variaveis ex:

form2 dados = new form2();

dados.nome1 = txtNome1.Text;

dados.nome2 = txtNome2.Text;

olha tem um negocio importante c você for chamar o form2 assim "new form2().Show();" desse modo não vai da erro

mas não vai setar o valor as variaveis você ja sabe porq faz um esforço ai beleza, você tem q chamar desse modo

usando a instancia criada q ja esta comos valores das txt.

dados.Show();

esta pronto as variaveis tem q ser publica no form e do tipo public e isso

dados.nome1 = txtNome1.Text;

dados.nome2 = txtNome2.Text;

dados.Show();

você pode colocar no botao. É isso que posso passar pra você c quiser corrigir ou questionar vai em frente isso é bom

valw cara e espero q te ajude, ha e estuda bastante conceito sobre orientação a objetos ira te ajudar muito, tem

varios videos no youtube do willian chimura ele é um genio em orientação a objetos e muito mais procura po chimufox

que você vai achar o canal dele bom espero ter ajudado t++ cara Falou..

Link para o comentário
Compartilhar em outros sites

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...