【问题标题】:Taking a Timer Value From One Form to Another将计时器值从一种形式转移到另一种形式
【发布时间】:2016-05-24 04:34:47
【问题描述】:

所以我正在用 c# 制作一个小型视频游戏,评分系统以秒为单位。当玩家完成关卡时,我想获取 Timers 值并将时间存储在其他玩家时间的数据库中。

private void timer2_Tick(object sender, EventArgs e)
{
    I++;
    lblView.Text = I.ToString() + " Seconds";
}

这是我的计时器的代码。

我不确定如何使 I 变量,我已将计时器用作全局变量。抱歉,如果这看起来含糊不清,我无法描述我想要发生的事情。

【问题讨论】:

  • 将它作为static variable 放在某个static 类中。并从您想要的任何地方访问它..
  • @usmanlqbal 我试过了,但对我没有用:/ namespace WindowsFormsApplication2 { public static class Global { public static int i = 0; } }
  • 您能否分享您为static 类编写的代码以及您如何访问该类中的静态变量.. 并解释您在访问该static 变量时遇到了什么错误。 ?
  • @usmanlqbal public partial class Game : Form { private SoundPlayer _soundPlayer;布尔右,左;布尔跳转;诠释 G = 30;诠释力量;公共游戏(){初始化组件(); } private void timer2_Tick(Global sender, EventArgs e) { i++; lblView.Text = i.ToString() + "秒"; }
  • @usmanlqbal 新静态类命名空间中的静态变量 WindowsFormsApplication2 { public static class Global { public static int i = 0; } }

标签: c# timer global-variables


【解决方案1】:

静态类语法

public static class Helper { public static int I = 0; }

访问static 变量。

private void timer2_Tick(object sender, EventArgs e)
{
    Helper.I++;//here accessing static variable
    lblView.Text = Helper.I.ToString() + " Seconds";
}

【讨论】:

  • 嘿,谢谢你的帮助,我真的很感激,现在可以工作了:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-23
  • 2015-06-06
  • 2010-10-06
相关资源
最近更新 更多