
[英](Unity, 2D, C#) How can I make the player let go of a button before needing to press it again?
[英]Is there a way to make a button press recursive in C# (press itself again)? [duplicate]
我想让一个按钮运行一些代码,然后在 Windows Forms 中再次按下它自己。
当我调用按钮本身时,我收到错误:
System.StackOverflowException
HResult=0x800703E9
Source=<Cannot evaluate the exception source>
StackTrace:
<Cannot evaluate the exception stack trace>
我想制作一个资源监控程序来熟悉C# 。 现在我被困在字符串处理和显示不断变化的字符串上。 我使用随机生成的数字作为占位符,我想永久显示和更改它,模仿真实的数据拉取。
我的代码:
public void Start_Click(object sender, EventArgs e){
var usage =new CpuUsage(); //placeholder class for getting the CPU use data
usage.setCPU(); //gets a random number
this.CPU.Text = usage.cpuUsage; //show the usage data on a textbox
Start_Click(null, EventArgs.Empty); //call this button again here
}
我想要得到的东西看起来像:
我认为您正在寻找的是Timer
控件。 您可以将其设置为以某个时间间隔运行,并定义以该时间间隔运行的代码。 如果您只想在按下按钮时启动它,您还可以控制定时器的启动和停止。
例如,在表单上放置一个Timer
控件并尝试以下代码:
private void Form1_Load(object sender, EventArgs e)
{
// Set the interval to how often you want it to execute
timer1.Interval = (int)TimeSpan.FromSeconds(1).TotalMilliseconds;
// Set a method to run on every interval
timer1.Tick += Timer1_Tick;
}
public void Start_Click(object sender, EventArgs e)
{
// start or stop the timer
timer1.Enabled = !timer1.Enabled;
// Above we are just flipping the 'Enabled' property, but
// you could also call timer1.Start() (which is the same as
// setting 'Enabled = true') or timer1.Stop() (which is
// the same as setting 'Enabled = false')
}
// Put code in this method that should execute when the timer interval is reached
private void Timer1_Tick(object sender, EventArgs e)
{
var usage = new CpuUsage(); //placeholder class for getting the CPU use data
usage.setCPU(); //gets a random number
this.CPU.Text = usage.cpuUsage; //show the usage data on a textbox
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.