我的Wondows表单应用程序中有许多按钮以不同的频率闪烁。当通过电极在头上测量特定频率时,信号在MATLAB中经历信号处理,此后将找到的频率发送到应用,其中来自UDP连接的特定值应该按下具有该特定闪烁频率的按钮。我有点迷失如何创建这个按钮处理程序,使用我从Matlab获得的数据。我的想法是:
来自连接的价值 - >
if value == 6
{
button1 is clicked
}
elseif value == 6.5
{
button2 is clicked
}
等等。
任何一个想法?
if(returnData == String.Empty)
{
}
else
{
button2.PerformClick();
}
这里的returnData是从UDP连接到MATLAB的ongiong incomming数据,你会这样做吗?
对于另一个问题,我在UDP连接方面遇到了一些麻烦,我想如果它可以接收我发送的数据,那么现在,我必须按一个按钮打开并接收数据,我已经做了它是这样的,因为我无法更新数据。记住,我是C#的新手。
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;
using System.Net.Sockets;
using System.Net;
namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
//public UdpClient receivingUdpClient = null;
public string returnData;
//public byte[] Receive(ref IPEndPoint remoteEP);
// private Print print = null;
//while (true)
//{
public UdpClient receivingUdpClient = new UdpClient(8051);
//bool done = false;
//Creates an IPEndPoint to record the IP Address and port number of the sender.
// The IPEndPoint will allow you to read datagrams sent from any source.
//while (true)
//{
public IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8051);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
label1.Visible = false;
label2.Visible = false;
}
void button1_Click(object sender, EventArgs e)
{
label1.Visible = false;
try
{
//while (true)
//{
// Blocks until a message returns on this socket from a remote host.
Byte[] receiveBytes = receivingUdpClient.Receive(ref RemoteIpEndPoint);
returnData = Encoding.ASCII.GetString(receiveBytes);
Console.WriteLine("This is the message you received " +
returnData.ToString());
Console.WriteLine("This message was sent from " +
RemoteIpEndPoint.Address.ToString() +
" on their port number " +
RemoteIpEndPoint.Port.ToString());
// }
}
catch (Exception err)
{
Console.WriteLine(err.ToString());
}
if(returnData == String.Empty)
{
}
else
{
button2.PerformClick();
}
}
void button2_Click(object sender, EventArgs e)
{
if (SPELL.Text == String.Empty)
{
SPELL.Text = SPELL.Text + returnData;
label1.Visible = true;
}
else
{
SPELL.Text = null;
SPELL.Text = SPELL.Text + returnData;
label1.Visible = true;
label2.Visible = true;
}
}
}
}
到目前为止,这是我在VIsual Studio中的小型测试程序。
感谢。
答案 0 :(得分:0)
如果你想模拟一个按钮点击后面的代码,你可以简单地调用按钮的处理程序,无论你想在哪里进行调用。
E.g。
if (value == 6)
{
this.button1_click(this,new EventArgs()) //button1 is clicked
}
else if (value == 6.5)
{
this.button2_click(this,new EventArgs()) //button2 is clicked
}
我建议你最好将按钮处理程序的逻辑移动到一个单独的方法并调用方法而不是直接调用处理程序。