下拉列表宽度_如何动态设置组合下拉列表宽度

下拉列表宽度

本周的“每周提示”将清楚地演示如何动态地将组合框的下拉列表宽度设置为其行源中最长的项目的长度。 本技巧的灵感来自我们自己的一名常驻专家,

mshmyob 。 为了响应与此主题相关的主题, mshmyob提出了一种相当巧妙的方法来完成此任务。 他计算了由大写和小写字符以及数字0到9组成的字符串的平均字符宽度,然后应用此逻辑来确定组合框的列表宽度。 他通过计算“组合框”中最长条目的长度,确定当前字体名称和大小,利用在“隐藏模式”下打开的报表的“ TextWidth”方法,计算“平均字符宽度”,然后将该值传回表单来做到这一点。 (到文本框),现在可以计算组合框的ListWidth来容纳最长的条目。 这种方法与众不同的原因在于:
  • 这都是在Access内部完成的,无需外部引用或API调用。
  • 无论字体大小和名称如何,代码都会调整并始终产生所需的结果。
  • 由于窗体没有TextWidth方法,因此必须实现一种替代方法来解决此难题。 mshmyob在“隐藏报告”中使用TextWidth方法,这一点都不直观,实际上非常聪明。
我将在下面发布代码以供您预览。 它得到了充分的评论,甚至有过大的杀伤力,但是它应该可以很好地指示正在发生的事情。 这些代码修改是在mshmyob的同意下进行的,但请记住,最初的概念是他的想法,而不是头脑。 即使有各种各样的评论,而且我感觉是基于声音和逻辑代码的方法,但我还是将此技巧作为附件提供。 我强烈建议您下载它并直观地了解此功能的实际实现方式。 如果您有任何疑问,请随时提问。 我将监视此线程,因为我确定mshmyob也会。
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

下拉列表宽度

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值