0

Basically I have a few hundred controls that on LostFocus need to update different variables located in a class.

Private Sub txtFirstName_LostFocus(sender As Object, e As System.EventArgs) Handles txtFirstNameMain.LostFocus
    Interview.FIRSTNAME = txtFirstNameMain.Text
End Sub
Private Sub txtLastName_LostFocus(sender As Object, e As System.EventArgs) Handles txtLastNameMain.LostFocus
    Interview.LASTNAME = txtLastNameMain.Text
End Sub

Is there a way to create a single Sub that can handle this? Maybe I can put the target variable on the tag of the control? But then how would I reference said variable without a lot of code work?

Private Sub Lost_Focus_UpdateClass(sender As TextBox, e As System.EventArgs) Handles txtFirstNameMain.LostFocus, txtLastNameMain.LostFocus
    Select Case sender.Tag
        Case "FIRSTNAME"
            Interview.FIRSTNAME = sender.Text
        Case "LASTNAME"
            Interview.LASTNAME = sender.Text
    End Select
End Sub

This just feels like it takes too much for what it does. Is there a better way to handle this?

I'm using WinForms.

EDIT: TnTinMn - Worked. Had to make an alteration for null-able fields.

    Me.tbFirstName.DataBindings.Add("Text", dataStore, "FirstName", True, DataSourceUpdateMode.OnValidation, String.Empty)
    Me.tbLastName.DataBindings.Add("Text", dataStore, "LastName", True, DataSourceUpdateMode.OnValidation, String.Empty)
1
  • You could use the Validation events. Among advantages would be that the code would be all in one place rather than in bits all over the form. You'd be able to UnDo or Cancel because none of the props would be set/transferred until the user says they are done. Commented Feb 3, 2016 at 15:09

1 Answer 1

1

Based on your described goals, data binding the control to the class properties should work.

Public Class Form1
    Private dataStore As Example
    Protected Overrides Sub OnLoad(e As EventArgs)
        MyBase.OnLoad(e)
        Me.SetupDataStore()
    End Sub

    Private Sub SetupDataStore()
        Me.dataStore = New Example
        Me.tbFirstName.DataBindings.Add("Text", dataStore, "FirstName", True, DataSourceUpdateMode.OnValidation)
        Me.tbLastName.DataBindings.Add("Text", dataStore, "LastName", True, DataSourceUpdateMode.OnValidation)
    End Sub

End Class

Friend Class Example
    Public Property FirstName As String
    Public Property LastName As String
End Class
Sign up to request clarification or add additional context in comments.

2 Comments

This looks like the best answer. Now I have to redo all the code I just wrote. ^-^ - Edit: If I have two textboxes bound to the same DataBinding will they both update to the new value when leaving one?
Ok, so I got all that working. Now here is a question, if a Date can be Nullable, how do I get it to move past the box when it's empty? It's "", but should be Nothing, setting it to nothing isn't working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.