KeyDown and MouseWheel don t working in c#

LEVY XAVIER MOUZINHO DE ABREU 0 Reputation points
2023-05-24T04:20:59.72+00:00

KeyDown and MouseWheel don t working in c#, the function does not fire and nothing happens.

I first tried:

add the ImageForm_KeyDown property of the keydown event through the graphical interface, without success.

Then I tried adding this.KeyDown += ImageForm_KeyDown. unsuccessfully.

Then I tried adding this.KeyDown += new KeyEventHandler(ImageForm_KeyDown), without success.

Then I tried following this tutorial https://learn.microsoft.com/pt-br/dotnet/api/system.windows.forms.control.isinputkey?view=windowsdesktop-7.0, also without success.

I only got the mousewheel.

Follow source code:

using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace app_manga12
{
    public partial class Form1 : Form
    {


        int Index;
        List<string> Caminho;
        string caminho2;
        
        public Form1(string Caminho2, List<string> caminho, int index)
        {


            InitializeComponent();
            Index = index;
            Caminho = caminho;
            caminho2 = Caminho2;
            KeyPreview = true;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //flowLayoutPanel1.BackgroundImage = Image.FromFile(@Caminho[Index]);
            BackgroundImage = Image.FromFile(@caminho2);
            BackgroundImageLayout = ImageLayout.Center;
          //  SizeMode = PictureBoxSizeMode.Zoom;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            // flowLayoutPanel1.BackgroundImage = Image.FromFile(@Caminho[++Index]);
            BackgroundImage = Image.FromFile(@Caminho[++Index]);
            BackgroundImageLayout = ImageLayout.Center;
            //    SizeMode = PictureBoxSizeMode.Zoom;
        }

        private void ImageForm_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Right || e.KeyCode == Keys.Space)
            {
                BackgroundImage = Image.FromFile(@Caminho[++Index]);
                BackgroundImageLayout = ImageLayout.Center;
            }
            else if (e.KeyCode == Keys.Left)
            {
                BackgroundImage = Image.FromFile(@Caminho[--Index]);
                BackgroundImageLayout = ImageLayout.Center;
            }
        }

        private void ImageForm_MouseWheel(object sender, MouseEventArgs e)
        {
            if (e.Delta > 0)
            {
                BackgroundImage = Image.FromFile(@Caminho[++Index]);
                BackgroundImageLayout = ImageLayout.Center;
            }
            else if (e.Delta < 0)
            {
                BackgroundImage = Image.FromFile(@Caminho[--Index]);
                BackgroundImageLayout = ImageLayout.Center;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
           // flowLayoutPanel1.BackgroundImage = Image.FromFile(@Caminho[--Index]);
            BackgroundImage = Image.FromFile(@Caminho[--Index]);
            BackgroundImageLayout = ImageLayout.Center;
            //  SizeMode = PictureBoxSizeMode.Zoom;
        }
    }
}

Developer technologies | Visual Studio | Other
Developer technologies | C#
{count} votes

2 answers

Sort by: Most helpful
  1. Olaf Helper 47,441 Reputation points
    2023-05-24T07:27:20.33+00:00

    don t working in c#,

    "Don't working" means what in detail? Error message, unexpected behaviour or ...?

    1 person found this answer helpful.

  2. Minxin Yu 13,506 Reputation points Microsoft External Staff
    2023-05-25T01:41:07.7666667+00:00

    Hi,

    There is no information about relationship between Form1 and ImageForm. You put the Key Down function of ImageForm in From1.

    Does ImageForm_KeyDown belong to Form1?

    Please ensure the KeyDown event is bound in form's properties and add break points to check.

    User's image

    You can also use this.KeyDown += ImageForm_KeyDown in the initialization of form.

    Best regards,

    Minxin Yu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.