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

C# 如何使用visual basic将标签的值传递到当前处于另一种形式的另一个标签?,c#,C#,增加我的分数标签每件物品137分 fMain //HUD Score public int Score() { labScore.Text = Convert.ToString(score); labScore.Text = "00000" + score; if (score >= 10) labScore.Text = "0000" + score

增加我的分数标签每件物品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;
        }

您所做的是将值放入一个非公共的字符串中。要么公开字符串并从另一个表单访问它,要么创建一个类并将所有此类变量放入该类中,然后在需要时从该类访问它们

您可能可以为
FMain2
中的
创建一个
自定义事件,该事件将反映回您的
FMain
LabScore.Text

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;
    }
 }
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 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;
    }
 }
因此,您的
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;
    }
  }
}

newfmain()您不需要新实例;你想要你现有的实例。我想你会得到一些提示