question

GennadyGurin-2005 avatar image
0 Votes"
GennadyGurin-2005 asked GennadyGurin-2005 answered

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

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
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@GennadyGurin-2005, Welcome to Microsoft Q&A, based on my test, I could not reproduce your error. The code related to touch is no problem. Therefore, I get an exception about object is not an instance for the following code:

  (Parent as Form1).Form1_MouseDown((this as object), me);


As Castorix31 said, the parent is null.

For your current error, it may be the device problem. If so, please change to another device to test the code to check if it still has the problem.

0 Votes 0 ·

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 Votes 0 ·
Castorix31 avatar image
0 Votes"
Castorix31 answered Castorix31 edited

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

You wrote : (Parent as Form1)

Then Parent is probably null



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

GennadyGurin-2005 avatar image
0 Votes"
GennadyGurin-2005 answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.