首页 > 解决方案 > 在 Access 中获取文本框的宽度

问题描述

我有两个文本框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

标签: vbams-accesssearchwidth

解决方案


您可以使用Screen.PreviousControl 属性

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

推荐阅读