Locking all controls on a form - From VB2005 to C# 2005

jcrcarmo

Freshman
Joined
Dec 14, 2005
Messages
32
Hi everybody,

Greetings from Brazil. I'm new to C# and would really appreciate it if anyone could please help me translate the VB2005 code below to C#. The following code locks all TextBox and MaskedTextBox controls on a form. Thanks in advance.

Best regards,

JC :)


Public Sub LockForm()
On Error Resume Next
Dim ctl As Control
For Each ctl In Me.Controls
If CType(ctl, TextBox).ReadOnly = False And CType(ctl, MaskedTextBox).ReadOnly = False Then
CType(ctl, TextBox).ReadOnly = True
CType(ctl, TextBox).BackColor = Color.White
CType(ctl, MaskedTextBox).ReadOnly = True
CType(ctl, MaskedTextBox).BackColor = Color.White
End If
Next
End Sub
 
C#:
		private void LockForm()
		{
			foreach(Control ctrlTemp in this.Controls)
			{
				if(ctrlTemp is TextBox)
				{
					TextBox txtTemp = (TextBox)ctrlTemp;
					txtTemp.ReadOnly = true;
					txtTemp.BackColor = Color.White;
				}
				/*// handle other types
				else if(ctrlTemp is CheckBox)
				{
					CheckBox chkTemp = (CheckBox)ctrlTemp;
					// etc...	
				}
				*/		
			}
		}
 
Locking all Controls on a Form - Solved!

Your code works really well. Thanks for replying so quickly.


JC :)
 
Back
Top