重写 Windows 窗体中的设置

发布于 2024-12-22 15:00:03 字数 1171 浏览 0 评论 0原文

我已在应用程序设置中设置了表单文本的名称,我需要重写它。是否有可能,因为它向我显示它只是一个只读。

我知道我们可以使用 me.text="" 简单地更改它 但我的应用程序有一个问题,因为我有下面的代码,其中每次加载表单时都会删除文本。

Protected Overrides Sub OnLayout(ByVal e As System.Windows.Forms.LayoutEventArgs)

        MyBase.OnLayout(e)
        'Me.Text = CStr(Val(Me.Text) + 1)
        FillList()

        MyBase.OnLayout(e)
        If FontColor.Items.Count = 0 Then
            FontColor.Items.AddRange(Known_Color)
            FontColor.MaxDropDownItems = 20
        End If


        MyBase.OnLayout(e)
        If OutlineColor.Items.Count = 0 Then
            OutlineColor.Items.AddRange(Known_Color)
            OutlineColor.MaxDropDownItems = 20
        End If



        MyBase.OnLayout(e)
        If BorderColor.Items.Count = 0 Then
            BorderColor.Items.AddRange(Known_Color)
            BorderColor.MaxDropDownItems = 20
        End If


        MyBase.OnLayout(e)
        If BackgroundColor.Items.Count = 0 Then
            BackgroundColor.Items.AddRange(Known_Color)
            BackgroundColor.MaxDropDownItems = 20
        End If
    End Sub

有什么解决方法可以做到这一点吗?

I have set the name of my Form text in the application settings and I need to rewrite it. Is it possible as it is showing me that it is only a readonly.

I know that we can simply change it by using me.text=""
But I have a problem in my application as I have the below code where On every time the form loads it is erasing the text.

Protected Overrides Sub OnLayout(ByVal e As System.Windows.Forms.LayoutEventArgs)

        MyBase.OnLayout(e)
        'Me.Text = CStr(Val(Me.Text) + 1)
        FillList()

        MyBase.OnLayout(e)
        If FontColor.Items.Count = 0 Then
            FontColor.Items.AddRange(Known_Color)
            FontColor.MaxDropDownItems = 20
        End If


        MyBase.OnLayout(e)
        If OutlineColor.Items.Count = 0 Then
            OutlineColor.Items.AddRange(Known_Color)
            OutlineColor.MaxDropDownItems = 20
        End If



        MyBase.OnLayout(e)
        If BorderColor.Items.Count = 0 Then
            BorderColor.Items.AddRange(Known_Color)
            BorderColor.MaxDropDownItems = 20
        End If


        MyBase.OnLayout(e)
        If BackgroundColor.Items.Count = 0 Then
            BackgroundColor.Items.AddRange(Known_Color)
            BackgroundColor.MaxDropDownItems = 20
        End If
    End Sub

Is there any workaround to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夕色琉璃 2024-12-29 15:00:03
MyBase.OnLayout(e)

这很可能就是你的错误所在。

您正在设置文本,然后调用基类 OnLayout 事件,这可能会导致文本恢复为默认值。如果您想更改某些内容,请在调用案例类事件处理程序后进行。

如果您只是想更改表单的文本,为什么不直接使用 Form_Load 事件处理程序而不调用基本事件处理程序。

MyBase.OnLayout(e)

most probably this is where your mistake is.

You are setting the text and then you are calling the base class OnLayout event which is probably causing the text to get back to default value. If you want to change something do it after you call the case class event handler.

If you are just trying to change the text of the form why not just use Form_Load event handler without calling the base event handler.

烟─花易冷 2024-12-29 15:00:03

回答有关应用程序设置中的表单文本为只读的问题。请查看此 MSDN 页面

来自上面的链接:

根据范围,有两种类型的应用程序设置:

  • 应用程序范围的设置可用于 Web 服务的 URL 或数据库连接字符串等信息。这些值
    与应用程序相关联。因此,用户无法更改
    它们在运行时。
  • 用户范围的设置可用于保留表单的最后位置或字体首选项等信息。用户可以更改
    这些值在运行时。

您可以使用 Scope 属性更改设置的类型。

简而言之,如果您的应用程序设置是应用程序范围的,则您无法在运行时更改它,您必须使用用户范围的设置或滚动您自己的存储。

编辑:添加到@Bojan 的答案。 OnLayout 事件将在 InitializeComponent() 方法期间以及每次调整表单大小或更改控件大小时触发。我个人会将您的初始化移至 Form_Load 事件New()

IE

Public Sub New()

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

    ' Add any initialization after the InitializeComponent() call.
    Me.Text = "Hello World"
End Sub

To answer your question about your Form Text in your application settings being readonly. Look at this MSDN Page.

From above Link:

There are two types of application settings, based on scope:

  • Application-scoped settings can be used for information such as a URL for a Web service or a database connection string. These values
    are associated with the application. Therefore, users cannot change
    them at run time.
  • User-scoped settings can be used for information such as persisting the last position of a form or a font preference. Users can change
    these values at run time.

You can change the type of a setting by using the Scope property.

In short if your application setting is application scoped you can not it change at runtime, you have to use a user scoped setting or roll your own storage.

Edit: to add to @Bojan 's answer. The OnLayout event will be fired during the InitializeComponent() method and everytime you resize the form or change the size of a control. I would personally move your initialization to the Form_Load event or to New().

i.e.

Public Sub New()

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

    ' Add any initialization after the InitializeComponent() call.
    Me.Text = "Hello World"
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文