-3

i have a Textbox and some RadioButtons in C#.

Now i want to set a Text to a disabled Textbox.

What i tried:

private void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
    TextBox1.Text = "****";
    TextBox1.Enabled = false;
}

This way i cant see the Text.

If i enable the Textbox, the TextBox shows me the String (****)

What can i do to set a Text to a disabled Textbox?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
NotYourFan
  • 45
  • 5

2 Answers2

1
private void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
        textBox1.Enabled = true;
        textBox1.Text = "*****";
        textBox1.ReadOnly = true;
        textBox1.Enabled = false;
    this.Invalidate(); //to perform form re-draw
}
Kuba Do
  • 155
  • 9
0

You can change the PasswordChar property based on whether or not the textbox is enabled:

TextBox1.PasswordChar = TextBox1.Enabled ? '\0' : '*';

The \0 character will show the contents in plain text.

See this answer for similar results.

ThePerplexedOne
  • 2,920
  • 15
  • 30
  • It isnt a Password TextBox. It can be also a String like "FFFF". – NotYourFan Nov 06 '19 at 12:32
  • 1
    I'm not sure what your point is? The code I gave you will allow you to mask the text based on whether the control is enabled or not. If this isn't what you're looking for, edit your question to make it more clear. – ThePerplexedOne Nov 06 '19 at 12:38
  • i have 10 TextBoxes ... when i checked the first CheckBox i want to disable two TextBoxes. In This TextBoxes should be a String ... – NotYourFan Nov 06 '19 at 12:42
  • 1
    Your opening sentence was `i have a Textbox`. That's not the same as having 10 is it? I'm really not sure what you're looking for. Hopefully someone else's answer will help. – ThePerplexedOne Nov 06 '19 at 12:43
  • its not relevant if i have 10 or 100 TextBoxes .... My question was "How can i disable a TextBox with a Text" ... I dont ask anything about a PasswordChar ???? – NotYourFan Nov 06 '19 at 12:47