In MS Access you can set the background to a specific colour when the field has the focus. This is done through "conditional formatting", using the "Field has Focus" feature.

How is that done in VB.NET?

Recommended Answers

All 5 Replies

Just some sample code for you, edit as you wish (I think this is right from what I understand from your post)

Protected Sub GridView1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.GridItemEventArgs)
           'Is it a GridDataItem
           If (TypeOf (e.Item) Is GridDataItem) Then
               'Get the instance of the right type
               Dim dataBoundItem As GridDataItem = e.Item

               'Check the formatting condition
               If (Integer.Parse(dataBoundItem( "Size").Text) > GridItemType.Footer) Then
                  dataBoundItem( "Received").ForeColor = Color.Red
                  dataBoundItem( "Received").Font.Bold = True
                    'Customize more...
               End If
           End If

Tnanks Jordan. But this is still somewhat too advanced for me.

This on the other hand seems to do the trick (in a maskedtextbox).

Public Class txtBase
    Inherits System.Windows.Forms.MaskedTextBox
    Dim nSaveColor As Color

    Private Sub txtBase_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.GotFocus
        Me.nSaveColor = Me.BackColor
        Me.BackColor = Color.Turquoise
    End Sub

    Private Sub txtBase_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LostFocus
        Me.BackColor = Me.nSaveColor
    End Sub
End Class

Well.... it seems to work. I'll wait some time before I set this thread as "solved". What do you think?

Are you trying to do it to a GridView or just a simple textbox?

- Jordan

Just a simple textbox (at this time).

If you're trying to do it conditionally

If blah = true then
textbox1.focus()
textbox1.backcolor = drawing.color.azure
endif

or are you trying to get the textbox to change colour when you click inside it?

Also, I'd recommend reading this to gain a better understanding of what you're trying to accomplish :)

http://www.dotnetspider.com/resources/19980-Custom-OnFocus-Textbox-Control.aspx

- Jordan

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.