visual form c# project, Object reference not set to an instance of an object error.

Gennady Gurin 21 Reputation points
2022-05-19T04:33:04.707+00:00

I have windows form C# project, where i have a feature to use touch devices with the finger instead of a mouse.

I have added a function which enables to do that:

 protected override void WndProc(ref Message m)  
        {  
            switch (m.Msg)  
            {  
  
                case Win32.WM_POINTERDOWN:  
                case Win32.WM_POINTERUP:  
                case Win32.WM_POINTERUPDATE:  
                case Win32.WM_POINTERCAPTURECHANGED:  
                    break;  
  
                default:  
                    base.WndProc(ref m);  
                    return;  
            }  
            int pointerID = Win32.GET_POINTER_ID(m.WParam);  
            Win32.POINTER_INFO pi = new Win32.POINTER_INFO();  
            if (!Win32.GetPointerInfo(pointerID, ref pi))  
            {  
                Win32.CheckLastError();  
            }  
            Point pt = PointToClient(pi.PtPixelLocation.ToPoint());  
            MouseEventArgs me = new MouseEventArgs(System.Windows.Forms.MouseButtons.Left, 1, pt.X, pt.Y, 0);  
            switch (m.Msg)  
            {  
                case Win32.WM_POINTERDOWN:  
                    Console.WriteLine("TOCOU" + pt);  
                    (Parent as Form1).Form1_MouseDown((this as object), me);  
                    break;  
  
                case Win32.WM_POINTERUP:  
                    Console.WriteLine("LEVANTOU");  
                    (Parent as Form1).Form1_MouseUp((this as object), me);  
                    break;  
  
                case Win32.WM_POINTERUPDATE:  
                    Console.WriteLine("UPDATE");  
                    (Parent as Form1).Form1_MouseMove((this as object), me);  
                    break;  
            }  
        }  
  
        private void Form1_MouseDown(object sender, MouseEventArgs e)  
        {  
  
        }  
  
        private void Form1_MouseUp(object sender, MouseEventArgs e)  
        {  
  
        }  
  
        private void Form1_MouseMove(object sender, MouseEventArgs e)  
        {  
  
        }  

I have tested a Windows Form C# application on the tablet , everything is working, except there is a small bug, when I top with my finger anywhere else except buttons or

pictures, or any controls, but just on a form itself I get an exception ERROR:. I have attached a file, and would be happy to fix this exception ERROR:

203512-exception-error-c.jpg

Get back, thank in advance.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,824 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Castorix31 81,481 Reputation points
    2022-05-19T07:14:00.713+00:00

    but just on a form itself I get an exception ERROR:.

    You wrote : (Parent as Form1)

    Then Parent is probably null

    0 comments No comments

  2. Gennady Gurin 21 Reputation points
    2022-05-20T03:10:16.273+00:00

    Hi, I have checked another device I still get the same ERROR when tapping on the FORM itself where there is NO object like picture controls or buttons/

    I am using SPACEDISK server to test as a second monitor, I am using FIRE tablet and iPhone, I get the same error on both.

    Gert back thanks.

    0 comments No comments