自动按钮单击C# [英] Automate Button Click in C#

查看:177
本文介绍了自动按钮单击C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要自动化下方按钮点击事件,而无需实际单击该按钮。

请给我这一个简单的解决方案。

 私人无效button4_Click(对象发件人,EventArgs的发送)
    {
        INT I = 0,综合因素的产物= 0;
        INT []阿伦=新INT [1];
        字节[] = AUID新的字节[7];
        字节[] = NbTg新的字节[1];
        字节[] = RDATA新的字节[64];
        字节[] = RDataLen新的字节[1];
        字节[] = InitData新的字节[64];
        串ValStr =;        综合因素的产物=综合因素的产物= PN_InListPassiveTarget(0,0,1,0,0,InitData,NbTg,RDataLen,RDATA);
        如果(0 ==综合因素的产物)
        {
            对于(I = 0,ValStr =; I&下; RDATA [4];我+ +)
                ValStr + = RDATA [5 + 1]的ToString(X2);
            textBox1.Text = ValStr;        }
        其他
            的MessageBox.show(请您持饭卡);
    }


解决方案

您可以使用计时器启动事件间隔2秒。你可以把NFC读卡器的逻辑在其自身的功能,并让计时器调用,从第一次点击的功能直到卡被成功读取:

 使用System.Timers;公共部分类MainWin能够:表单
    {
        公共MainWin将()
        {
            的InitializeComponent();
        }        定时器nfcTimer;        私人无效buttonStartNFC_Read_Click(对象发件人,EventArgs的发送)
        {
            尝试
            {
                nfcTimer =新定时器(2000)); \\\\< - 2000毫秒为2秒
                nfcTimer.Elapsed + =新ElapsedEventHandler(readCard);
                nfcTimer.Enabled = TRUE;                buttonNFC.Enabled = FALSE; \\\\ü可能要禁用按钮
            }
            赶上(异常前)
            {
                打印(中的例外:+ ex.Message);
            }
        }

您可以在这里把NFC读卡器的逻辑:

 公共无效readCard(对象发件人,ElapsedEventArgs E){
    //读取卡    //无法读取NFC卡 - >继续每2秒计时器    //卡被成功读取,您可以禁用计时器
    nfcTimer.Enabled = FALSE;
 }

您可以找到有关如何调用一个按钮的Click事件编程这里更多的信息:的 http://msdn.microsoft.com/en-ca/library/hkkb40tf(v = VS.90)的.aspx

I need to automate below button click event without actually clicking the button..

Please give me a simple solution for this..

   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");
    }  

解决方案

You can use a timer to initiate the event in 2 seconds intervals. You can put the NFC card reader logic in its own function and let the timer call that function from the first click until the card was successfully read:

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);
            }
        }

You can put the NFC card reader logic here:

 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;
 }

You can find more info about how to call a Button's Click Event Programmatically here: http://msdn.microsoft.com/en-ca/library/hkkb40tf(v=vs.90).aspx

这篇关于自动按钮单击C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆