2013-11-25 46 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; 
    } 
    } 
} 
相關問題