Doing What I Used to Do in VBA: Cycling Through Controls and Assigning Values Based on Field Names in C# - Possible?

13 Views Asked by At

I've been using MSACCESS and VBA for a few years, but I'm getting excited about C#, WPF and EF 7. I really like C# constructs, they're simple even for me. There are some things, however, that seem to become necessarily difficult. I'll give you an example: in VBA I open a recordset to read the data and, if I've given the controls names similar to those of the fields, I can easily cycle through the controls and assign values.


    Dim db As DAO.Database, rs As DAO.Recordset, ctrl As Control
    Set db = CurrentDb
    Set rs = db.OpenRecordset("dbo_label", dbOpenDynaset)
    rs.MoveLast: rs.MoveFirst
    rs.FindFirst "[id_eti]='" & Me.Search & "'"
    If rs.NoMatch = False Then
        For Each ctrl In Me.Controls
            If Left(ctrl.Name, 3) = "txt" Or Left(ctrl.Name, 3) = "cc_" Or Left(ctrl.Name, 3) = "img" Then
                ctrl = rs(Mid(ctrl.Name, 4))
            End If
        Next
    Else
        MsgBox "No match for " & Me.Search, vbExclamation, "Warning"
    End If
    rs.Close
    Set rs = Nothing: Set db = Nothing

As you can see, I can cycle through the controls (and I do this in C# too), assigning each control the value contained in the field, looking for it by the field name. In c#, EF7 generates a class to access the table, but I haven't found a simple way to access the data using the field name. Something like: myTxt.Text == myClass[nameoffield]; where nameoffield is a string variable. Also, you will see that the field value is assigned to the control regardless of the type of control: in my case they are TextBox, CheckBox and Image controls. Can I hope for a future update or not?

0

There are 0 best solutions below