下拉列表宽度
本周的“每周提示”将清楚地演示如何动态地将组合框的下拉列表宽度设置为其行源中最长的项目的长度。 本技巧的灵感来自我们自己的一名常驻专家,
mshmyob 。 为了响应与此主题相关的主题, mshmyob提出了一种相当巧妙的方法来完成此任务。 他计算了由大写和小写字符以及数字0到9组成的字符串的平均字符宽度,然后应用此逻辑来确定组合框的列表宽度。 他通过计算“组合框”中最长条目的长度,确定当前字体名称和大小,利用在“隐藏模式”下打开的报表的“ TextWidth”方法,计算“平均字符宽度”,然后将该值传回表单来做到这一点。 (到文本框),现在可以计算组合框的ListWidth来容纳最长的条目。 这种方法与众不同的原因在于:- 这都是在Access内部完成的,无需外部引用或API调用。
- 无论字体大小和名称如何,代码都会调整并始终产生所需的结果。
- 由于窗体没有TextWidth方法,因此必须实现一种替代方法来解决此难题。 mshmyob在“隐藏报告”中使用TextWidth方法,这一点都不直观,实际上非常聪明。
Private Sub Form_Open(Cancel As Integer)
Dim lngNumberOfEntries As Long
Dim lngLongestRowlength As Long
Dim dblLargestCboEntryWidth As Double
Call IntroDialog
'gcboCtl is a Global Object Variable of Type Combo Box
'Initialize it now
Set gcboCtl = Me![cboAdjust]
lngNumberOfEntries = gcboCtl.ListCount
If lngNumberOfEntries = 0 Then Exit Sub 'No entries, Bye-Bye!
'Retrieve the longest Row of data from the Combo Box via the Function, pass
'to the Function the number of entries in the Combo Box
lngLongestRowlength = fGetLongestRowLength(lngNumberOfEntries)
'Determine Font Size and Font Name in use and Save these values in Global
'Variables instead of passing Arguments to the Report, then retrieving them
gintFontSize = gcboCtl.FontSize
gstrFontName = gcboCtl.FontName
'Open the Report, but in Hidden Mode so you can apply Parameters to it such as
'FontSize and FontName. We need to use a Report because Access's TextWidth
'Method only works in a Report, and not a Form
DoCmd.OpenReport "rptAvgString", acViewPreview, , , acHidden
'txtAvgValue holds the value passed back from the Report and is the Average
'Character Width based on the pre-determined String (strMessage) as defined
'in the Print() Event of the Report. You can hide the txtAvgValue Text Box,
'I just used this to show you how it works. Multiply the Average Character
'Width by the longest value in the Combo Box to arrive at the Length of the
'longest entry in Twips.
dblLargestCboEntryWidth = Me.txtAvgValue * lngLongestRowlength
'Adjust the Drop Down List Width of the Combo Box
gcboCtl.ListWidth = dblLargestCboEntryWidth
'Close the 'Hidden' Report
DoCmd.Close acReport, "rptAvgString", acSaveNo
Call FillDescription 'Program Description in Form Label
End Sub
Private Sub Detail_Print(Cancel As Integer, PrintCount As Integer)
Dim rpt As Report
Dim strMessage As String
Dim dblAvgSize As Double
Const conTWIPS As Byte = 1
Set rpt = Me
'Create a string with all upper and lowercase characters, This process will allow
'the creation of an'Average Characer Width in Twips given the Font's Name and Size.
strMessage = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
With rpt
'Set ScaleMode to Twips, and set FontName and FontSize properties.
'Character size is based on Font Size and Font Name - set as Twips not Pixels
.ScaleMode = conTWIPS
.FontName = gstrFontName
.FontSize = gintFontSize
End With
'Use the TextWidth method to determine the Horizontal Width - Vertical is unimportant.
'The value returned by the TextWidth method is expressed in terms of the coordinate system
'in effect for the Report, as defined by the Scale Method
dblAvgSize = rpt.TextWidth(strMessage) / 62
'Pass this number back to the calling form to calculate length needed
Forms!frmListWidth.txtAvgValue = dblAvgSize
End Sub
翻译自: https://bytes.com/topic/access/insights/830511-how-dynamically-set-combo-drop-down-list-width
下拉列表宽度