【问题标题】:Why when changing the text in a textBox the forecolor is black and not green?为什么更改文本框中的文本时,前景色是黑色而不是绿色?
【发布时间】:2015-12-07 21:37:55
【问题描述】:

在 form1 构造函数中,我将 textBox 前景色设置为绿色:

cTextBox3.WaterMarkForeColor = Color.Green;
cTextBox3.WaterMarkActiveForeColor = Color.Green;
cTextBox3.ForeColor = Color.Green;
cTextBox3.WaterMark = "Browse To The File Location";

然后我有一个带有 openFileDialog 的按钮单击事件

private void button6_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = "Json Files (*.json)|*.json";
            openFileDialog1.FilterIndex = 0;
            openFileDialog1.RestoreDirectory = true;
            openFileDialog1.Multiselect = true;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                cTextBox3.Text = openFileDialog1.FileName;
                w = new StreamWriter(AuthenticationFileName, true);
                w.WriteLine(cTextBox3.Text);
                w.Close();
            }

        }

在设计器中,我将 cTextBox3 ReadOnly 属性设置为 true。

问题是一旦将 openFileDialog1.FileName 的文本分配给 cTextBox3.Text,该文本就会变成黑色。我希望它是绿色的。 我尝试在行前添加:

cTextBox3.Text = openFileDialog1.FileName;

所有颜色都像我在构造函数中所做的那样更改为绿色

【问题讨论】:

    标签: c# .net winforms


    【解决方案1】:

    这是由于视觉风格。除非您想完全禁用它们,否则您还需要设置背景颜色,例如:

    tbx.ReadOnly = true;
    tbx.BackColor = Color.White;
    tbx.ForeColor = Color.Green;
    

    【讨论】:

      猜你喜欢
      • 2011-04-27
      • 1970-01-01
      • 1970-01-01
      • 2011-05-21
      • 1970-01-01
      • 2017-03-25
      • 1970-01-01
      • 1970-01-01
      • 2014-06-16
      相关资源
      最近更新 更多