Resolved Using MouseWheel on application main form.

MPIon

Well-known member
Joined
Jun 13, 2020
Messages
73
Location
England
Programming Experience
10+
I am trying to access the MouseWheel events on my application main form, with much difficulty. There is no event handler in the properties and after much internet searching, it seems as if I have to manually create the event handler.
My code is :-
C#:
using System.ComponentModel;
using System.IO;
using System.Net.NetworkInformation;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using static System.Net.WebRequestMethods;

namespace Image_Viewer
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            
            this.MouseWheel += new MouseEventHandler(MainForm_MouseWheel);
        }
But the compiler says
1691426373754.png

Am I missing a directive or something?
I have seen almost identical code posted on the internet, except using Form1 instead of MainForm
 
All this does is hookup the event handler to a method named MainForm_MouseWheel within the same class:
C#:
this.MouseWheel += new MouseEventHandler(MainForm_MouseWheel);

Where did you define the method and its implementation? The compiler can't find it and so it is giving you that error.
 
All this does is hookup the event handler to a method named MainForm_MouseWheel within the same class:
C#:
this.MouseWheel += new MouseEventHandler(MainForm_MouseWheel);

Where did you define the method and its implementation? The compiler can't find it and so it is giving you that error.

Ok thanks very much, missing much knowledge here. I have added this to method and implementation to code as follows - just to test it is working .
C#:
        public MainForm()
        {
            InitializeComponent();
           
            this.MouseWheel += new MouseEventHandler(MainForm_MouseWheel);
        }

        private void MainForm_MouseWheel(object sender, MouseEventArgs e)
        {
            MessageBox.Show("Mouse wheel");
        }

This does work - but have got a compiler warning which does not make much sense to me.

1691429017596.png
 
C#:
public delegate void MouseEventHandler(object? sender, MouseEventArgs e);
Notice the nullable sender parameter.
 
C#:
public delegate void MouseEventHandler(object? sender, MouseEventArgs e);
Notice the nullable sender parameter.

Thanks for that, the ? after object got rid of the compiler warning.
C#:
        private void MainForm_MouseWheel(object? sender, MouseEventArgs e)
        {
            MessageBox.Show("Mouse wheel");
        }

I did not change the code to public delegate as it gave compiler errors. Where would that line have been placed in the class, in addition to the actual code above to handle the event?

Must go on a refresher course as I haven't written much c# since I was writing code with Visual C# 2020.
 
MouseEventHandler is the delegate for that event, your handler method must match the signature, which means you must have a void method with matching arguments - that you now have.
 
By the way, if you write:

C#:
  someThing.SomeEvent +=

Visual Studio asks you to press TAB a first time to complete it to..


C#:
  someThing.SomeEvent += new SomeEventHandler(SomeThing_SomeEvent);

And the `SomeThing_SomeEvent` is highlighted whereupon it asks you to press TAB a second time to complete it to:

C#:
  someThing.SomeEvent += new SomeEventHandler(SomeThing_SomeEvent);
}

void SomeThing_SomeEvent(object sender, SomeThingEventArgs e){
  throw new NotImplementedException();
}

You don't get this help/offer to press TAB when pasting..

Life is easier when you use the force! :)
 
Specially when you use git push --force. :)
 

Latest posts

Back
Top Bottom