每个项目增加我的分数标签137分
fMain
//HUD Score
public int Score()
{
labScore.Text = Convert.ToString(score);
labScore.Text = "00000" + score;
if (score >= 10)
labScore.Text = "0000" + score;
if (score >= 100)
labScore.Text = "000" + score;
if (score >= 1000)
labScore.Text = "00" + score;
return score;
}
我希望我的labScore2.Text与labScore.text相同。但它们有不同的形式。
fMain2
public void btnIntroducir_Click(object sender, EventArgs e)
{
fMain f = new fMain();
f.Score();
try
{
string n = txtNombre.Text;
int s = int32.Parse(labScore2.Text);
lista[indice] = new Koby(n, s);
indice++;
muestraLista(ref lstJugadores);
txtNombre.Clear();
txtNombre.Enabled = false;
}
答案 0 :(得分:1)
您正在做的是将值放在不公开的字符串中。要么将字符串设置为public并从其他表单访问它,要么您可以创建一个类并将所有这些变量放在该类中,然后在需要时从那里访问它们。
答案 1 :(得分:0)
您可以在Custom Event
中为FMain2
创建一个反映回FMain
LabScore.Text
的{{1}}。
在FMain2
中,创建自定义事件。假设我们使用Custom Event
作为参数创建score
。我们将有以下内容:
public partial class FMain2 : Form
{
public delegate void ScoreChangedEvent(String score); // Event Handler Delegate
public event ScoreChangedEvent ScoreChanged; // Event Handler to subscribe to the Delegate
public FMain2()
{
InitializeComponent();
}
}
在您的FMain
中,您可以初始化事件并在触发事件时进行必要的更改,例如:
private void btnChangeScore_Click(object sender, EventArgs e)
{
FormMain2 FMain2 = new FormMain2();
FMain2.ScoreChanged += new FMain2.ScoreChangedEvent(FMain2_ScoreChanged);
FMain2.Show();
}
void FMain2_ScoreChanged(string score)
{
labScore.Text = score; // This will receive the changes from FMain2 LabScore2.Text
}
然后返回FMain2
,添加以下内容:
public void btnIntroducir_Click(object sender, EventArgs e)
{
try
{
string n = txtNombre.Text;
int s = int32.Parse(labScore2.Text);
ScoreChanged(labScore2.Text); // This will reflect back to FMain labScore.Text
lista[indice] = new Koby(n, s);
indice++;
muestraLista(ref lstJugadores);
txtNombre.Clear();
txtNombre.Enabled = false;
}
}
因此,您FMain2
的最终代码将是:
public partial class FMain2 : Form
{
public delegate void ScoreChangedEvent(String score); // Event Handler Delegate
public event ScoreChangedEvent ScoreChanged; // Event Handler to subscribe to the Delegate
public FMain2()
{
InitializeComponent();
}
public void btnIntroducir_Click(object sender, EventArgs e)
{
try
{
string n = txtNombre.Text;
int s = int32.Parse(labScore2.Text);
ScoreChanged(labScore2.Text); // This will reflect back to FMain labScore.Text
lista[indice] = new Koby(n, s);
indice++;
muestraLista(ref lstJugadores);
txtNombre.Clear();
txtNombre.Enabled = false;
}
}
}