如何禁用列表框的 select 功能
How to disable select feature of list-box
我喜欢在 GUI 中生成 table,但没有 selection 的功能。我尝试更改选择和样式,但 selection 功能仍然如屏幕截图所示。
(define list-box
(new list-box% [label #f]
[parent top-frame]
[choices '()]
[style '(extended column-headers)]
[columns (list "No." "Fruit" "Color")]
[stretchable-width #t]
[stretchable-height #t]))
(send list-box set data)
如果我使用[enabled #f],列宽将无法再调整,列中较长的文本将被隐藏。有没有办法禁用 select 功能而不在文本后显示“...”,这意味着在列中显示全文。
即使未启用 table,您也可以使用 set-coumn-width
设置特定列的宽度。该方法还采用最小和最大宽度,这会设置宽度可以大或小的界限,但仅在启用列表时才重要。
此外,如您所见,如果您禁用该列表,用户也将无法 select 项目。
因此,为了将它们放在一起,您可以创建一个 table,您可以在其中设置宽度,并通过以下操作防止用户点击它:
#lang racket/gui
(define top-frame (new frame%
[label "Example"]
[width 500]
[height 500]))
(define list-box
(new list-box% [label #f]
[parent top-frame]
[choices '()]
[style '(extended column-headers)]
[columns (list "No." "Fruit" "Color")]
[enabled #f]
[stretchable-width #t]
[stretchable-height #t]))
(send list-box set '("1" "2" "3") '("4" "5" "6") '("7" "8" "9"))
(send list-box set-column-width 0 10 10 10)
(send list-box set-column-width 1 100 100 100)
(send list-box set-column-width 2 50 50 50)
(send top-frame show #t)
您可以设置回调函数以在选择完成后立即清除:
(new list-box%
[parent top-frame]
[style '(single)] ; use 'single so at most one row is selected
;; ...
[callback (lambda (list-box . _)
(define selection (send list-box get-selection))
(when selection (send list-box select selection #f)))]))
当一个项目被选中并迅速被取消时,这有时会导致短暂的闪烁。为了避免,可以继承list-box%
,在子类中拦截鼠标事件:
(define unselectable-list-box%
(class list-box%
(super-new)
(define/override (on-subwindow-event window event) #t)))
(define list-box
(new unselectable-list-box%
[parent top-frame]
[style '(multiple ; does NOT work with 'single
clickable-headers)]
;; ...
))
如果设置了 clickable-headers
,单击 header 仍会生成 column-control-event
。
在这个解决方案中,注意在style
中使用'multiple
;关于 'single
的一些东西使得 list-box 希望在单击后选择某些东西,即使鼠标事件被拦截。
我喜欢在 GUI 中生成 table,但没有 selection 的功能。我尝试更改选择和样式,但 selection 功能仍然如屏幕截图所示。
(define list-box
(new list-box% [label #f]
[parent top-frame]
[choices '()]
[style '(extended column-headers)]
[columns (list "No." "Fruit" "Color")]
[stretchable-width #t]
[stretchable-height #t]))
(send list-box set data)
如果我使用[enabled #f],列宽将无法再调整,列中较长的文本将被隐藏。有没有办法禁用 select 功能而不在文本后显示“...”,这意味着在列中显示全文。
即使未启用 table,您也可以使用 set-coumn-width
设置特定列的宽度。该方法还采用最小和最大宽度,这会设置宽度可以大或小的界限,但仅在启用列表时才重要。
此外,如您所见,如果您禁用该列表,用户也将无法 select 项目。
因此,为了将它们放在一起,您可以创建一个 table,您可以在其中设置宽度,并通过以下操作防止用户点击它:
#lang racket/gui
(define top-frame (new frame%
[label "Example"]
[width 500]
[height 500]))
(define list-box
(new list-box% [label #f]
[parent top-frame]
[choices '()]
[style '(extended column-headers)]
[columns (list "No." "Fruit" "Color")]
[enabled #f]
[stretchable-width #t]
[stretchable-height #t]))
(send list-box set '("1" "2" "3") '("4" "5" "6") '("7" "8" "9"))
(send list-box set-column-width 0 10 10 10)
(send list-box set-column-width 1 100 100 100)
(send list-box set-column-width 2 50 50 50)
(send top-frame show #t)
您可以设置回调函数以在选择完成后立即清除:
(new list-box%
[parent top-frame]
[style '(single)] ; use 'single so at most one row is selected
;; ...
[callback (lambda (list-box . _)
(define selection (send list-box get-selection))
(when selection (send list-box select selection #f)))]))
当一个项目被选中并迅速被取消时,这有时会导致短暂的闪烁。为了避免,可以继承list-box%
,在子类中拦截鼠标事件:
(define unselectable-list-box%
(class list-box%
(super-new)
(define/override (on-subwindow-event window event) #t)))
(define list-box
(new unselectable-list-box%
[parent top-frame]
[style '(multiple ; does NOT work with 'single
clickable-headers)]
;; ...
))
如果设置了 clickable-headers
,单击 header 仍会生成 column-control-event
。
在这个解决方案中,注意在style
中使用'multiple
;关于 'single
的一些东西使得 list-box 希望在单击后选择某些东西,即使鼠标事件被拦截。