【发布时间】:2019-12-10 05:08:01
【问题描述】:
我有两个文本框 txtSearchDisplay 和 txtFind(one of 15 fields)。 txtSearchDisplay 从 txtSearchText 获取它的值。它是用户为在整个表单中进行文本搜索而输入的文本字段。我的代码调整到txtFind 框的顶部和左侧,但我还需要它来调整txtFind 框的宽度。
我在网上搜索过,但找不到合适的代码来执行此操作。谢谢您的帮助。最大
Private Sub cboField_AfterUpdate()
On Error GoTo Err_Handler
'Purpose: Locate the display text box over the field to be searched,
' and fire the search code.
'Note: On forms where controls have different widths and heights,
' you will need to set Width and Height as well.
Dim ctl As Control
If Not IsNull(Me.cboField) Then
'Set ctl to the control named in the combo.
Set ctl = Me(Me.cboField)
'NEED TO GET THE WIDTH OF THE TEXTBOX THE CONTROL IS SHOWING ON TOP OF
'Locate the search display on top of the control is simulates.
With Me.txtSearchDisplay
.Top = ctl.Top
.Left = ctl.Left
End With
End If
Call txtSearchText_AfterUpdate
Exit_Handler:
Set ctl = Nothing
Exit Sub
Err_Handler:
Resume Exit_Handler
End Sub
让活动控件显示或隐藏的代码
Private Function ShowHide(ctl As Control, bShow As Boolean)
On Error GoTo Err_Handler
'Purpose: Show or hide the control, moving focus if it has focus.
Dim strActiveControl As String 'Name of active control on this form.
Dim strSafeControl As String 'Name of a control we can set focus to.
If ctl.Visible <> bShow Then
'Get the active control name. Will error in Form_Load.
strActiveControl = Me.ActiveControl.Name
'Move focus if it's the one we are trying to hide.
If (strActiveControl = ctl.Name) And Not bShow Then
strSafeControl = Me.cboField.ItemData(0)
Me(Nz(Me.cboField, strSafeControl)).SetFocus
End If
ctl.Visible = bShow
End If
Exit_Handler:
Exit Function
Err_Handler:
'In Form_Load, there's no active control yet, so ActiveControl.Name yields error 2474.
If Err.Number = 2474& Then
Resume Next
Else
Call LogError(Err.Number, Err.Description, conMod & ".ShowHide")
Resume Exit_Handler
End If
结束函数
到目前为止我正在尝试的代码
Private Function getWidth(ctl As Control)
On Error GoTo Err_Handler
' Determine the correct size for the text box based on its text length
' Create a new SizeF object to return the size into
Dim mySize As New System.Drawing.SizeF
' Create a new font based on the font of the textbox we want to resize
' Or, use this for a specific font and font size.
'Get error on myFont. Doesn't seem to have system.drawing.font
Dim myFont As New System.Drawing.Font("Verdana", 8)
' Get the size given the string and the font
mySize = e.Graphics.MeasureString("This is a test", myFont)
' Resize the textbox to accommodate the entire string
Me.TextBox1.Width = mySize.Width
'This doesn't fire
MsgBox ("Width " & mySize.Width)
'Me.TextBox1.Width = CType(Math.Round(mySize.Width, 0), Integer)
End Function
【问题讨论】:
-
我尝试在控件上使用 ctl.SizeToFit,但问题是我需要获取控件下方文本框的大小/宽度(15 个字段之间的变化)。 @LynnCrumbling
-
ctl.Width不是您要查找的号码吗? -
否,因为那将是我需要调整的控件宽度。我需要从中获取宽度的控件是 ActiveControl
标签: vba ms-access search width