从单独的类中确定Button State

时间:2012-10-15 04:27:41

标签: c# windows button emulation

我正在编写我的chip8 Emu的另一个问题。我已经成功地将所有指令从需要输入或涉及图形的指令中移除。

我正在尝试编写一个单独的类来简单地确定按钮是否被按下。我一直在研究这个问题,我认为我必须遗漏一些非常简单的东西或者我从概念上不理解某些东西。这是我正在编写的类,其中包含注释中的确切错误

using System;
 using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsFormsApplication1
{
    class userIn
{
    bool pressed = false;
    public userIn()
    {

    }

    public bool checkPress() //Error    9   'WindowsFormsApplication1.userIn.checkPress()': not all code paths return a value   C

    {  //<---- it is teling me that a closing bracket is expected here?
         private void key5_Click(object sender, EventArgs e)
    {
             pressed = true;
    }

    private void keyb_Click(object sender, EventArgs e)
    {
        pressed = true;
    }

    private void key0_Click(object sender, EventArgs e)
    {
        pressed = true;
    }

    private void keya_Click(object sender, EventArgs e)
    {
        pressed = true;
    }

    private void keye_Click(object sender, EventArgs e)
    {
        pressed = true;
    }

    private void key9_Click(object sender, EventArgs e)
    {
        pressed = true;
    }

    private void key8_Click(object sender, EventArgs e)
    {
        pressed = true;
    }

    private void key7_Click(object sender, EventArgs e)
    {
        pressed = true;
    }

    private void keyd_Click(object sender, EventArgs e)
    {
        pressed = true;
    }

    private void keyf_Click(object sender, EventArgs e)
    {
        pressed = true;
    }

    private void key6_Click(object sender, EventArgs e)
    {
        pressed = true;
    }

    private void key4_Click(object sender, EventArgs e)
    {
        pressed = true;
    }

    private void keyc_Click(object sender, EventArgs e)
    {
        pressed = true;
    }

    private void key3_Click(object sender, EventArgs e)
    {
        pressed = true;
    }

    private void key2_Click(object sender, EventArgs e)
    {
        pressed = true;
    }

    private void key1_Click(object sender, EventArgs e)
    {
        pressed = true;
    }


        return pressed; //Error 6   Invalid token 'return' in class, struct, or interface member declaration    

    }
}

} //错误8类型或命名空间定义,或期望的文件结尾

最后一行不适合方框

有没有人有任何想法或建议?

一如既往地感谢任何帮助!

3 个答案:

答案 0 :(得分:4)

}方法的

checkPress出现在课程结束时,正确的方法:

public bool checkPress()
{ 
    return pressed; 
}

你的代码应该是这样的:

using System;
...

namespace WindowsFormsApplication1
{
    class userIn
    {
        bool pressed = false;
        public userIn()
        {

        }

        public bool checkPress() // This method
        {
            return pressed;
        } // Must close here, not end of class

        .
        . 
        .

        private void key1_Click(object sender, EventArgs e)
        {
            pressed = true;
        }
    }
}

答案 1 :(得分:0)

如果按钮属于类XXX,则可以通过创建只读属性来公开其状态。创建另一个依赖于XXX的内部实现的类不是一个好主意。

答案 2 :(得分:0)

简答:

你已经在另一个方法中打开了一个方法。


答案很长

以下是您的代码正确缩进(我建议您使用AStyle):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsFormsApplication1
{
    class userIn
    {
        bool pressed = false;
        public userIn()
        {

        }

        public bool checkPress() //Error    9   'WindowsFormsApplication1.userIn.checkPress()': not all code paths return a value   C
        {  //<---- it is teling me that a closing bracket is expected here?
            private void key5_Click(object sender, EventArgs e)
            {
                pressed = true;
            }

            private void keyb_Click(object sender, EventArgs e)
            {
                pressed = true;
            }

            private void key0_Click(object sender, EventArgs e)
            {
                pressed = true;
            }

            private void keya_Click(object sender, EventArgs e)
            {
                pressed = true;
            }

            private void keye_Click(object sender, EventArgs e)
            {
                pressed = true;
            }

            private void key9_Click(object sender, EventArgs e)
            {
                pressed = true;
            }

            private void key8_Click(object sender, EventArgs e)
            {
                pressed = true;
            }

            private void key7_Click(object sender, EventArgs e)
            {
                pressed = true;
            }

            private void keyd_Click(object sender, EventArgs e)
            {
                pressed = true;
            }

            private void keyf_Click(object sender, EventArgs e)
            {
                pressed = true;
            }

            private void key6_Click(object sender, EventArgs e)
            {
                pressed = true;
            }

            private void key4_Click(object sender, EventArgs e)
            {
                pressed = true;
            }

            private void keyc_Click(object sender, EventArgs e)
            {
                pressed = true;
            }

            private void key3_Click(object sender, EventArgs e)
            {
                pressed = true;
            }

            private void key2_Click(object sender, EventArgs e)
            {
                pressed = true;
            }

            private void key1_Click(object sender, EventArgs e)
            {
                pressed = true;
            }
            return pressed; //Error 6   Invalid token 'return' in class, struct, or interface member declaration
        }
    }
} // Error 8 Type or namespace definition, or end-of-file expected

你仍然无法看到问题吗?好的,这是一个浓缩版本:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsFormsApplication1
{
    class userIn
    {
        bool pressed = false;
        public userIn()
        {

        }

        public bool checkPress() //Error    9   'WindowsFormsApplication1.userIn.checkPress()': not all code paths return a value   C
        {  //<---- it is teling me that a closing bracket is expected here?
            private void key5_Click(object sender, EventArgs e)
            {
                pressed = true;
            }

            //And some other methods

            return pressed; //Error 6   Invalid token 'return' in class, struct, or interface member declaration
        }
    }
} // Error 8 Type or namespace definition, or end-of-file expected

你认为对吗?

嗯,这只是一种方式,这是编译器看到的:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsFormsApplication1
{
    class userIn
    {
        bool pressed = false;
        public userIn()
        {

        }

        public bool checkPress() //Error    9   'WindowsFormsApplication1.userIn.checkPress()': not all code paths return a value   C
        {  //<---- it is teling me that a closing bracket is expected here?
            private void key5_Click(object sender, EventArgs e)
            {
                pressed = true;
        }

        //And some other methods

        return pressed; //Error 6   Invalid token 'return' in class, struct, or interface member declaration
    }
}
} // Error 8 Type or namespace definition, or end-of-file expected

现在有意义吗?

您可能认为上述代码是正确的,如果是这样,这不是答案......而是一个问题:您认为您想做什么?

好的,是的,C#支持lambda,你试图做以下事情吗?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsFormsApplication1
{
    class userIn
    {
        bool pressed = false;
        public userIn()
        {

        }

        public bool checkPress()
        {

            key5.Click += (sender, e)  =>
            {
                pressed = true;
            };

            //And some other (anonymous) methods

            return pressed;
        }
    }
}

但是我的直觉说你只是做了一些奇怪的复制粘贴,目标代码是这样的:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsFormsApplication1
{
    class userIn
    {
        bool pressed = false;
        public userIn()
        {

        }

        public bool checkPress()
        {               
            return pressed;
        }

        private void key5_Click(object sender, EventArgs e)
        {
            pressed = true;
        }

        //And some other methods
    }
}

有意义吗?我希望。