【问题标题】:How do I transfer the value of a label to another label that currently is in another form using visual basic?如何使用 Visual Basic 将标签的值传输到当前处于另一种形式的另一个标签?
【发布时间】:2013-11-25 04:44:03
【问题描述】:

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

【问题讨论】:

  • new fMain(); 你不想要一个新的实例;你想要你现有的实例。
  • 我想你会得到一些提示here

标签: c#


【解决方案1】:

您正在做的是将值放入不公开的字符串中。要么将字符串公开并从其他形式访问它,要么您可以创建一个类并将所有此类变量放在该类中,然后在需要时从那里访问它们。

【讨论】:

    【解决方案2】:

    您可能会为FMain2 创建一个Custom Event,它会反映到您的FMain LabScore.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;
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多