Click here to Skip to main content
15,893,663 members
Please Sign up or sign in to vote.
1.80/5 (5 votes)
See more:
how to enable, disable textbox in c# using windows form?

i search n found this

textbox.Enabled = false;


but this is not working,the textbox remains enable
Posted
Updated 18-Jun-11 21:27pm
v3
Comments
[no name] 19-Jun-11 3:15am    
Would you plese also tell us what is not working. Or do we have to guess? It also would be nice to know what kind of controls you are using. ASP .Net? Windows Forms? WPF?
Shahin Khorshidnia 19-Jun-11 3:26am    
Hello, Where did you try to disable the textBox? (textbox.Enable = false;)
Can you paste complite code?
Kim Togo 19-Jun-11 3:27am    
Is it WinForm, WPF or ASP.NET ?
Sergey Alexandrovich Kryukov 19-Jun-11 20:54pm    
Always tag it. WPF, Forms, ASP.NET, something else?
--SA

No that is not true.
When you set TextBox.Enabled = false, the TextBox will not "interact" with the user. The TextBox is still visible and via code you can assign a new value to it.

Please see TextBox.Enabled[^] and How to Lock Text Boxes in C#[^]
 
Share this answer
 
v3
Comments
Sweety Khan 19-Jun-11 3:34am    
yes u r right, its working now, idont know wht happened before.
Sergey Alexandrovich Kryukov 19-Jun-11 20:55pm    
Lies detected, 5 for the answer.
--SA
Kim Togo 20-Jun-11 2:30am    
Thanks SA
int checker;

private void btnHideTextBox_Click((object sender, EventArgs e)
{

    if (checker%2 == 0)
      myTextBox.Enabled = false;
    else
      myTextBox.Enabled = true;

checker++;

}


Try this............
 
Share this answer
 
Comments
Sweety Khan 19-Jun-11 3:34am    
it is the same wht i wrote n now its working. idont know wht happened before.
Isuru_senanayake 20-Jun-11 13:33pm    
Nice to hear that...... See you with another question.!
Even if Enabled did work - which I agree it doesn't appear to, but I haven't tested that much - it would probably confuse the user because it would still look like it should work.

Instead, use the ReadOnly property - that also grays the background so the user gets a visual cue that he can't enter information.
 
Share this answer
 
Hello

Add this code to TextChanged eventHandler:

C#
private string previousText;
private void TextBox_TextChanged(object sender, EventArgs e)
{
  if (!this.TextBox.Enabled)
  {
    this.TextBox.Text = previousText;
    return;
  }
previousText = this.TextBox.Text;
}


Then the textBox likes enabled via code too!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900