Domanda

I think that my question is pretty self explanatory, that's my problem. I'm relatively new to c# and i have some experience in c++, but never handled these kind of problems. When i compile my code it says that i have attempted to divide by zero after 6 clicks. Here's my code so far:

public partial class Form3 : Form {
    int btnClick=0;
    int brPicsArray = 7; 
    public Form3() {
        InitializeComponent();
    }

    private void Form3_Load(object sender, EventArgs e) {

    }

    private void button1_Click(object sender, EventArgs e) {
        btnClick++;
        brPicsArray = btnClick % brPicsArray;
        switch (brPicsArray) {
            case 1: pictureBox1.Image = Image.FromFile(@"C:\Users\Korice\Documents\Visual Studio 2012\Projects\.....\form3pic1.jpg"); 
                break;
            case 2: pictureBox1.Image = Image.FromFile(@"C:\Users\Korice\Documents\Visual Studio 2012\Projects\.....\form3pic2.jpg");
                break;
            case 3: pictureBox1.Image = Image.FromFile(@"C:\Users\Korice\Documents\Visual Studio 2012\Projects\.....\form3pic3.jpg"); 
                break;
            case 4: pictureBox1.Image = Image.FromFile(@"C:\Users\Korice\Documents\Visual Studio 2012\Projects\.....\form3pic4.jpg");
                break;
            case 5: pictureBox1.Image = Image.FromFile(@"C:\Users\Korice\Documents\Visual Studio 2012\Projects\.....\form3pic5.jpg");
                break;
            case 6: pictureBox1.Image = Image.FromFile(@"C:\Users\Korice\Documents\Visual Studio 2012\Projects\.....\form3pic6.jpg"); 
                break;
            case 7: pictureBox1.Image = Image.FromFile(@"C:\Users\Korice\Documents\Visual Studio 2012\Projects\.....\form3pic7.jpg"); 
                break;
È stato utile?

Soluzione

Your problem is that you're setting brPicsArray to btnClick % brPicsArray, which will eventually equal 0.

Instead, you should use something like:

switch (brPicsArray % btnClick) {
    case 0: 
    ...
}

Note: We're starting with a case index of 0, which is more standard, and we're not changing the value of brPicsArray.


I would encourage you to store your image names in an array instead and use the array's Count property instead of a separately stored variable. In that case, you'd end up with something more like:

public partial class Form3 : Form {
    string[] imageFilenames = new string[] {
        "C:\Users\Korice\Documents\Visual Studio 2012\Projects\.....\form3pic1.jpg",
        "C:\Users\Korice\Documents\Visual Studio 2012\Projects\.....\form3pic2.jpg",
        "C:\Users\Korice\Documents\Visual Studio 2012\Projects\.....\form3pic3.jpg",
        "C:\Users\Korice\Documents\Visual Studio 2012\Projects\.....\form3pic4.jpg",
        "C:\Users\Korice\Documents\Visual Studio 2012\Projects\.....\form3pic5.jpg",
    };
    int btnClick=0;

    private void button1_Click(object sender, EventArgs e) {
        btnClick++;
        pictureBox1.Image = Image.FromFile(imageFilenames[imageFilenames.Count % btnClick]);
    }
}

Altri suggerimenti

Modulo (%) is a divide function so when brPicsArray becomes 0 (when the divide have no remains) you will get a divide by zero error.

You are getting the error because. If btnClick = 7 then you are doing

btnClick % brPicsArray.

The same as

7 % 7

Which both return 0;

A very small problem that you initialized brPicsArray value and modified it and that results in 0 and hence divide by zero exception, a small solution

 private void button1_Click(object sender, EventArgs e)
    {
        btnClick++;
        int switchPicsValue = btnClick % brPicsArray;
        switch (switchPicsValue)

Just initialize a new variable for switch

I don't think you need a switch here because your pictues are labelled with a number, and you are calculating a number to get the image anyway, so calculate the number, and append it to the file name, but make sure your number never exceeds the range (i.e. 1 - 7)

Example:

private_void button1_Click(object sender, EventArgs e)
{
    btnClick++;
    if(btnClick > 7) btnClick = 1; // or 7 if you don't want to loop
    pictureBox1.Image = Image.FromFile(String.Format(@"C:\Users\Korice\Documents\Visual Studio 2012\Projects\.....\form3pic{0}.jpg", btnClick));
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top