在 C# 中按下按钮时数字不断减少 [英] having a constant decrease of a number when pushing a button in C#

查看:50
本文介绍了在 C# 中按下按钮时数字不断减少的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是初学者,所以让我轻松一点.我想在按下按钮时按名称 (0-20) 的顺序显示一组图像.我设法用一个随机数 (0-20) 做到了,但如果我想从 20 减少到 19 到 18 等等,我该怎么做?

I am a begginer, so make it easy for me. I want to display an collection of images at in order of their name (0-20) when I press a button. I managed to do it with a random number (0-20), but if I want to have a decrease from 20 to 19 to 18 and so on, how would I do that?

public partial class Form1 : Form
{

    //Random r = new Random();


    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        string path = @"C:\Users\Ole-Jeger\Documents\Visual Studio 2013\Projects\Test_Spill1\Test_Spill1\Pictures\";
        //Heathbar.Image = Image.FromFile(path + r.Next(20).ToString() + ".png");
        Heathbar.Image = Image.FromFile(path + /*what to put here?*/)
    }
}

推荐答案

定义一个类成员字段 ImageNumber 保存当前的编号:

Define a class member field ImageNumber that hold the the number of the current:

public partial class Form1 : Form
{
    private const string path = 
        @"C:\Users\Ole-Jeger\Documents\Visual Studio 2013\Projects\Test_Spill1\Test_Spill1\Pictures\";

    private const int INT_ImageCount = 20;
    private int ImageNumber = INT_ImageCount;

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (ImageNumber > 0)
        {
            ImageNumber -= 1;
            // How about disabling the button?
            if (ImageNumber == 0)
            {
                button1.Enabled = false;
            }
        }
        /*else
        {
            // Bad boys, Bad boys,
            // whatcha gonna do?
            // whatcha gonna do?
            // whatcha gonna do when the number is nil?

            // Note: if you disabled the button, this shoudln't be a problem
        }*/
        Heathbar.Image = Image.FromFile(path + ImageNumber.ToString() + ".png");
    }
}

这段代码会从19.png开始,依次往18.png17.png,直到到达<代码>0.png.

This code will start at 19.png and go to 18.png, 17.png and so on, until it reaches 0.png.

上面的代码不输出20.png,因为它在使用之前减小了值.所以第一次迭代是20,然后减少到19,然后使用.

The code above doesn't output 20.png because it decreases the value before using it. So the first iteration it is 20, then it is decreased to 19, and then it is used.

我使用了一个名为 INT_ImageCount 的常量设置为 20,这反映了从 0.png0.png 有 20 张图像的事实代码>19.png.根据需要调整值.

I have used a constant named INT_ImageCount set to 20, this reflects the fact that there are 20 images from 0.png to 19.png. Tweak the value as needed.

注意:如果您没有 0.png 图像,您可能希望在使用时将 1 添加到变量中:

Note: if you don't have a 0.png image you may want to add 1 to the variable when you use it:

Heathbar.Image = Image.FromFile(path + (ImageNumber + 1).ToString() + ".png");

这样做,它将从 20.png 变成 1.png.作为替代方案,您可以将验证更改为:

Doing that, it will go from 20.png to 1.png. As an alternative you can change the verification to :

if (ImageNumber == 1)

这将导致代码从 19.png 变为 1.png(改变 cosntant 以适应这一点,尽管常量将不再代表数字图像,但最后一个图像的值...在这种情况下,您可能希望将其称为 INT_MaxImage).

This will cause the code to go from 19.png to 1.png (change the cosntant to adapt to this, although will the constant no longer represent the number of images, but the value of the last image... you may want to call it INT_MaxImage in that case).

兴趣点:

  1. 在现代 CPU 中使用 int 实际上更快,因为它要么是一个词要么是半个词.在这种情况下不需要使用 byte.

  1. using int in modern CPUs is actually faster because it is either a word o half a word. There is no need for using byte in this scenario.

我已将路径设为常量,实际上您可能希望将其设为 readonly 字段,该字段从可执行文件的路径中获取其值(或从配置或类似来源).但那是另一回事了,以后再说.

I have made the path a constant, in practice you may want to make it a readonly field that gets its value from the path of the executable (or read it from configuration or a similar source). But that is another story, and shall be told another time.

Random.Next(n) 将给出一个可以在 [0, n) 范围内的数字,我的意思是它不包括n.换句话说:结果可以是 0,但不能是 n.所以 Random.Next(20) 永远不会返回 20.

Random.Next(n) will give a number that can be in the reange [0, n), I mean that it is not inclusive for n. In other words: The result can be 0, but not n. So Random.Next(20) will never return 20.

正如所评论的,默认情况下,在字节上使用 -=unchecked.所以它从 0 到 255.使用 checked{num -= 1} 将导致它抛出异常.这是因为字节不能存储负值.

As commented, using -= on a byte is unchecked by default. So it goes from 0 to 255. using checked{num -= 1} will cause it to throw an exception. This is because byte cannot store negative values.

您可能有兴趣预加载图像并将它们放在一个数组中,这样可以更快地在它们之间切换,同时避免两次加载相同的图像.

You may be interested in preloading your images and have them in an array, so it can switch among them faster, also avoid loading the same image twice.

这篇关于在 C# 中按下按钮时数字不断减少的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆