gpt4 book ai didi

C# 按长度更改 txtBox BackColor

转载 作者:行者123 更新时间:2023-11-30 20:23:57 25 4
gpt4 key购买 nike

我想写一个简单的“注册/登录”程序,只是为了我,只是为了好玩。

我想更改用户键入姓名的 TxtBox 的颜色。当txtBox.Length<4它应该将其背景更改为红色。

我不知道为什么我的下面的代码不起作用。当我将 txtBox 属性中的文本固定更改为超过 5 个时,它一开始是蓝色的,但之后没有改变。

我的代码:

  using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _4Fun
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
if (regTxtBoxName.TextLength<4) {
regTxtBoxName.BackColor = Color.Red;
}
else{
regTxtBoxName.BackColor = Color.DarkBlue;
}
}
private void Form1_Load(object sender, EventArgs e)
{

}

private void regBtn_Click(object sender, EventArgs e)
{
if (regTxtBoxName.TextLength < 4)
{
txtBoxStatus.Text = "Choose a name with minimal length 5. "; // Urobit txtboxname a pass v registru červene pozadie ak x<4
}
else {
txtBoxStatus.Text = "Your account has been successfully created.";
string name = regTxtBoxName.Text;

}
if (regTxtBoxPass.TextLength < 4)
{
txtBoxStatus.Text = txtBoxStatus.Text + "Choose password with minimal length 5. ";
}
else {
txtBoxStatus.Text = "Your account has been successfully created.";
string pass = regTxtBoxPass.Text;
}

}
}
}

最佳答案

您的代码在窗体的构造函数中设置颜色,然后您不更改它。您需要在 TextBox 上注册 TextChanged 事件,以便在应用程序运行时根据 Textbox 中的字符数更改颜色.

关于C# 按长度更改 txtBox BackColor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27705031/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com