locked
Change background color on any control where data changed RRS feed

  • Question

  • Is there a way to avoid having to reference textbox controls separately in order to change the background color?

    Here is an example of a project I'm currently developing:

    There will be many more textboxes and many more tabs by the time the project is finished.

    If data in a textbox is changed, the background color changes to cyan, as seen above.  I found some code snippets here and here that allow the background color change.

    This is the code currently:

            Private Sub tblClientDemographicDetail_Activated()
                AddHandler Me.FindControl("ClientNumber").ControlAvailable, AddressOf TextBoxAvailable
                AddHandler Me.FindControl("RWNumber").ControlAvailable, AddressOf TextBoxAvailable
            End Sub
    
            Private Sub TextBoxAvailable(sender As Object, e As ControlAvailableEventArgs)
                AddHandler CType(e.Control, System.Windows.Controls.TextBox).KeyUp, AddressOf TextBoxKeyUp
            End Sub
    
            Private Sub TextBoxKeyUp(sender As Object, e As System.Windows.RoutedEventArgs)
                CType(sender, System.Windows.Controls.Control).Background =
                        New System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Cyan)
            End Sub
    

    To avoid having to type in "AddHandler Me.FindControl(<textbox>) " for each and every textbox, is there a way to just detect if any changes in data were made?


    Thanks.

    Tuesday, January 14, 2014 10:36 PM

Answers

All replies

  • Not possible?

    Thanks.

    Thursday, January 16, 2014 2:16 PM
  • There is a way to loop through all controls here:

    http://social.msdn.microsoft.com/Forums/vstudio/en-US/65b146d3-2bee-44ed-a4e6-e54383627756/is-there-a-way-to-programatically-browse-all-controls-on-a-screen?forum=lightswitch#21b4686b-369a-4a80-b98f-d0ae3336877f

    After implementing Patryks FindControls extension your code would look something like this:

    For Each control in me.FindControls()
    
            AddHandler Me.FindControl(control.name).ControlAvailable, AddressOf TextBoxAvailable
    
    Next

    or maybe this would work, not sure:

    For Each control in me.FindControls()
        AddHandler control.ControlAvailable, AddressOf TextBoxAvailable
    Next

    You might want to add a check for typeof textbox.

    HTH,

    Josh


    • Edited by joshbooker Thursday, January 16, 2014 4:25 PM
    • Marked as answer by db_dweeb Thursday, January 16, 2014 6:06 PM
    • Unmarked as answer by db_dweeb Friday, January 17, 2014 3:08 PM
    • Marked as answer by db_dweeb Friday, January 17, 2014 3:22 PM
    Thursday, January 16, 2014 4:11 PM
  • Thanks.  I'm just beginning with Lightswitch development.  Creating an extension appears to be some heavy coding that I'm not sure I'm comfortable dealing with just yet.   I found this on MSDN.  I assume that would be a good place to start learning?

    Thanks.

    Thursday, January 16, 2014 5:10 PM
  • I don't think it's very complicated.  I think you need to simply paste the code (Partyk's at the end of the thread) in the right place but where?  I asked in the thread above.
    Thursday, January 16, 2014 5:31 PM
  • I see the word "extension" in Patryk's code so I assume I have to create an extension as suggested by the MSDN site I referred to above.  If my assumption is correct, then I have many more questions before I attempt it.

    I'll keep an eye on your other thread and hope you get an answer soon.

     


    Thanks.



    • Edited by db_dweeb Thursday, January 16, 2014 6:06 PM
    Thursday, January 16, 2014 5:53 PM
  • Hey guys!

    Don't worry, Patryk was referring to .NET extension methods, not Visual Studio extensions.  You can put the code anywhere in the Silverlight client project.

    Take care!

    Jan


    It's your story - time to switch on the innovation.||About me||LightSwitch blog

    Thursday, January 16, 2014 7:33 PM
  • In Solution Explorer Right Click on Desktop Client Project then choose

    Add | New | Module

    Name it ScreenObjectExtensions

    Paste the following into the file ScreenObjectExtensions.vb

    Namespace LightSwitchApplication
    
        Module ScreenObjectExtensions
    <system.runtime.compilerservices.extension()>
            Public Function FindControls(screen As Microsoft.LightSwitch.Client.IScreenObject) As IEnumerable(Of IContentItemProxy)
                Dim model = screen.Details.GetModel()
                ' recursion, but screens are not that complex
                Return model.GetChildItems().SelectManyRecursive(Function(c) c.GetChildItems()).OfType(Of Microsoft.LightSwitch.Model.IContentItemDefinition)().[Select](Function(c) screen.FindControl(c.Name))
            End Function
    
            <system.runtime.compilerservices.extension()>
            Public Iterator Function SelectManyRecursive(Of T)(source As IEnumerable(Of T), selector As Func(Of T, IEnumerable(Of T))) As IEnumerable(Of T)
                For Each item As T In source
                    Yield item
    
                    Dim children = selector(item)
                    If children Is Nothing Then
                        Continue For
                    End If
                    For Each descendant As T In children.SelectManyRecursive(selector)
                        Yield descendant
                    Next
                Next
            End Function
    
        End Module
    
    End Namespace       

    Now in your screen code you can do this:

    Private Sub someScreen_Activated()
                For Each ctrl In Me.FindControls()
                    AddHandler ctrl.ControlAvailable, AddressOf TextBoxAvailable
                Next
            End Sub
    
            Private Sub TextBoxAvailable(sender As Object, e As ControlAvailableEventArgs)
                On Error Resume Next

    AddHandler CType(e.Control, System.Windows.Controls.TextBox).GotFocus, AddressOf TextBoxKeyUp End Sub Private Sub TextBoxKeyUp(sender As Object, e As System.Windows.RoutedEventArgs) CType(sender, System.Windows.Controls.Control).Background = New System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Cyan) End Sub

    HTH,

    Josh



    • Proposed as answer by joshbooker Friday, January 17, 2014 2:49 AM
    • Edited by joshbooker Friday, January 17, 2014 2:51 AM
    Friday, January 17, 2014 2:47 AM
  • (I'm going to unmark your first answer for now)

    I tried it again but get the same errors.  The second function appears to be the culprit.


    Thanks.


    • Edited by db_dweeb Friday, January 17, 2014 3:09 PM
    Friday, January 17, 2014 3:04 PM
  • What version of visual studio are you using?

    I believe this code will work on VS2012+ since the Iterator and Yeild keywords were added to VB.net in 2012.  For LS2011 you'd have to rework the code to loop differently.

    You're best bet would be to upgrade to VS2013.

    Josh

    • Edited by joshbooker Friday, January 17, 2014 3:17 PM
    Friday, January 17, 2014 3:09 PM
  • Ah-ha, I'm using VS2010.  I am testing 2013 and like what I see so far.  I'll try this in 2013. 

    Thanks so much.


    Thanks.

    Friday, January 17, 2014 3:22 PM