我有一个按钮,可以将文本框的字体和大小更改为Tahoma 8pt。按钮事件是:
Private Sub btnSetFont_Click()
MsgBox ("Setting Inventory Description to Tahoma 8pt")
Me.InventoryDescription.FontSize = 8
Me.InventoryDescription.FontName = "Tahoma"
End Sub
不幸的是,文字没有改变。我正在测试它,首先手动编辑字体和大小,然后按我的按钮。
但是,如果我执行以下操作,
Private Sub btnSetFont_Click()
MsgBox ("Setting Inventory Description to Tahoma 8pt")
Me.InventoryDescription.Value = "hello"
Me.InventoryDescription.FontSize = 24
Me.InventoryDescription.FontName = "Times"
End Sub
当然,文本更改为“hello”,但字体和大小确实会发生变化。 (我使用了Times 24pt,因为文本框的默认值是Tahoma 8pt,我想确保它不仅仅恢复到默认值)这让我觉得文本框需要有重点来进行更改。所以,我试过了:
Private Sub btnSetFont_Click()
MsgBox ("Setting Inventory Description to Tahoma 8pt")
Me.InventoryDescription.SetFocus
Me.InventoryDescription.FontSize = 24
Me.InventoryDescription.FontName = "Times"
End Sub
但是,不要去。
Soooo,我做错了什么?
我找到了问题的一个方面。文本框.TextFormat设置为Rich Text。如果我将其更改为纯文本,则按钮效果有效。但是,将其设置为Rich Text的原因是允许斜体。所以,我尝试先将其设置为纯文本,然后更改字体/大小,但这也不起作用。
答案 0 :(得分:0)
您的代码适合我。尝试在更改字体后添加Me.Repaint
。
答案 1 :(得分:0)
您的代码使用Access 2010为我工作。
我将此代码添加到了几个命令按钮的click事件中,允许用户在名为teachersList的列表框中动态控制字体大小。
Private Sub cmdDecreaseFont_Click()
Me.teachersList.FontSize = Me.teachersList.FontSize - 1
End Sub
Private Sub cmdIncreaseFont_Click()
Me.teachersList.FontSize = Me.teachersList.FontSize + 1
End Sub
然后在表单中添加了几个简单的命令按钮
答案 2 :(得分:0)
我有同样的需求:我希望用户能够使用粗体,斜体和带下划线的字符格式化文本,但我不想允许字体名称更改或字体大小更改。复制/粘贴操作通常会在我的textBox中导入格式化文本,需要对其进行“清理”。
我找到的解决方案在下面的函数中。 此功能应由事件过程调用(即更新后或单击时)。
Public Function CleanRichText(strTEXT, strFont, nSize)
'*****************************************************
'
For i = 1 To 9
strTEXT = Replace(strTEXT, "size=" & i, "size=" & nSize)
Next i
strTEXT = Replace(strTEXT, "font face", "font_face")
strTEXT = Replace(strTEXT, "font" & Chr(13) & Chr(10) & "face", "font_face")
Do While InStr(1, strTEXT, "font_face=" & Chr(34)) > 0
iCut1 = InStr(1, strTEXT, "font_face=" & Chr(34))
iCut2 = InStr(iCut1 + 12, strTEXT, Chr(34))
strLeft = Left(strTEXT, iCut1 - 1) & "font_face=Face"
strRight = Right(strTEXT, Len(strTEXT) - iCut2)
strTEXT = strLeft & strRight
Loop
Do While InStr(1, strTEXT, "font_face=") > 0
iCut1 = InStr(1, strTEXT, "font_face=")
iCut2 = InStr(iCut1 + 12, strTEXT, Chr(32))
strLeft = Left(strTEXT, iCut1 - 1) & "font face=" & strFont & Chr(32)
strRight = Right(strTEXT, Len(strTEXT) - iCut2)
strTEXT = strLeft & strRight
Loop
CleanRichText = strTEXT
End Function
Private Sub Cause_AfterUpdate()
Me.Cause = CleanRichText(Me.Cause, Me.Cause.FontName, 2)
End Sub
答案 3 :(得分:0)
我开始使用@yves解决方案,但是当“ font”标签包含“ size”属性时,该解决方案将失败,例如:
<font face="Arial" size="5">a colour in it will break?</font>
因此,我发现了使用#RegExp的更好方法,您可以按照以下论坛中的主题进行操作:accessforums.net
Public Function CleanRichTextRegEx(ByVal strText As String, _
ByVal strFont As String, _
ByVal nSize As Integer) As String
Dim objRegEx As Object
Set objRegEx = CreateObject("VBScript.RegExp")
On Error Resume Next
With objRegEx
.Global = True
'Replace font size
.Pattern = "size=[0-9]"
strText = .Replace(strText, " size=" & nSize)
'Replace font face
.Pattern = "face=([""'])(?:[\r\n]*(?=(\\?))\2.)*?\1"
strText = .Replace(strText, "face=" & strFont)
End With
Set objRegEx = Nothing
CleanRichTextRegEx = strText
End Function
您可以通过以下方式使用它:
richText = "<font face='Arial' size='5'>a colour in it will break?</font>"
richTextResult = CleanRichTextRegEx(richText, "Arial", 2)