当事件“'MouseHover'”处于活动状态时向文本框添加文本

Adding text to text box when event ''MouseHover'' is active

我是 C# 新手。 每次将鼠标悬停在按钮上时,我都想向 HALLO(在文本框中)添加“#”。

这是我的:

public partial class Form1 : Form
{
    string Q = "HALLO";
    string hashtag = "#";

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        tB1.Text = Q;
    }

    private void bT1_MouseHover(object sender, EventArgs e)
    {
        tB1.Text += hashtag;

        if (Q.Length > 20)
        {
            tB1.Clear();
        }

        lBkarakters.Text = Convert.ToString(tB1.Text.Length);
    }

    }
    }

确实添加了“#”,但是 HALLO 不见了。

在某处初始化您的文本框(我建议在 Load 事件处理程序上):

tB1.Text = "HALLO";

为按钮上的 MouseHover 事件注册事件处理程序:

this.yourButton.MouseHover += new System.EventHandler(this.yourButton_MouseHover);

// ...

private void yourButton_MouseHover(object sender, System.EventArgs e)
{
    tB1.Text += "#";
}
public partial class Form1 : Form
{
    string Q = "HALLO";
    string hashtag = "#";

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        tB1.Text = Q;
    }

    private void bT1_MouseHover(object sender, EventArgs e)
    {
        tB1.Text += hashtag;
    }
}

public partial class Form1 : Form
{
    string Q = "HALLO";
    string hashtag = "#";

    public Form1()
    {
        InitializeComponent();

        tB1.Text = Q;
    }

    private void bT1_MouseHover(object sender, EventArgs e)
    {
        tB1.Text += hashtag;
    }
}

确保您的活动已注册: