我需要在按钮点击事件下方自动化而不实际点击按钮..
请给我一个简单的解决方案..
private void button4_Click(object sender, EventArgs e)
{
int i = 0, aResult = 0;
int[] aLen = new int[1];
byte[] aUID = new byte[7];
byte[] NbTg = new byte[1];
byte[] RData = new byte[64];
byte[] RDataLen = new byte[1];
byte[] InitData = new byte[64];
String ValStr = "";
aResult = aResult = PN_InListPassiveTarget(0, 0, 1, 0, 0, InitData, NbTg, RDataLen, RData);
if (0 == aResult)
{
for (i = 0, ValStr = ""; i < RData[4]; i++)
ValStr += RData[5 + i].ToString("X2");
textBox1.Text = ValStr;
}
else
MessageBox.Show("Please Hold Your Meal Card");
}
答案 0 :(得分:0)
您可以使用计时器以2秒的间隔启动事件。你可以将NFC读卡器逻辑放在它自己的功能中,让定时器从第一次点击开始调用该功能,直到卡被成功读取:
using System.Timers;
public partial class MainWin : Form
{
public MainWin()
{
InitializeComponent();
}
Timer nfcTimer;
private void buttonStartNFC_Read_Click(object sender, EventArgs e)
{
try
{
nfcTimer = new Timer(2000)); \\ <- 2000 milliseconds is 2 seconds
nfcTimer.Elapsed += new ElapsedEventHandler(readCard);
nfcTimer.Enabled = true;
buttonNFC.Enabled = false; \\ u probably want to disable the button
}
catch (Exception ex)
{
print("Exception in :" + ex.Message);
}
}
您可以将NFC读卡器逻辑放在此处:
public void readCard(object sender, ElapsedEventArgs e){
// read card
// failed to read the NFC card -> continue with the timer every 2 sec
// The card was read successfully, you can disable the timer
nfcTimer.Enabled = false;
}
您可以在此处找到有关如何以编程方式调用按钮的Click事件的更多信息:http://msdn.microsoft.com/en-ca/library/hkkb40tf(v=vs.90).aspx
答案 1 :(得分:0)
如果有一个事件会检测到该卡,您可以在那里设置一个定时器,在2秒后运行以执行此代码。 如果仍然需要通过按钮单击运行代码,请将代码移动到方法并从两个位置调用它。
答案 2 :(得分:0)
为什么不使用这样的计时器? -
// Create a timer with a two second interval.
aTimer = new System.Timers.Timer(2000);
// Hook up the Elapsed event for the timer.
aTimer.Elapsed += button4_Click;
aTimer.Enabled = true;
Elapsed
事件的实际事件处理程序签名是 -
private static void OnTimedEvent(Object source, ElapsedEventArgs e)
{
Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime);
}
http://msdn.microsoft.com/en-us/library/system.timers.timer(v=vs.110).aspx
但是EventArgs
是ElapsedEventArgs
的超类,所以你可以使用它。