【问题标题】:Calling C# click event without actually clicking the button not working在没有实际单击按钮的情况下调用 C# click 事件不起作用
【发布时间】:2018-06-26 15:29:37
【问题描述】:

我在 C# Form 应用程序中编写了以下代码,我试图从 Leap Motion 设备获取 x,yz 坐标。

public partial class Form1 : Form
{


    public Form1()
    {
        InitializeComponent();
        //controller.EventContext = WindowsFormsSynchronizationContext.Current;
        button1.Click += new EventHandler(button1_Click);
     }


    private void button2_Click(object sender, EventArgs e)
    {
        Application.Exit();

    }

    private void button1_Click(object sender, EventArgs e)
    {

        // Initialize the Controller object which connects to the Leap motion service
        // and captures the hand tracking data

        Controller controller = new Controller();

        //Get the most recent tracking data using the Frame object
        Frame frame = controller.Frame();

        for (int h = 0; h < frame.Hands.Count; h++)
        {


            // Initialize the Hand in the given frame
            Hand leapHand = frame.Hands[h];

            // Get the "Pointer" finger of current hand which refers to where a person is pointing
            Finger leapFinger = leapHand.Fingers[1];

            // Prepare a vector which will store the co-ordinate values of the tip of the pointer
            Vector currentPosition = leapFinger.StabilizedTipPosition;

            textBox1.Text = Convert.ToString(currentPosition.x);
            textBox2.Text = Convert.ToString(currentPosition.y);
            textBox3.Text = Convert.ToString(currentPosition.z);

        }

    }
}

但是,我需要显式单击 button1 才能显示。 知道有什么问题吗?

【问题讨论】:

  • 不清楚您的问题是什么。您是否希望无需实际单击按钮即可显示坐标?
  • 你为什么不打电话给button1_Click
  • 我想在不手动点击按钮的情况下调用按钮1的点击事件。我试过 button1.PerformClick() ,它不起作用
  • @sous2817 是的
  • 您还希望代码如何执行?使用不同的事件...必须有触发代码运行的东西。

标签: forms visual-studio c#-4.0 onclick


【解决方案1】:

这是一个非常简单的示例,说明了一种方法。我并不是说这是最好的方法,但您应该能够使用您的代码修改此示例代码。

在这个例子中,有一个简单的“HelloWorld”方法,它在表单初始化时以及在您按下按钮时被调用。如果这能让你接近你所追求的,请告诉我。

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            button1.Click += new EventHandler(button1_Click);

            HelloWorldTest();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            HelloWorldTest();
        }

        private void HelloWorldTest()
        {
            MessageBox.Show("Hello World!");
        }
    }
}

对于它的价值,我没有使用你的代码,因为我没有安装 Leap 库,如果有错字,我不会帮助解决它。尽管如此,希望它能让你走上正确的道路。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-14
    相关资源
    最近更新 更多