首页 > 解决方案 > 为什么 My.Settings 没有保存?

问题描述

我在My.Settingsnamed中有一个设置Categories。所以在一个模块里面我有一个公共子运行这个代码:

Public Sub DeclareCategories()
    Dim currentCategory As String
    '   GET CURRENT CATEGORIES
    currentCategory = My.Settings.Categories

    '   ADD THE CATEGORY
    My.Settings.Categories = currentCategory & frmNewCategory.txtCategoryName.Text & ","
    My.Settings.Save()
End Sub

然后,用户将My.Settings.Categories在单击该表单中的按钮时更新该值frmNewCategory。这是按钮的代码:

If txtCategoryName.Text <> "" Then
    Try
        DeclareCategories()
        MsgBox("Successfully added the new category!")
    Catch ex As Exception
        MsgBox(ex.Message, MsgBoxStyle.Critical)
    End Try

Else
    MsgBox("Please enter a category name before adding.", MsgBoxStyle.Critical)

End If

由于某种原因,我的设置没有保存。Categories也是一个字符串。我什至尝试删除所有currentCategory东西并保留:

My.Settings.Categories = frmNewCategory.txtCategoryName.Text
My.Settings.Save()

但它仍然没有保存设置。任何建议将不胜感激,谢谢。

标签: vb.netwinforms

解决方案


提醒 My.Settings 仅保存在保存它们的计算机上。示例:您的类别设置将保存在您计算机上的文件中。

如果您将设置保存在“类别”中并将可执行应用程序放在另一台计算机上,则“类别”设置将为空。

此外,检查字符串currentCategory & frmNewCategory.txtCategoryName.Text以确保字符串实际上不是空的。


推荐阅读