Forms 调用C#click事件,而不实际单击不起作用的按钮
我在C#Form应用程序中编写了以下代码,试图从Leap Motion设备获取Forms 调用C#click事件,而不实际单击不起作用的按钮,forms,visual-studio,c#-4.0,onclick,Forms,Visual Studio,C# 4.0,Onclick,我在C#Form应用程序中编写了以下代码,试图从Leap Motion设备获取x,y和z坐标 public partial class Form1 : Form { public Form1() { InitializeComponent(); //controller.EventContext = WindowsFormsSynchronizationContext.Current; button1.Click += new
x,y
和z
坐标
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);
}
}
}
公共部分类表单1:表单
{
公共表格1()
{
初始化组件();
//controller.EventContext=WindowsFormsSynchronizationContext.Current;
button1.Click+=新建事件处理程序(button1\u Click);
}
私有无效按钮2\u单击(对象发送者,事件参数e)
{
Application.Exit();
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
//初始化连接到Leap motion服务的控制器对象
//并捕获手部跟踪数据
控制器=新控制器();
//使用帧对象获取最近的跟踪数据
Frame=controller.Frame();
对于(int h=0;h
但是,我需要显式单击按钮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库,如果有输入错误,我也不会帮你解决。尽管如此,希望它能让你走上正确的道路。不清楚你的问题是什么。您是否希望坐标显示,而不必实际单击按钮?为什么不调用
按钮1\u click
?我想调用按钮1的单击事件,而不手动单击按钮。我尝试了button1.PerformClick(),但没有work@sous2817是的,确切地说,您还希望代码如何执行?使用不同的事件…必须有触发代码运行的东西。