我正在尝试通过读取指定给特定文本的颜色来为文本框着色。
这是我的代码:
using (MySQLHelper LeftCurrent = new MySQLHelper())
{
string mysql = "Select BandColor as LeftSideCurrentBandColor from band WHERE PnBanda LIKE '" + textBoxLeftSideCurrentBandColor.Text + "'";
MySqlCommand LeftCurrentCommand = LeftCurrent.GetCommand(mysql);
using (MySqlDataReader reader = LeftCurrentCommand.ExecuteReader())
while (reader.Read())
{
textBoxLeftSideCurrentBandColor.BackColor = System.Drawing.Color.FromName(reader.GetString(0));
}
但每次我更改指定的文本时,颜色都不会改变。我该怎么做?
我有一个文本框,其中填充了以下值之一:N\A、108/5010/0300、108/5015/0200、108/5015/0900。
当文本框填满一个值时,我想用指定的颜色给它着色。
1 N\A Red
2 108/5010/0300 yellow
3 108/5015/0200 DarkBlue
4 108/5015/0900 white
最佳答案
请在不使用循环的情况下尝试此操作,因为您将检索一个正确的值
using (MySQLHelper LeftCurrent = new MySQLHelper())
{
string mysql = "Select BandColor as LeftSideCurrentBandColor from band WHERE PnBanda LIKE '" + textBoxLeftSideCurrentBandColor.Text + "'";
MySqlCommand LeftCurrentCommand = LeftCurrent.GetCommand(mysql);
textBoxLeftSideCurrentBandColor.BackColor = System.Drawing.Color.FromName((LeftCurrentCommand.ExecuteOracleScalar() ?? "RED").ToString());
}
关于c# - 如何从数据库读取特定文本框为文本框背景颜色着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42621181/