如何使用visual basic将标签的值传输到另一个当前处于另一种形式的标签?

时间:2013-11-25 04:44:03

标签: c#

每个项目增加我的分数标签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;
        }

2 个答案:

答案 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;
    }
  }
}