Problem with KeyUp and arrows

Sanel Pandzic 41 Reputation points
2022-01-20T21:17:38.307+00:00

I have one problem. I want to use the right and left arrows to scroll through the images, and I did it perfectly. The right arrow goes forward and the left arrow goes backward. And when I press the right arrow, then it's like pressing the left arrow, and again when I press the right arrow again, only then do the pictures scroll, and so every time, so I have to double-click the right arrow to scroll through the pictures, so does the left. Mostly you will notice that when I click the arrow, that "button" is in blue which means like I pressed it but I didn't.
Here is the video: 668392526

        private void nxt_KeyUp(object sender, KeyEventArgs e)///Next image
        {
            if(e.KeyCode == Keys.Right)
            {
                try
                {

                    next++;
                    slike.Image = listaSlika.Images[next];


                }
                catch (Exception ex)
                {

                    MessageBox.Show("Nema vise opcija za naprijed!");
                }
                if (next > 3)
                {
                    next = 3;
                }
            }
        }

        private void prev_KeyUp(object sender, KeyEventArgs e)//Previous image
        {
            if (e.KeyCode == Keys.Left)
            {
                try
                {
                    next--;
                    slike.Image = listaSlika.Images[next];

                }
                catch (Exception ex)
                {
                    MessageBox.Show("Ne mozes nazad!");
                }
                if (next < 0)
                {
                    next = -1;
                }
            }
        }
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,164 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Sanel Pandzic 41 Reputation points
    2022-01-22T21:45:42.85+00:00

    @Jack J Jun @Castorix31 @Rool0507

    Now I put the code under the switch, and I merged these two buttons into one event and still have
    I still have a problem with the button, when I press the left arrow the first time, it looks like I clicked the right button, but I didn't, and the second time I press the left click, then it's fine, but that's how it goes. So I just have one more problem left, you can see it better in the video.

    private void nxt_KeyUp(object sender, KeyEventArgs e)  
                    {  
                        switch(e.KeyCode)  
                        {   
                         case Keys.Left:  
                            try  
                             {  
                        
                        next--;  
                        slike.Image = listaSlika.Images[next];  
                  
                    }  
                    catch (Exception ex)  
                    {  
                        MessageBox.Show("Ne mozes nazad!");  
                    }  
                    if (next < 0)  
                    {  
                        next = -1;  
                    }  
                    break;  
                        case Keys.Right:  
                            try  
                    {  
          
                        next++;  
                        slike.Image = listaSlika.Images[next];  
               
          
                    }  
                    catch (Exception ex)  
                    {  
                        MessageBox.Show("Nema vise opcija za naprijed!");  
                    }  
                    if (next > 3)  
                    {  
                        next = 3;  
                    }  
                    break;  
                }  
            }