我希望使用鼠标输入事件为我的用户控制 [英] i want use from mouse enter event for my user control

查看:63
本文介绍了我希望使用鼠标输入事件为我的用户控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi
i想要使用鼠标进入和离开事件以供我的用户控制。

i想要在鼠标进入时改变用户控制forecolor,但是不能正常工作鼠标输入事件用户控制项目,

我的代码用户控件是(这是用于悬停):

  private   void  lbl_operation_MouseHover( object  sender,EventArgs e)
{
lbl_operation.ForeColor = Color.Yellow;
}



我怎么做?

解决方案

UserControl的完整示例:



使用System; 
使用System.Collections.Generic;使用System.ComponentModel
;
使用System.Data;使用System.Drawing
;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;
使用System.Windows.Forms;

命名空间Test_UserControl
{
公共部分类Form1:表格
{
MyCtl ctl = new MyCtl();
颜色ctlDefaultColor;

public Form1()
{
InitializeComponent();
InitOthers();
}

private void InitOthers()
{
this.SuspendLayout();
//设置ctl属性
this.ctl.Left = 10;
this.ctl.Top = 10;
this.Controls.Add(ctl);
this.ctl.MouseEnter + = ctl_MouseEnter;
this.ctl.MouseLeave + = ctl_MouseLeave;

this.ctlDefaultColor = this.ctl.ForeColor;
this.ResumeLayout(false);
this.PerformLayout();
}

void ctl_MouseLeave(object sender,EventArgs e)
{
//恢复背景颜色
this.ctl.ForeColor = this.ctlDefaultColor;
}

void ctl_MouseEnter(object sender,EventArgs e)
{
// set backcolor
this.ctl.ForeColor = Color.Yellow;
}
}

公共类MyCtl:UserControl
{
Label lbl;
Button btn;

public MyCtl()
{
this.Initialize();
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
}
private void Initialize()
{
this.SuspendLayout();
this.lbl = new Label();
this.lbl.Left = 10;
this.lbl.Top = 10;
this.lbl.Text =这里有一个标签;
this.Controls.Add(this.lbl);

this.btn = new Button();
this.btn.Left = 10;
this.btn.Top + = this.lbl.Bottom + 10;
this.btn.Text =我的按钮;
this.Controls.Add(this.btn);

this.ResumeLayout(false);
}

}
}





和文档:用户控件类 [ ^ ]


hi i wanna use mouse enter and leave events for my user control.
i wanna changing user control forecolor when mouse enter,but don't work mouse enter event the user controls items,
my code user control is(this is for hover):

private void lbl_operation_MouseHover(object sender, EventArgs e)
       {
           lbl_operation.ForeColor = Color.Yellow;
       }


how do i?

解决方案

Complete example for a UserControl:

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;

namespace Test_UserControl
{
    public partial class Form1 : Form
    {
        MyCtl ctl = new MyCtl();
        Color ctlDefaultColor;

        public Form1()
        {
            InitializeComponent();
            InitOthers();
        }

        private void InitOthers()
        {
            this.SuspendLayout();
            // set ctl properties
            this.ctl.Left = 10;
            this.ctl.Top = 10;
            this.Controls.Add(ctl);
            this.ctl.MouseEnter += ctl_MouseEnter;
            this.ctl.MouseLeave += ctl_MouseLeave;

            this.ctlDefaultColor = this.ctl.ForeColor;
            this.ResumeLayout(false);
            this.PerformLayout();
        }

        void ctl_MouseLeave(object sender, EventArgs e)
        {
            // restore backcolor
            this.ctl.ForeColor = this.ctlDefaultColor;
        }

        void ctl_MouseEnter(object sender, EventArgs e)
        {
            // set backcolor
            this.ctl.ForeColor = Color.Yellow;
        }
    }

    public class MyCtl : UserControl
    {
        Label lbl;
        Button btn;

        public MyCtl()
        {
            this.Initialize();
        }
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }
        private void Initialize()
        {
            this.SuspendLayout();
            this.lbl = new Label();
            this.lbl.Left = 10;
            this.lbl.Top = 10;
            this.lbl.Text = "There exists a label here";
            this.Controls.Add(this.lbl);

            this.btn = new Button();
            this.btn.Left = 10;
            this.btn.Top += this.lbl.Bottom + 10;
            this.btn.Text = "My Button";
            this.Controls.Add(this.btn);

            this.ResumeLayout(false);
        }
        
    }
}



And documentation: UserControl Class[^]


这篇关于我希望使用鼠标输入事件为我的用户控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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