2013-11-25 44 views
2

增加每个项目我皮卡如何使用Visual Basic将标签的值传递给另一个表单中的另一个标签?

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; 

     } 

137点我的分数标签,我想我的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

'new fMain();'你不想要一个新的实例;你想要你现有的实例。 – SLaks

+0

我想你会得到一些提示[这里](http://stackoverflow.com/questions/3062575/passing-values-between-forms-winforms) – ViSu

回答

1

你正在做的是把价值在一个字符串,它是不公开的。将字符串公开并从其他窗体访问它,或者创建一个类并将所有这些变量放在该类中,然后在需要时从那里访问它们。

0

您可能会在FMain2中创建一个Custom Event,这将反映回您的FMainLabScore.Text

在您的FMain2中,创建自定义事件。假设我们用score作为参数创建Custom Event。我们将具备以下条件:

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; 
    } 
    } 
} 
相关问题