Click here to Skip to main content
12,299,947 members (44,759 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
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 18-Jun-11 21:11pm
Edited 18-Jun-11 21:27pm
v3
Comments
CDP1802 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 ?
SAKryukov 19-Jun-11 20:54pm
   
Always tag it. WPF, Forms, ASP.NET, something else?
--SA
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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#[^]
  Permalink  
v3
Comments
Sweety Khan 19-Jun-11 3:34am
   
yes u r right, its working now, idont know wht happened before.
SAKryukov 19-Jun-11 20:55pm
   
Lies detected, 5 for the answer.
--SA
Kim Togo 20-Jun-11 2:30am
   
Thanks SA
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

int checker;
 
private void btnHideTextBox_Click((object sender, EventArgs e)
{
 
    if (checker%2 == 0)
      myTextBox.Enabled = false;
    else
      myTextBox.Enabled = true;
 
checker++;
 
}

Try this............
  Permalink  
Comments
Sweety Khan 19-Jun-11 3:34am
   
it is the same wht i wrote n now its working. idont know wht happened before.
IsuruSenanayake 20-Jun-11 13:33pm
   
Nice to hear that...... See you with another question.!
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

Hello

Add this code to TextChanged eventHandler:

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!
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160525.2 | Last Updated 3 Apr 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100