Pett - 2 months ago 6

C# Question

so basically, I ran into a problem, I can't figure out.

I have this code:

`if ((v - e.Delta) <= 0) vScrollBar1.Value = vScrollBar1.Minimum;`

if ((v - e.Delta) >= vScrollBar1.Maximum) vScrollBar1.Value = vScrollBar1.Maximum;

else vScrollBar1.Value -= e.Delta;

Even though the first if statement is true and runs its code, it runs the "else" code anyway.

Example: v = 0, e.Delta = 120 so the first if statement is true and works, but I don't know why it runs the else statement anyway, it doesn't run when the second if statement is true.

Thank for answers/suggestions!

Answer

Those two `if`

s are independent.

```
if ((v - e.Delta) <= 0) vScrollBar1.Value = vScrollBar1.Minimum;
if ((v - e.Delta) >= vScrollBar1.Maximum) vScrollBar1.Value = vScrollBar1.Maximum;
else vScrollBar1.Value -= e.Delta;
```

You should add an `else`

before your second `if`

. Like this:

```
if ((v - e.Delta) <= 0) vScrollBar1.Value = vScrollBar1.Minimum;
else if ((v - e.Delta) >= vScrollBar1.Maximum) vScrollBar1.Value = vScrollBar1.Maximum;
else vScrollBar1.Value -= e.Delta;
```