我是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已经消失了。
答案 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;
}
}
确保您的活动已注册: