How do I move / repeat all objects inside a form on VB.net?

I have a VB class inside which one of the methods takes an array of forms.

For each form inside the array, I need it to go through all the objects, check if they are a specific tyoe (input, label, flag, etc.) and get the properties of each object. Then I want to dump them into a text file in the following format:

Form1 | Label1 | "Enter your name"

"Enter your name", which is the caption or text of the form object.

I want to do this to facilitate the translation of the application. Any ideas or thoughts you may have about this?

+3
source share
4 answers

.Controls collection. , .Controls, . , parent-child, .

Case, Type Of . .

+1

IEnumerable(Of Control), . .

Public Function GetAllControls(ByVal source as Control) As IEnumerable(Of Control)
  Dim seq = Enumerable.Empty(Of Control)
  For Each child in source.Controls 
    if child.Controls.Count > 0 Then
      seq = seq.Concat(GetAllControls(child))
    End If
  Next 
  Return seq
End Function
+3

If all you want to do is to be able to localize your form, it would be much easier to set the Localizable property form to true. This will create a .resx file containing all the values ​​of various properties for all form controls, to texts, etc. Could be translated and distributed as a separate assembly of satellites.

+1
source
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ProcessForm(Me)
    End Sub

    Private Sub ProcessForm(ByVal frm As Form)
        For Each el As Control In frm.Controls
            Dim str As String
            str = String.Format("{0} | {1} | {2}", frm.Name.ToString(), el.Name.ToString(), el.Text.ToString())
            Debug.Print(str)
        Next
    End Sub
End Class
+1
source

Source: https://habr.com/ru/post/1733400/


All Articles