c# - 如何在 C# 中制作一个始终检查文本框条件的程序?
问题描述
我一直想知道如何在 C# 中制作一个程序,始终检查当文本框不包含任何内容时,将文本框的背景颜色变为红色。否则,文本框保持不变。我已经为此编写了代码,但不知道放在哪里。
for (;;)
{
if (textBox1.Text == "" && textBox2.Text == "")
{
textBox1.BackColor = System.Drawing.Color.Red;
textBox2.BackColor = System.Drawing.Color.Red;
}
if (textBox1.Text != "" && textBox2.Text == "")
{
textBox1.BackColor = System.Drawing.SystemColors.Control;
textBox2.BackColor = System.Drawing.Color.Red;
}
if (textBox1.Text == "" && textBox2.Text != "")
{
textBox1.BackColor = System.Drawing.Color.Red;
textBox2.BackColor = System.Drawing.SystemColors.Control;
}
if (textBox1.Text != "" && textBox2.Text != "")
{
textBox1.BackColor = System.Drawing.SystemColors.Control;
textBox2.BackColor = System.Drawing.SystemColors.Control;
}
}
我也不希望代码处于用户权限之下。(比如说点击一个按钮来检查文本框的情况)
解决方案
您可以使用事件 textbox_change 或按钮
textbox8 是上部文本框,textbox9 是下部文本框试试这个:
---下面的代码是用于文本更改
private void TextBox8_TextChanged(object sender, EventArgs e)
{
if (TextBox8.Text == "")
TextBox8.BackColor = Color.Red;
else
TextBox8.BackColor = Color.White;
}
private void TextBox9_TextChanged(object sender, EventArgs e)
{
if (TextBox9.Text == "")
TextBox9.BackColor = Color.Red;
else
TextBox9.BackColor = Color.White;
}
下面的代码用于按钮:
private void Button2_Click(object sender, EventArgs e)
{
if (TextBox8.Text == "")
TextBox8.BackColor = Color.Red;
else
TextBox8.BackColor = Color.White;
if (TextBox9.Text == "")
TextBox9.BackColor = Color.Red;
else
TextBox9.BackColor = Color.White;
}
推荐阅读
- python - 按特定列排序,但将具有相同主索引的项目放在一起
- python - 如何更改没有文本的表格中所有单元格的字体大小?
- python - 如何检测我的两个图像是否在 Pygame 中接触?
- ios - 如何使用“CADdisplaylink”制作动画(不会制作动画)
- spring - 如何在 Kotlin 中使用 @ConfigurationProperties
- swift - AVAudioPlayer 在使用 M4A 文件初始化时抛出。(OSStatus 错误 2003334207。)
- android - 片段内的 AutocompleteSupportFragment
- rest - 如何使用 api 创建 jira 项目
- javascript - 圆形半径的 Mapbox setPaintProperty 未更新图层
- javascript - Heroku Build 没有正确部署 MERN