保存设置的代码在一个vb.net项目中不起作用。

huangapple go评论62阅读模式
英文:

The save settings code does not work in a project vb.net

问题

I'm working on a project with text boxes
And I have a command button to change the font type, color and size
I want the correct way in code to save these settings after exiting the program
Even if I open the project again, it will still be the same font type and color as the one in the text boxes
Thanks to all

This is a picture of the font and color settings 1

Private Sub MyLibrary_FormClosed(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.FormClosed
    My.Settings.Text_Color = TxtBook.ForeColor
    My.Settings.Text_Font = TxtBook.Font
    My.Settings.Save()
End Sub

Private Sub Text_Font()
    TxtBook.Font = My.Settings.Text_Font
    TxtAuthor.Font = My.Settings.Text_Font
    TxtPublisher.Font = My.Settings.Text_Font
    TxtPublish.Font = My.Settings.Text_Font
    txtPage.Font = My.Settings.Text_Font
    TxtNote.Font = My.Settings.Text_Font
    TxtBook.ForeColor = My.Settings.Text_Color
    TxtAuthor.ForeColor = My.Settings.Text_Color
    TxtPublisher.ForeColor = My.Settings.Text_Color
    TxtPublish.ForeColor = My.Settings.Text_Color
    txtPage.ForeColor = My.Settings.Text_Color
    TxtNote.ForeColor = My.Settings.Text_Color
End Sub
英文:

I'm working on a project with text boxes
And I have a command button to change the font type, color and size
I want the correct way in code to save these settings after exiting the program
Even if I open the project again, it will still be the same font type and color as the one in the text boxes
Thanks to all

This is a picture of the font and color settings 1

Private Sub MyLibrary_FormClosed(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.FormClosed
    My.Settings.Text_Color = TxtBook.ForeColor
    My.Settings.Text_Font = TxtBook.Font
    My.Settings.Save()
End Sub
Private Sub Text_Font()
TxtBook.Font = My.Settings.Text_Font
    TxtAuthor.Font = My.Settings.Text_Font
    TxtPublisher.Font = My.Settings.Text_Font
    TxtPublish.Font = My.Settings.Text_Font
    txtPage.Font = My.Settings.Text_Font
    TxtNote.Font = My.Settings.Text_Font
 TxtBook.ForeColor = My.Settings.Text_Color
    TxtAuthor.ForeColor = My.Settings.Text_Color
    TxtPublisher.ForeColor = My.Settings.Text_Color
    TxtPublish.ForeColor = My.Settings.Text_Color
    txtPage.ForeColor = My.Settings.Text_Color
    TxtNote.ForeColor = My.Settings.Text_Color
End Sub

答案1

得分: 3

以下是您要求的代码部分的中文翻译:

你现在的代码毫无意义。这就是为什么在编写代码之前需要清晰了解你要实现的逻辑的原因。然后,当事情不按预期工作时,你可以将代码与逻辑进行比较。让我们逐步了解你的代码。

当窗体关闭时,你这样做:

Text_Font()

在这个方法中,你这样做:

Me.TxtBook.Font = Me.setting.Text_Font
'...
Me.TxtBook.ForeColor = Me.setting.Text_Color
'...

你根据当前设置的值修改了该控件。然后你这样做:

Me.setting.Text_Color = TxtBook.ForeColor
Me.setting.Text_Font = TxtBook.Font

你用控件的值更新了设置...而这些值刚刚从设置中获取!这有什么意义呢?在你的代码中没有显示你在其他地方更改了设置或控件,所以实际上什么都不应该改变。

我认为逻辑上的正确做法应该是在用户选择时更新设置。如何更新取决于你。用户选择字体和/或颜色,然后你更新设置,再从设置中更新控件。你可以在设置它们时显式调用Save来保存设置,或者可以让应用程序在关闭时自动保存它们,这是默认行为。

实际上,我刚刚意识到你在所有地方都使用Me.setting而不是My.Settings。我不知道这是否是一个拼写错误,但是,如果你希望修改标准应用程序设置,那么你需要使用My.Settings,例如:

Me.TxtBook.Font = My.Settings.Text_Font

总的来说,你可以考虑类似以下的方式:

Private Sub UpdateFontSettings()
    '在此处更新My.Settings的值。

    My.Settings.Save()

    UpdateControlFonts()
End Sub

Private Sub UpdateControlFonts()
    '在此处从设置更新控件属性。
End Sub

当窗体加载时,你可以调用UpdateControlFonts。当用户进行更改时,你可以调用UpdateFontSettings。在关闭时,你不必做任何操作。

英文:

The code you have makes little sense as it is. This is why you need to have a clear understanding of the logic you're trying to implement before writing code. You can then compare the code to the logic whenever things don't work as expected. Let's step through the code you have.

When the form is closed, you do this:

Text_Font()

In that method, you do this:

Me.TxtBook.Font = Me.setting.Text_Font
'...
Me.TxtBook.ForeColor = Me.setting.Text_Color
'...

You're modifying that control based on the current value of the settings. You then do this:

Me.setting.Text_Color = TxtBook.ForeColor
Me.setting.Text_Font = TxtBook.Font

You're updating the settings with the values from the control... which you just got from the settings! How does that make sense? You haven't shown us anywhere else in your code that you're changing either the settings or the controls, so how is anything actually supposed to change?

I would think that the logical thing to do would be to update the settings somewhere when the user chooses to. How you do that is up to you. The user chooses the font and/or colour and you update the settings and then update the controls from the setting. You can save the settings when you set them, by explicitly calling Save, or you can just let the application save them automatically at shutdown, which is the default behaviour.

Actually I just realised that you're using Me.setting everywhere rather than My.Settings. I don;t know whether that's a typo but, if you expect to modify standard application settings then you need to use My.Settings, e.g.

Me.TxtBook.Font = My.Settings.Text_Font

Overall, you might consider something like this:

Private Sub UpdateFontSettings()
    'Update My.Settings values here.

    My.Settings.Save()

    UpdateControlFonts()
End Sub

Private Sub UpdateControlFonts()
    'Update control properties from settings here.
End Sub

When the form loads, you would call UpdateControlFonts. When the user makes changes, you would call UpdateFontSettings. At shutdown, you don't have to do anything.

huangapple
  • 本文由 发表于 2023年4月4日 09:32:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75924856.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定