C# Button 按钮功能

1.Button1 按下为True,Button2按下为false

 // M102打开
        private void butt1_Click_1(object sender, EventArgs e)
        {
            string _Addr = "102";
            Int16 _value = Int16.Parse("1");
            bool[] _Send = new bool[1];
            if (_value > 0)
            {
                _Send[0] = true;
            }
            else
            {
                _Send[0] = false;
            }
            mform1.P_mPLC.DB_WriteByte(UInt16.Parse(_Addr), _Send); // 发送数据到PLC
        }


        //M102关闭
        private void butt2_Click(object sender, EventArgs e)
        {
            string _Addr = "102";
            Int16 _Value = Int16.Parse("0");
            bool[] _Send = new bool[1];
            if (_Value > 0)
            {
                _Send[0] = true;
            }
            else
            {
                _Send[0] = false;
            }
            mform1.P_mPLC.DB_WriteByte(UInt16.Parse(_Addr), _Send); // 发送数据到PLC

PS: 注意在事件属性中要选择对应的事件

2.Button1 按下为True,松开为false ,点动

 //单按钮切换,点动
        private void button10_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
           
                string _Addr = "104";
                Int16 _Value = Int16.Parse("1");
                bool[] _Send = new bool[1];
                _Send[0] = true; // 如果值大于0,设置为true,否则为false  
                mform1.P_mPLC.DB_WriteByte(UInt16.Parse(_Addr), _Send); // 发送数据到PLC 
            }
        }

        private void button10_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
           
                string _Addr = "104";
                Int16 _Value = Int16.Parse("0");
                bool[] _Send = new bool[1];

                _Send[0] = false; // 如果值大于0,设置为true,否则为false  
                mform1.P_mPLC.DB_WriteByte(UInt16.Parse(_Addr), _Send); // 发送数据到PLC  
            }
        }

3. 循环点动,取反点动

//    单击切换状态自动手动
        private bool leftButtonDown5 = false;

        private void button10_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                // 切换leftButtonDown的状态    
                leftButtonDown5 = !leftButtonDown5;

                string _Addr = "105";
                Int16 _Value = leftButtonDown5 ? Int16.Parse("1") : Int16.Parse("0");
                bool[] _Send = new bool[1];

                _Send[0] = leftButtonDown5;
                //_Send[0] = true; // 设置_Send数组的值根据leftButtonDown的状态    
                mform1.P_mPLC.DB_WriteByte(UInt16.Parse(_Addr), _Send); // 发送数据到PLC  

                if (leftButtonDown5 == true)
                {
                    button10.BackColor = Color.Green;
                    button10.Text = "手动模式";
                }
                else
                {
                    button10.BackColor = Color.Red;
                    button10.Text = "自动模式";

                }

            }
        }

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值