按钮启用无法正常工作

编程入门 行业动态 更新时间:2024-10-26 23:36:46
本文介绍了按钮启用无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在开发一个迷你点击器游戏,它没有什么大问题,但是我在启用按钮方面遇到了问题,但是我可以将其禁用.我仍在学习,我认为可以问这样的愚蠢问题.:D

I am working on a mini clicker game, it is not anything big, but i am having a problem with enabling my button, but i can disable it. I am still learning and i think it is okay to ask stupid questions like this. :D

这是我的代码:

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Diamond_Clicker { public partial class Form1 : Form { private int clicks = 0; private int counter = 1; public Form1() { InitializeComponent(); } private void myDiamond_MouseUp(object sender, MouseEventArgs e) { myDiamond.Image = Image.FromFile("C:\\Matej Dodevski\\Semos\\C#\\Diamond Clicker\\diamond.png"); } private void myDiamond_MouseDown(object sender, MouseEventArgs e) { myDiamond.Image = Image.FromFile("C:\\Matej Dodevski\\Semos\\C#\\Diamond Clicker\\diamondMouseUp.png"); clicks++; DiamondsScore.Text = "Diamonds: " + clicks.ToString(); } private void timer1_Tick(object sender, EventArgs e) { clicks++; } private void timer1_Tick_1(object sender, EventArgs e) { counter++; clicks = clicks + 1; DiamondsScore.Text = "Diamonds: " + clicks.ToString(); } private void button1_Click(object sender, EventArgs e) { clicks = clicks - 50; DiamondsScore.Text = "Diamonds: " + clicks.ToString(); timer1.Enabled = true; } private void Form1_Load(object sender, EventArgs e) { if (clicks > 5) { button1.Enabled = true; } else button1.Enabled = false; } } }

推荐答案

加载事件旨在执行一次,而这恰好是在屏幕上显示表单之前.通常,此事件是您一次进行某种初始化的地方.

The Load Event is intended to get executed one time, and that's just before the form is displayed on the screen. Usually this event is where you would do some kind of one time initialization.

您需要做的是将该代码放入函数中

What you need to do instead is put that code into a function:

private void UpdateButton() { if (clicks > 5) button1.Enabled = true; else button1.Enabled = false; // This function can be reduced to one line of code: // button1.Enabled = clicks > 5; }

然后,您需要在button1_click函数,timer1_tick函数,mousedown函数和timer1_tick_1函数的末尾调用此函数.基本上,可以将clicks变量更改为任何函数.

Then you need to call this function at the end of your button1_click function, timer1_tick function, mousedown function and your timer1_tick_1 functions. Basically, into any function where the clicks variable can change.

更多推荐

按钮启用无法正常工作

本文发布于:2023-11-27 05:29:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636789.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:无法正常   按钮   工作

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!