如何通过程序而不是鼠标按下按钮 [英] how to press button by program not by mouse

查看:57
本文介绍了如何通过程序而不是鼠标按下按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道如何通过程序按下button1,我的意思是在程序内部(以编程方式),而不是将鼠标指向该按钮,然后按下鼠标左键
看我的程序

i need to know how can i press the button1 by the program i mean inside the program (Programitically) not by pointing the mouse on that button then pressing the left mouse button
look at my program

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace keyPressed
{
    public partial class Form1 : Form
    {
        string str = "hellow";
        public Form1()
        {
            InitializeComponent();
            pp();
        }

        private void pp()
        {
            //I need to call button1_Click from here  
            //without using the mouse ,how could i do that ?
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(str);
        }
    }
}

推荐答案

使用Button.PerformClick()方法

Use the Button.PerformClick() method

private void button1_Click(object sender, EventArgs e)
    {
    button2.PerformClick();
    }

private void button2_Click(object sender, EventArgs e)
    {
    MessageBox.Show("Hello");
    }



[edit]添加了示例-OriginalGriff [/edit]



[edit]Added example - OriginalGriff[/edit]


您可以直接调用事件处理方法button1_Click:
You can call the event handling method, button1_Click, directly:
private void pp()        
{            
//I need to call button1_Click from here              
//without using the mouse ,how could i do that ? 

button1_Click(this, new EventArgs());

}



如果要模拟按钮单击,可以发送 WM_LBUTTONDOWN [ ^ ]按钮控制.

问候
Espen Harlinn



If you want to simulate a button click you can send a WM_LBUTTONDOWN[^] to the button control.

Regards
Espen Harlinn


这篇关于如何通过程序而不是鼠标按下按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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