user2763309
user2763309

Reputation:

"} expected" where it shouldn't

I'm new to C# and I came across two errors. I've got this code from here and put it in my application. I've tried replacing the mouseclick function but never successful and I'm not sure if the public static extern void mouse_event is in the right place.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{


    public partial class Form1 : Form
    {
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        { //Here it expects "}"

            public const int MOUSEEVENTF_LEFTDOWN = 0x02;
            public const int MOUSEEVENTF_LEFTUP = 0x04;
            public const int MOUSEEVENTF_RIGHTDOWN = 0x08;
            public const int MOUSEEVENTF_RIGHTUP = 0x10;

            public void MouseClick()
            {
                int x = 100;
                int y = 100;
                mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
                mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
            }
        }
    }
}//Here the error is "type or namespace definition, or end-of-file expected

Upvotes: 0

Views: 109

Answers (3)

Hamid Pourjam
Hamid Pourjam

Reputation: 20754

Problem 1:
You can not declare public const fields inside methods

private void button2_Click(object sender, EventArgs e)
{ //Here it expects "}"
    public const int MOUSEEVENTF_LEFTDOWN = 0x02; //error

Access modifiers (public ,private ...) are keywords used to specify the declared accessibility of a member or a type and they are not supposed to be used inside methods. Removing the public keyword before const definitions will solve the problem. If you want to access the constants from other methods in your class or from outside the class then you should move them into class.

Problem 2:
You are defining a method inside another method and you are not allowed to do that. MouseClick is declared inside button2_Click which is not legal.

Try to change your code to this

public const int MOUSEEVENTF_LEFTDOWN = 0x02;
public const int MOUSEEVENTF_LEFTUP = 0x04;
public const int MOUSEEVENTF_RIGHTDOWN = 0x08;
public const int MOUSEEVENTF_RIGHTUP = 0x10;

private void button2_Click(object sender, EventArgs e)
{ 
    MouseClick();
}

public void MouseClick()
{
    int x = 100;
    int y = 100;
    mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
    mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
}

Upvotes: 1

ppetrov
ppetrov

Reputation: 3105

You have to move your MouseClick method, and your constants declarations to the scope of your Form1 class:

public partial class Form1 : Form
{
    [System.Runtime.InteropServices.DllImport("user32.dll")]
    public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    public const int MOUSEEVENTF_LEFTDOWN = 0x02;
    public const int MOUSEEVENTF_LEFTUP = 0x04;
    public const int MOUSEEVENTF_RIGHTDOWN = 0x08;
    public const int MOUSEEVENTF_RIGHTUP = 0x10;

    public void MouseClick()
    {
        int x = 100;
        int y = 100;
        mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
        mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
    }

    private void button2_Click(object sender, EventArgs e)
    { //Here it expects "}"
        MouseClick();
    }
}

Upvotes: 0

Lucas Trzesniewski
Lucas Trzesniewski

Reputation: 51330

It expects } because you wrote public const int MOUSEEVENTF_LEFTDOWN = 0x02; after {, and the public keyword is something that can't appear in a method body. Local constants are... well... local, not public.

Same for MouseClick, you can't write methods inside methods with this syntax, you can only write lambdas and inline delegates.

Did you mean this?

private void button2_Click(object sender, EventArgs e)
{
    const int MOUSEEVENTF_LEFTDOWN = 0x02;
    const int MOUSEEVENTF_LEFTUP = 0x04;
    const int MOUSEEVENTF_RIGHTDOWN = 0x08;
    const int MOUSEEVENTF_RIGHTUP = 0x10;

    int x = 100;
    int y = 100;
    mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
    mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
}

or perhaps this?

private void button2_Click(object sender, EventArgs e)
{
    MouseClick();
}

public const int MOUSEEVENTF_LEFTDOWN = 0x02;
public const int MOUSEEVENTF_LEFTUP = 0x04;
public const int MOUSEEVENTF_RIGHTDOWN = 0x08;
public const int MOUSEEVENTF_RIGHTUP = 0x10;

public void MouseClick()
{
    int x = 100;
    int y = 100;
    mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
    mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
}

Upvotes: 2

Related Questions