-2

I dynamically create multiple textboxes for showing information to the user. Now I want to set the back- and forecolor of some textboxes if a statement is true.

All this works just fine, untill I disable the textbox, now the forecolor "resets" to the standart color instead of showing my desired one.

DataTable dt = [some data]
//Col 0: ID
//Col 1: some text
//Col 2: date

for (int i = 0; i < dt.Rows.Count; i++)
{

    DateTime d = (DateTime) dt.Rows[i].ItemArray[2];
    TextBox txt = new TextBox();
    txt.Multiline = true;
    txt.Font = tb_Aufloesung.Font;
    txt.Text = dt.Rows[i].ItemArray[1].ToString() + "\n" + d.ToString(@"dd.MM.yyyy");
    txt.Size = new Size((TextRenderer.MeasureText(dt.Rows[i].ItemArray[1].ToString(), txt.Font).Width) + 10, 34);
    txt.Location = new Point(43, 3 + split.Panel2.Controls.Count / 2 * 40);

    if(d <= DateTime.Now) {
        txt.BackColor = Color.Red;
        txt.ForeColor = Color.White;
    }    

    //txt.Enabled = false;

    split.Panel2.Controls.Add(txt);
}

This is how the Textboxes look when I use the code above with :

  1. comment the line -> //txt.Enabled = false;
  2. uncomment the line -> txt.Enabled = false;

enter image description here

I have no idea why in the second case, the forecolor of the red textbox is not white as it should be. Anyone any idea?

Tim Schmidt
  • 1,297
  • 1
  • 15
  • 30
  • Instead, you can set its `ReadOnly` property to true. – Reza Aghaei Nov 08 '16 at 10:12
  • Well `ReadOnly` wasn´t the solution i searched for. @Sinatr is right, duplicate. – Tim Schmidt Nov 08 '16 at 10:16
  • [See here for a similar example](http://stackoverflow.com/questions/40416262/change-label-text-on-its-own-click-event-during-runtime/40418144#40418144) - You would use the EnabledChanged event. – TaW Nov 08 '16 at 11:51

1 Answers1

0

This is how textboxes work - they're meant for input, not display. Why not just use a label?

robhol
  • 195
  • 1
  • 11
  • Becaus at some other point there might be a userinput necessary, but untill then the textboxes should be disabled. – Tim Schmidt Nov 08 '16 at 10:19