• 欢迎光临~

遍历保存软件参数

开发技术 开发技术 2022-11-17 次浏览
 Dim ParameterPath As String = Application.StartupPath & "FrmLog"

    '遍历保存 textBox、ComboBox参数
    Public Function SaveForEachTextBox(ByVal Form As System.Windows.Forms.Control, Optional Msg As String = "") As Boolean

        Try

            '定义键值对
            Dim Dictionary As New Dictionary(Of String, String)()

            For Each TextBox As Control In Form.Controls
                If TypeOf (TextBox) Is Panel Or TypeOf (TextBox) Is GroupBox Then
                    For Each TextBox1 As Control In TextBox.Controls
                        If TypeOf (TextBox1) Is Panel Or TypeOf (TextBox1) Is GroupBox Then
                            For Each TextBox2 As Control In TextBox1.Controls
                                If TypeOf (TextBox2) Is TextBox Or TypeOf (TextBox2) Is ComboBox Then
                                    Dictionary.Add(TextBox2.Name, TextBox2.Text)
                                End If
                            Next
                        Else
                            If TypeOf (TextBox1) Is TextBox Or TypeOf (TextBox1) Is ComboBox Then
                                Dictionary.Add(TextBox1.Name, TextBox1.Text)
                            End If
                        End If
                    Next
                Else
                    If TypeOf (TextBox) Is TextBox Or TypeOf (TextBox) Is ComboBox Then
                        Dictionary.Add(TextBox.Name, TextBox.Text)
                    End If
                End If
            Next


            Dim TextBoxJsonParameter As String = JsonConvert.SerializeObject(Dictionary, Formatting.Indented)

            Dim Path As String = ParameterPath & Form.Name & "TextBoxPrarmeter" & ".txt"

            If Not Directory.Exists(ParameterPath) Then Directory.CreateDirectory(ParameterPath)

            File.WriteAllText(Path, TextBoxJsonParameter)

        Catch ex As Exception
            Msg = "保存参数失败!" & ex.Message
            Return False
        End Try
        Msg = "保存参数成功!"
        Return True

    End Function

    '遍历保存窗体控件Txet参数
    Public Sub SaveFrmParameter(ByVal Form As System.Windows.Forms.Control)

        Try

            '定义键值对
            Dim Dictionary As New Dictionary(Of String, String)()

            For Each TextBox As Control In Form.Controls

                Dictionary.Add(TextBox.Name, TextBox.Text)

            Next

            Dim TextBoxJsonParameter As String = JsonConvert.SerializeObject(Dictionary, Formatting.Indented)

            Dim Path As String = ParameterPath & Form.Name & "TextBoxPrarmeter" & ".txt"

            If Not Directory.Exists(ParameterPath) Then Directory.CreateDirectory(Path)


            File.AppendAllText(Path, Now.ToString & vbCrLf & TextBoxJsonParameter & vbCrLf)

        Catch ex As Exception
           
        End Try


    End Sub




    ''' <summary>
    ''' 加载TextBox参数
    ''' </summary>
    ''' <param name="Form"></param>
    Public Function GetForEachTextBox(ByVal Form As System.Windows.Forms.Control, Optional Msg As String = "") As Boolean

        Try

            Dim Path As String = ParameterPath & Form.Name & "TextBoxPrarmeter" & ".txt"

            If Not File.Exists(Path) Then
                Msg = "无法加载参数!参数文件不存在! " & Path
                Return False
            End If

            Dim TextBoxJsonParameter As String = File.ReadAllText(Path)

            Dim Dictionary = JsonConvert.DeserializeObject(TextBoxJsonParameter)

            For Each TextBox As Control In Form.Controls

                If TypeOf (TextBox) Is Panel Or TypeOf (TextBox) Is GroupBox Then
                    For Each TextBox1 As Control In TextBox.Controls
                        If TypeOf (TextBox1) Is Panel Or TypeOf (TextBox1) Is GroupBox Then
                            For Each TextBox2 As Control In TextBox1.Controls
                                If TypeOf (TextBox2) Is TextBox Or TypeOf (TextBox2) Is ComboBox Then
                                    TextBox2.Text = Dictionary(TextBox2.Name)
                                End If
                            Next
                        Else
                            If TypeOf (TextBox1) Is TextBox Or TypeOf (TextBox1) Is ComboBox Then
                                TextBox1.Text = Dictionary(TextBox1.Name)
                            End If
                        End If
                    Next
                Else
                    If TypeOf (TextBox) Is TextBox Or TypeOf (TextBox) Is ComboBox Then
                        TextBox.Text = Dictionary(TextBox.Name)
                    End If
                End If

            Next

        Catch ex As Exception

            Msg = "加载参数失败! " & ex.Message
            Return False
        End Try
        Return True
    End Function

  

程序员灯塔
转载请注明原文链接:遍历保存软件参数
喜欢 (0)
违法和不良信息举报电话:022-22558618 举报邮箱:dljd@tidljd.com