Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a form with 4 textboxes.How can i disable all other textboxes after a textbox get edited?
i can't predict which textbox get edited.
Posted
Updated 8-May-11 21:54pm
v2

VB
for each ctrl as control in me.controls
 if typeof ctrl is textbox then
  if not ctrl.name = sender.name then
    ctrl.enable = false
  end if
 end if
next


you will need to put something like this in each textboxes textchanged or similar event
 
Share this answer
 
v2
Comments
RAJI @Codeproject 9-May-11 4:27am    
thanks for this!!
Tarun.K.S 9-May-11 5:25am    
Excellent answer. 5+
Simon_Whale 9-May-11 5:37am    
Thanks :)
This is how I did:

First I created a common TextChanged event for all the textboxes.
VB
Private Sub TextChanged(sender As Object, e As EventArgs)
	Dim txtEdit As TextBox = DirectCast(sender, TextBox)
	For Each cntrl As Control In Me.Controls
		If TypeOf cntrl Is TextBox AndAlso cntrl.Name <> txtEdit.Name Then
			cntrl.Enabled = False
		End If
	Next
End Sub
}


And then register it :

VB
Public Sub New()
    InitializeComponent();
    AddHandler(textBox1.TextChanged, AddressOf(TextChanged))
    AddHandler(textBox2.TextChanged, AddressOf(TextChanged))
    AddHandler(textBox3.TextChanged, AddressOf(TextChanged))
End Sub


Hope it helped.
 
Share this answer
 
v3
Comments
Wayne Gaylard 9-May-11 5:28am    
How do you get 2 different languages in one Code file? The method is VB and the registration is C#?
Tarun.K.S 9-May-11 5:32am    
Oops conversion problem. Will fix it right away.
Wayne Gaylard 9-May-11 5:33am    
No problem, just pulling your leg.
Tarun.K.S 9-May-11 5:36am    
:D
Tarun.K.S 9-May-11 5:33am    
Fixed now. Thanks for notifying that. :)
try this:
protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
        TextBox1.Enabled = false;
        TextBox2.Enabled = false;

        TextBox3.Enabled = false;
        TextBox4.Enabled = false;
    }
 
Share this answer
 
v2
Google is enough for you.

Disable all textboxes vb.net[^]
 
Share this answer
 
Comments
Simon_Whale 9-May-11 4:23am    
nice answer for such a simple question +5
All you need to do is call TextBox1.Enabled = false in your TextBox Changed or TextBox KeyPress events.

Hope this helps
 
Share this answer
 
its easy..
you can you foreach code to desable all the text box controls in the form....
 
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