Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 如何在以前执行的基础上加载表单?_Vb.net - Fatal编程技术网

Vb.net 如何在以前执行的基础上加载表单?

Vb.net 如何在以前执行的基础上加载表单?,vb.net,Vb.net,我在Vb.net中制作了一个表单,用户可以在其中进行一些更改。我希望用户所做的更改在每次执行时都保持不变 例如,我在执行时对字体应用了一些效果- Private Sub Checkbox1_CheckedChanged () If CheckBox1.Checked = True then Label1.Font = New Drawing Style ("Comic Sans Ms", 16, FontStyle.Bold) End If End Sub 用户所做

我在Vb.net中制作了一个表单,用户可以在其中进行一些更改。我希望用户所做的更改在每次执行时都保持不变

例如,我在执行时对字体应用了一些效果-

Private Sub Checkbox1_CheckedChanged ()
   If CheckBox1.Checked = True then
        Label1.Font = New Drawing Style ("Comic Sans Ms", 16, FontStyle.Bold)
   End If
End Sub
用户所做的更改应在下次执行程序时可见。

可能吗


提前谢谢

是的,您可以为此使用外部Css文件

首先,创建项目的外部Css文件并指定一些默认样式

喜欢你的Css文件吗

.Fstyle {
    font-family: Comic Sans Ms;
    }
还有你的aspx

<asp:label id="lblname" runat="server" class="Fstyle">

第二,您可以通过用户所做的更改重新写入此文件
保存对变量的更改,并使用vb.net的IO函数覆盖此文件。更改将永久保留。

您有两个选项,如果您只需要进行一些设置,我将使用内置选项,您可以在项目属性中创建一个设置。这是您的字体示例

然后你会像这样使用它

Private Sub Checkbox1_CheckedChanged() Handles CheckBox1.CheckedChanged
    If CheckBox1.Checked = True Then
        Label1.Font = New Font("Comic Sans Ms", 16, FontStyle.Bold)
        My.Settings.MyNewFont = Label1.Font
        My.Settings.Save()
    End If
End Sub

Public Sub New()

    ' This call is required by the designer.
    InitializeComponent()
    If Not IsNothing(My.Settings.MyNewFont) Then Label1.Font = My.Settings.MyNewFont

End Sub

答案是我将如何做,除了如果您希望存储对多个不同控件的更改,您最好循环使用它们,检查表单关闭时的设置,并将它们存储在
System.Collections.Specialized.StringCollection
中的
My.settings
中,然后您可以循环使用它集合,并按原样设置设置

让我知道如果你想要一个例子,我可以提供它

例如:

注意:我总是发现,在添加System.Collections.Specialized.StringCollection时,您需要手动添加一个空字符串(然后可以将其删除),以强制它创建.xml(这可能是一个更为技术性的解决方案,但它对我有效)

我创建了一个名为
FormLoadData
为了确定要为哪些控件存储数据,我在它们的名称前加上了“UC_”前缀,如果它是表单上的所有控件,或者全部在一个groupbox中,等等,那么您可以跳过此检查

然后添加以下表单结束子项:

 Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    'clear collection
    My.Settings.FormLoadData.Clear()
    'loop through each control on the form
    For Each Con As Control In Me.Controls
        'run check to see if it is a control to store changes for
        If Con.Name.Substring(0, 3) = "UC_" Then
            'load any settings you wish to store in the collection starting with the control ID
            My.Settings.FormLoadData.Add(Con.Name & "," & Con.Font.ToString)
        End If
    Next
End Sub
在新的

 Public Sub New()

    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.
    'loop through controls
    For Each Con As Control In Me.Controls
        'loop through settings to compare
        For Each item In My.Settings.FormLoadData
            'check stored name against control name
            If Con.Name = item.Split(",").ElementAt(0) Then
                'update settings 
                Con.Font = New Font(item.Split(",").ElementAt(1), Con.Font.Size)
                MsgBox(item)
                Exit For
            End If
        Next item
    Next Con
End Sub
当然,有一种更好的方法来实现这一点,而不是通过创建的集合进行循环,但除非您为数千个控件存储数据,否则这将正常工作


我的浏览器正在播放,所以我无法检查这是什么样子,但希望它是清楚的

存储更改并在表单加载时检索和应用?将更改存储在db或appconfig中,并签入表单加载如何存储更改!是的。。这就是我想知道的。这个评论是为bansi写的。我刚刚试过这个,效果非常好!现在,我也知道如何存储多个数据!非常感谢。请提供一个例子。我犯了一个错误。我知道我迟到了,但如果你自愿提供一个例子,我将非常有帮助@KashishArora我用一个例子更新了答案。