【问题标题】:Get width of text box in Access在 Access 中获取文本框的宽度
【发布时间】:2019-12-10 05:08:01
【问题描述】:

我有两个文本框 txtSearchDisplaytxtFind(one of 15 fields)txtSearchDisplaytxtSearchText 获取它的值。它是用户为在整个表单中进行文本搜索而输入的文本字段。我的代码调整到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


【解决方案1】:

如果您需要基于文本长度的TextBox的所需宽度,Access的隐藏对象Wizook具有正确的功能:TwipsFromFont

http://pointltd.com/Downloads/Files/WizHook.pdf

【讨论】:

    【解决方案2】:

    您可以使用Screen.PreviousControl property:

    ctl.Width = Screen.PreviousControl.Width
    

    应该给你你想要的。

    编辑:

    返回的类型(控件)不支持宽度。您可以通过先转换为另一种控件类型来检索宽度。因此,如果您之前的控件是一个文本框:

    Dim prevTB as TextBox
    Set prevTB = Screen.PreviousControl    
    Me.OtherControl.Width = prevTB.Width  ' you can now access the width
    

    如果上一个控件是组合框:

    Dim prevCombo As ComboBox
    Set prevCombo = Screen.PreviousControl
    Me.OtherControl.Width = prevTB.Width  ' you can now access the width
    

    【讨论】:

    • 也许我没有把它放在正确的地方,但这仍然不适合我。 @Lynn Crumbling 你能指出代码应该放在哪里吗?
    猜你喜欢
    • 1970-01-01
    • 2010-12-17
    • 2015-12-31
    • 1970-01-01
    • 2012-11-16
    • 2012-08-11
    • 2014-02-16
    • 1970-01-01
    • 2019-01-23
    相关资源
    最近更新 更多