When the readonly property of textbox is set to true then back colour turns to grey. i use below code to change the back colour to white

 Private Sub TextBox1_ReadOnlyChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.ReadOnlyChanged
 If TextBox1.ReadOnly = True Then
 TextBox1.BackColor = Color.White
 End If
 End Sub

How can i make custom control for text box to handel above code so that i dont have to write above code for each and every text box.

You could put the following in the form load event

For Each tbx As TextBox In Me.Controls.OfType(Of TextBox)()
    If tbx.ReadOnly Then
        tbx.BackColor = Color.White
    End If
Next

no it dosen change the BackColor

I tried it before I posted it and, yes, it does. Are your textboxes in another collection like a panel or groupbox? If so then you have to iterate through that collection instead.

Here's one way to do this. Make your own control:

Public Class White_ReadOnly
    Inherits TextBox
    Public Sub New()
        [ReadOnly] = True
        BackColor = Color.White
    End Sub
End Class

With this you can add new ones dynamically, by adding this class to the end of your form code, and declaring it like any variable:

Dim NewReadOnlyTB as Whie_ReadOnly
NewReadOnlyTB.Location = New Point(35,92)
Me.Controls.Add(NewReadOnlyTB)

You could also put this in a control library and add it to your Toolbox, to be available for use in any project.

yes all textboxes were in different gorupboxes ,so i tried below code and it worked

 For Each Gbox As GroupBox In Me.Controls.OfType(Of GroupBox)()
    For Each tbx As TextBox In Gbox.Controls.OfType(Of TextBox)()
        If tbx.ReadOnly Then
        tbx.BackColor = Color.White
        End If
     Next
Next

but there are some textboxes wher user cant change value and set as read only with yellow backcolor

where above code changes the back color.

I would prefer to making custom control

Public Class CustomTextBox
Inherits TextBox

how to add below code in custom control

Private Sub TextBox1_ReadOnlyChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.ReadOnlyChanged
If TextBox1.ReadOnly = True Then
TextBox1.BackColor = Color.White
End If
End Sub

how to capture or override ReadOnlyChanged event in my custom control class

I've attached a sample project in which I created a custom textbox control. The only difference between the custom control and the standard textbox is that the background color is forced to white regardless of the ReadOnly property. Well, I'm trying. I keep getting The file could not be written to disk. I'll try to post it again later.

I was going to say you could use the AddHandler Statement BUT if you follow the example given by tinstaafi you don't need to - the custom class is always set to read only and always has a white background.

I'll give you an example of using addhandler on a control if you want, but you could also just append Handles Event to the sub.

'Example of setting a sub routine to handle an event with addhandler:

sub HandleMyEvent1(byval sender as object, byval e as system.eventArgs)
    msgbox("You clicked me")
end sub

dim mycustomControl as new CustomControl 

me.controls.add(mycustomControl)

addhandler mycustomControl.OnClick, AddressOf HandleMyEvent1

'Example of setting sub routine with handles
dim MyControl2 as New CustomControl
me.controls.add(MyControl2)

sub HandleMyEvent2(byval sender as object, byval e as system.eventArgs) Handles MyControl2.MouseOver
    msgbox ("Mouse Over me")
End Sub

Sorry, if you want to use the override a behaviour of the base class on your custom class you would do it like this:

Public class DataItem

private _Data as Object
private _Display as string

Public Property Data As Object
     Get()
        return _Data
    end Get 
    Set (byref value as Object)
        _Data = Value
    end Set
End Property
Public Property Display as String
    Get() 
        return _Display
    end Get
    Set (byref value as Object)
    _Display = value
    end Set
End Property
Public Overrides Function ToString()
    Return _Display
End Function
Public Sub New (Optional ByVal Key as String ="", Optional ByVal Item as Object = nothing)
    _Data = Item
    _Display = Key
End Sub
End Class

You could try something like this:

Public Class CustomTextBox
    Inherits TextBox
    Public Sub New()
        [ReadOnly] = True
        BackColor = Color.White
    End Sub
    Private Overloads Sub Me_ReadOnlyChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.ReadOnlyChanged
        [ReadOnly] = True
    End Sub
End Class

This will force the textbox to have readonly always true. This could easily be adapted to accomplish different things. Using it would be the same. Simply add this control wherever you want readonly and use the regular textbox wherever you don't.

I Have several texaboxs on single form and will be so in other forms also .Frequently i change the readonly value of textboxes to false for editing except few and put back to reodonly when record is saved in database where the back color change to grey. To avoide grey color i have to write below code for each and every text box on form.

Private Sub TextBox1_ReadOnlyChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.ReadOnlyChanged
If TextBox1.ReadOnly = True Then
TextBox1.BackColor = Color.White
End If
End Sub

if i manage to put this code in custom textbox then i dont need to write this for each and every text box as custom text box will take care of back color when readonly property is set to true.

my custom textbox class code is below

Public Class TextBoxWhiteBackNew
Inherits TextBox
Protected Overrides Sub OnGotFocus(ByVal e As System.EventArgs)
If Me.ReadOnly <> True Then
Me.BackColor = Color.Lime
End If
MyBase.OnGotFocus(e)
End Sub

Protected Overrides Sub OnLostFocus(ByVal e As System.EventArgs)
Me.BackColor = Color.White
MyBase.OnLostFocus(e)
End Sub

'But below dosent work

'Protected Overrides ReadOnlyChanged(ByVal sender As Object, ByVal e As System.EventArgs)
'    If Me.ReadOnly = True Then
'        Me.BackColor = Color.White
'    End If
'End Sub
'End Class

With my class changed like this it works:

Public Class TextBoxWhiteBackNew
    Inherits TextBox
    Public Sub New()
        BackColor = Color.White
    End Sub
    Private Overloads Sub Me_ReadOnlyChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.ReadOnlyChanged
        BackColor = Color.White
    End Sub
End Class

You can change the ReadOnly property at will and the BackColor stays White.

i think i got it in test . let me try in my project

thanks Tinstaafl

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.