Windows Develop Bookmark and Share   
 index > Windows Forms Sample Applications > Changing readonly property of all textboxes on form
 

Changing readonly property of all textboxes on form

In VB code, I am trying to change the readonly property of all of the textboxes on a form so they can't be edited. However, I haven't been able to find a way to do this without writing code to specify each textbox name and then change it's readonly property individually.

Does anyone have a solution to this problem?I'm at my wits end with this. I can't imagine having to go through each form and manually turning on and off the readonly property manually.

Thanks

Julie Missick  Monday, July 21, 2008 6:55 PM

Hi!

This should do it:

Code Snippet

for each currentControl as Control in Me.Controls

ifTypeOf currentControl Is TextBox then

Dim theTextBox as TextBox =DirectCast(currentControl, TextBox)

theTextBox.ReadOnly = true

end if

next

Regards,

Lucian Baciu  Tuesday, July 22, 2008 7:42 AM

Hi Julie,

A trick for you:

1.Create a custom TextBox which sets the ReadOnly property toTrue by default:

Public Class ReadOnlyTextBox
Inherits TextBox

Public Sub New()
MyBase.New()
Me.ReadOnly = True
End Sub
End Class

2. Build the class.
3. Press Ctrl+Shift+H keys to open the Find and Replace dialog;

4. Type System.Windows.Forms.TextBox in the "Find What" box;

5. Type ReadOnlyTextBox in the "Replace with" box;
6. Check "Match cases" and "Match whole word" options;

7. Click "Replace All".

8. Rebuild your project.

After the steps, all of the TextBoxes in the entire solution become read only.

Best Regards,

Zhi-Xin Ye

Zhi-Xin Ye  Tuesday, July 22, 2008 7:45 AM

Hi!

This should do it:

Code Snippet

for each currentControl as Control in Me.Controls

ifTypeOf currentControl Is TextBox then

Dim theTextBox as TextBox =DirectCast(currentControl, TextBox)

theTextBox.ReadOnly = true

end if

next

Regards,

Lucian Baciu  Tuesday, July 22, 2008 7:42 AM

Hi Julie,

A trick for you:

1.Create a custom TextBox which sets the ReadOnly property toTrue by default:

Public Class ReadOnlyTextBox
Inherits TextBox

Public Sub New()
MyBase.New()
Me.ReadOnly = True
End Sub
End Class

2. Build the class.
3. Press Ctrl+Shift+H keys to open the Find and Replace dialog;

4. Type System.Windows.Forms.TextBox in the "Find What" box;

5. Type ReadOnlyTextBox in the "Replace with" box;
6. Check "Match cases" and "Match whole word" options;

7. Click "Replace All".

8. Rebuild your project.

After the steps, all of the TextBoxes in the entire solution become read only.

Best Regards,

Zhi-Xin Ye

Zhi-Xin Ye  Tuesday, July 22, 2008 7:45 AM

Thanks for your help. This works, but I don't quite know why. Why do you need to directcast a textbox to a textbox?

I had already found that you can no longer iterate through the controls using GetType to turn properties on and off for a group of textboxes, but not one of the books I've obtained tell you how to make the transition to VB 2008.

Some of the things that worked in VB6 jut don't in VB 2008, and I'm having trouble, even with all the books I've aquired on VB 2008, making the connection of why certain thingswork the way they are. Do you have any suggestions?

Julie

Julie Missick  Tuesday, July 22, 2008 2:34 PM

Thanks for your response to my question. This goes beyond what I currently need to do.

Julie

Julie Missick  Tuesday, July 22, 2008 2:39 PM

You can use google to search for other answers

Custom Search

More Threads

• XML Web Services over APACHE ??
• Scheduler
• cannot log in with jdoe & welcome
• Problem with scrollbars of panel in C#
• Forms without icon
• system clender time and date
• I can not see a data in the database?!
• What happened to the Tarrarium Forum?
• æ— æ³•打开文件 C:\Program Files\Terrarium Server [Beta 1]\\website\chartdata\33b4a0ad-aefb-4661-8e5b-7152b87f0ecc.gif 以进行导出ã€
• taskvision cpntrol sources