Dailycode.info

Short solution for short problems

Read and write windows forms textboxes into DTO

Without using any kind of existing binding protocols, these functions will fill the controls with the corresponding fields from a class (DTO in this case).

The most important requirement in this case is that you name the textfields like your DTO properties. you can use a random prefix for the text fields. Contain the text fields in a panel or group box. Then for eg. each group box use a different container. In my case I had several tab pages, for each tab page I use a prefix txt1 up to txt5. So a text field is called txt1CREATED or ...

The first function is to Fill the fields on the form. You provide a DTO class, a container and a number that is equal to the number of prefix characters.

    Private Sub FillDataFieldsOnForm(Of T)(ByVal DTO As T, ByVal collection As Control.ControlCollection, ByVal i As Short)

        Dim myType As Type = GetType(T)

 

        For Each txt In collection

            If (TypeOf txt Is DevExpress.XtraEditors.TextEdit) Then

                Try

                    Dim name As String = DirectCast(txt, DevExpress.XtraEditors.TextEdit).Name.Substring(i)

                    Dim myProp As PropertyInfo = myType.GetProperty(name)

 

                    DirectCast(txt, DevExpress.XtraEditors.TextEdit).EditValue = myProp.GetValue(DTO, Nothing)

                Catch ex As Exception

                    DirectCast(txt, DevExpress.XtraEditors.TextEdit).EditValue = ex.Message

                End Try

            End If

        Next txt

 

    End Sub

Here you can see an implementation of this function:

FillDataFieldsOnForm(kKna1, grpHeader.Controls, 3)

Now when you want to read the controls, its equally easy:

ReadDataFieldsOnForm(Of KNB1)(kKnb1, grpCustomerMasterData, 4)

The function for to do this is:

    Private Sub ReadDataFieldsOnForm(Of T)(ByVal DTO As T, ByVal grp As DevExpress.XtraEditors.GroupControl, ByVal i As Short)

        Dim myType As Type = GetType(T)

 

        For Each txt In grp.Controls

            If (TypeOf txt Is DevExpress.XtraEditors.TextEdit) Then

                Try

                    Dim name As String = DirectCast(txt, DevExpress.XtraEditors.TextEdit).Name.Substring(i)

                    Dim myProp As PropertyInfo = myType.GetProperty(name)

 

                    myProp.SetValue(DTO, DirectCast(txt, DevExpress.XtraEditors.TextEdit).EditValue, Nothing)

                Catch ex As Exception

                End Try

            End If

        Next txt

 

    End Sub


Grabbing the image from AXIS (P1346) Network Camera

Thanks to the wonderful API this camera has, you do not need DLL's or Active X controls. You can just get an image for example by the API.

Here an example to get the image from the camera using c# (this can be used in a service, web service or other applications):

Uri test = new Uri("http://192.0.0.100/jpg/image.jpg");

WebClient wc = new WebClient();

wc.Credentials = new NetworkCredential("MyUser", "MyPassW0rd");

wc.DownloadFile(test, @"C:\TakePicture\picFromWS.jpg");

wc.Dispose();