c# 使用textbox的mouseclick事件
时间: 2023-10-28 16:56:44 浏览: 166
在C#中使用TextBox的MouseClick事件,可以通过定义事件处理方法来实现。你可以在代码中定义textBox_MouseClick事件处理,代码如下所示:
```csharp
private void textBox_MouseClick(object sender, MouseEventArgs e)
{
if (textBox1.Focused)
{
textBox1.BackColor = Color.Cyan;
}
else
{
textBox1.BackColor = Color.Teal;
}
if (textBox2.Focused)
{
textBox2.BackColor = Color.Cyan;
}
else
{
textBox2.BackColor = Color.Teal;
}
}
```
在这个事件处理方法中,我们可以根据TextBox是否处于焦点状态来改变其背景颜色。如果textBox1处于焦点状态,将其背景颜色设置为Cyan,否则设置为Teal。同样的逻辑也适用于textBox2。
你可以在Form的构造函数或者其他适当的位置,通过textBox.MouseClick事件与textBox_MouseClick事件处理方法进行绑定。具体的代码如下所示:
```csharp
public Form1()
{
InitializeComponent();
textBox1.MouseClick += textBox_MouseClick;
textBox2.MouseClick += textBox_MouseClick;
}
```
通过这样的绑定,当TextBox被点击时,textBox_MouseClick事件处理方法将会被调用,从而实现你想要的效果。<span class="em">1</span><span class="em">2</span>
#### 引用[.reference_title]
- *1* [C#TextBox 失去与获取焦点事件](https://blog.csdn.net/ou832339/article/details/39122395)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"]
- *2* [C# windowForm TextBox获得焦点时高亮显示](https://blog.csdn.net/zcn596785154/article/details/120214380)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文