问题
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?*/)
}
}
回答1:
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");
}
}
This code will start at 19.png
and go to 18.png
, 17.png
and so on, until it reaches 0.png
.
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.
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.
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");
Doing that, it will go from 20.png
to 1.png
. As an alternative you can change the verification to :
if (ImageNumber == 1)
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).
Points of interest:
using
int
in modern CPUs is actually faster because it is either a word o half a word. There is no need for usingbyte
in this scenario.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)
will give a number that can be in the reange[0, n)
, I mean that it is not inclusive forn
. In other words: The result can be 0, but notn
. SoRandom.Next(20)
will never return20
.As commented, using
-=
on a byte isunchecked
by default. So it goes from 0 to 255. usingchecked{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.
来源:https://stackoverflow.com/questions/20594245/having-a-constant-decrease-of-a-number-when-pushing-a-button-in-c-sharp