当事件“MouseHover”处于活动状态时,将文本添加到文本框中

时间:2016-09-30 12:54:32

标签: c# winforms visual-studio mousehover

我是C#的新手。 每次将鼠标悬停在按钮上时,我都希望将“#”添加到HALLO(在textBox中)。

这就是我所拥有的:

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已经消失了。

2 个答案:

答案 0 :(得分:3)

在某处初始化您的文本框(我建议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 += "#";
}

答案 1 :(得分:0)

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

确保您的活动已注册:

enter image description here