本文介绍了格式化用户表单控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在每个用户窗体上设置相应的控件格式,但是我已经在用户窗体上设置了所有控件的格式,而不仅仅是标签或者只是文本框。

  Private Sub UserForm_Initialize()

FormatUserForms UFNewRequest

End Sub

Sub FormatUserForms(UF As UserForm)

UF.BackColor = RGB(51,51,102)

对于UF.Controls中的每个标签
Label.BackColor = RGB(51,51,102)
Label.ForeColor = RGB(247,247,247)
下一个

对于UF.Controls中的每个按钮
Button.BackColor = RGB(247,247,247)
Button.ForeColor = RGB(0,0,0)
下一个

对于每个TextBox在UF.Controls中
TextBox.BackColor = RGB(247,247,247)
TextBox.ForeColor = RGB(0,0,0)
Next

End Sub

这种做我想要的,但是每个For Each覆盖最后。有没有反正在用户窗体中划定控件以免它们被覆盖?

此外,每当我打开我的,然后关闭我的用户窗体,我得到一个内存不足的错误。帮助与此将不胜感激!谢谢。

解决方案

我没有检查,但内存不足错误很可能与使用默认实例 UserForm 在它自己的 Initialize 过程中:

 FormatUserForms UFNewRequest 

你不应该那样做需要)。您可以随时通过 Me 获取表单的引用。我会完全删除。至于控件,你可以调用 TypeName 来确定它是什么类型的控件,然后使用 Select Case 格式化它们:

$ pre $ Private $ $ User $ $ $ b $ FormatUserForms $ b $ End Sub
Private Sub UserForm_Initialize
Sub FormatUserForms()
Me.BackColor = RGB(51,51,102)
Dim current As Control

For Each current In Me.Controls
选择案例类型名称(当前)
案例标签
current.BackColor = RGB(51,51,102)
current.ForeColor = RGB(247,247,247)
案例CommandButton
current.BackColor = RGB(247,247,247)
current.ForeColor = RGB(0,0,0)
案例文本框
当前.BackColor = RGB(247,247,247)
current.ForeColor = RGB(0,0,0)
Next
End Sub


I am trying to format controls on each of my User Forms similarly, but what I have formats all controls on a User Form instead of just the labels or just the textboxes.

Here is what I have:

Private Sub UserForm_Initialize()

FormatUserForms UFNewRequest

End Sub

Sub FormatUserForms(UF As UserForm)

UF.BackColor = RGB(51, 51, 102)

For Each Label In UF.Controls
    Label.BackColor = RGB(51, 51, 102)
    Label.ForeColor = RGB(247, 247, 247)
Next

For Each Button In UF.Controls
    Button.BackColor = RGB(247, 247, 247)
    Button.ForeColor = RGB(0, 0, 0)
Next

For Each TextBox In UF.Controls
    TextBox.BackColor = RGB(247, 247, 247)
    TextBox.ForeColor = RGB(0, 0, 0)
Next

End Sub

This sort of does what I want, but each For Each overwrites the last. Is there anyway to delineate Controls in a User Form so that they are not overwritten?

Also, every time I open my and then close my User Forms with this, I get an "out of memory" error. Help with that would be appreciated as well! Thanks.

解决方案

I didn't check, but the "out of memory" error is most likely related to using the default instance of the UserForm in its own Initialize procedure:

FormatUserForms UFNewRequest

You shouldn't do that (and don't need to). You can always get a reference to the form in its code-behind with Me. I'd remove that entirely. As for the controls, you can call TypeName to figure out what type of control it is, and then use a Select Case to format them accordingly:

Private Sub UserForm_Initialize()
    FormatUserForms
End Sub

Sub FormatUserForms()
    Me.BackColor = RGB(51, 51, 102)
    Dim current As Control

    For Each current In Me.Controls
        Select Case TypeName(current)
            Case "Label"
                current.BackColor = RGB(51, 51, 102)
                current.ForeColor = RGB(247, 247, 247)
            Case "CommandButton"
                current.BackColor = RGB(247, 247, 247)
                current.ForeColor = RGB(0, 0, 0)
            Case "TextBox"
                current.BackColor = RGB(247, 247, 247)
                current.ForeColor = RGB(0, 0, 0)
    Next
End Sub

这篇关于格式化用户表单控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 14:17