# Validation and ComboBox

**URL:** <https://www.ruby-forum.com/t/validation-and-combobox/160705>\
**Category:** wxRuby\
**Created:** [February 19, 2009, 1:02am UTC](https://www.ruby-forum.com/t/validation-and-combobox/160705 "2009-02-19T01:02:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Peoples](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f6c823/32.png) [@David\_Peoples](https://www.ruby-forum.com/u/David_Peoples)\
**Post date:** [February 19, 2009, 1:02am UTC](https://www.ruby-forum.com/t/validation-and-combobox/160705/1 "2009-02-19T01:02:53Z")

</div>

I’m trying to make a ComboBox that can be typed into but only accepts  
entries that are already in the drop-down list. I hoped a combination of  
the CB\_DROPDOWN style and a TextValidator with the FILTER\_INCLUDE\_LIST  
would make that happen, but the validator seems to be ignored. Is that  
combination supposed to do what I want? If so, please review the code  
below and let me know where my mistake is.

Relevant libraries/platform: Ubuntu 8.04, Ruby 1.8.6, wxWidgets 2.8.9  
(deb from the [apt.wxwidgets.org](http://apt.wxwidgets.org) repository), wxruby 1.9.10 gem.

David Peoples

* * *

#!/usr/bin/env ruby  
require ‘wx’

class MyFrame \< Wx::Frame  
def initialize(title)  
super(nil, :title =\> title, :size =\> [400, 300])  
panel = Wx::Panel.new(self, -1)  
@combo\_choices = [“Apple”, “Aardvark”, “Banana”, “Berry”, “Canada”,  
“Cantaloupe”, “Cauliflower”, “Dummy”]  
validator = Wx::TextValidator.new(Wx::FILTER\_INCLUDE\_LIST)  
validator.includes = @combo\_choices  
combobox = Wx::ComboBox.new(panel, -1, “”, Wx::Point.new(15,15),  
Wx::Size.new(-1,-1), @combo\_choices, Wx::CB\_DROPDOWN, validator,  
“my\_combobox”)  
end  
end

Wx::App.run do  
self.app\_name = “Test validated combobox”  
frame = MyFrame.new(“ComboBox test”)  
frame.show  
end

---

<div class="post-metadata">

**Author:** ![David\_Peoples](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f6c823/32.png) [@David\_Peoples](https://www.ruby-forum.com/u/David_Peoples)\
**Post date:** [February 19, 2009, 2:19am UTC](https://www.ruby-forum.com/t/validation-and-combobox/160705/2 "2009-02-19T02:19:28Z")

</div>

David Peoples wrote:

> I’m trying to make a ComboBox that can be typed into but only accepts  
> entries that are already in the drop-down list.

I’m presumably missing something, but why not just use a Wx::Choice if  
you want a fixed list?

> I hoped a combination of  
> the CB\_DROPDOWN style and a TextValidator with the FILTER\_INCLUDE\_LIST  
> would make that happen, but the validator seems to be ignored. Is that  
> combination supposed to do what I want?

Thanks for mentioning this, as I hadn’t really looked into this use of  
Validators before.

Firstly, from an assertion failure I get with the debug build, it seems  
TextValidator is only for use with TextCtrl, not anything else like  
ComboBox.

With INCLUDE\_LIST and EXCLUDE\_LIST, the Validator clearly can’t validate  
on each keystroke as it does with the other types, where it blocks  
incorrect characters (eg limiting it to numerics only). wxWidgets  
expects the control to be part of a Dialog, and validates it only when  
the Dialog is dismissed.

Since the automatic message one gets on a validation failure is not very  
helpful anyway (it’s hardcoded to “XXXX is not valid”), I’d suggest it’d  
be easier to roll your own. Trap the event when the contents should be  
checked (eg evt\_kill\_focus, or clicking “OK” on a dialog button) and act  
accordingly, eg

combobox.evt\_kill\_focus do  
unless @combo\_choices.include?(combobox.value)  
# do some warning  
end  
end

alex

---

<div class="post-metadata">

**Author:** ![David\_Peoples](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f6c823/32.png) [@David\_Peoples](https://www.ruby-forum.com/u/David_Peoples)\
**Post date:** [February 19, 2009, 6:11am UTC](https://www.ruby-forum.com/t/validation-and-combobox/160705/3 "2009-02-19T06:11:23Z")

</div>

On Thu, 2009-02-19 at 01:06 +0000, Alex F. wrote:

> David Peoples wrote:
> 
> > I’m trying to make a ComboBox that can be typed into but only accepts  
> > entries that are already in the drop-down list.
> 
> I’m presumably missing something, but why not just use a Wx::Choice if  
> you want a fixed list?

My application needs to allow fairly high speed heads-down text entry  
with as close to zero mouse use as possible. The Wx::Choice control has  
a very clumsy keyboard interface. The app I’m converting was written in  
Delphi on Windows, and the custom combobox controls there had an  
excellent keyboard driven autocomplete operation I’m trying to emulate  
here.

After looking closer at the docs I realized that I can get fairly close  
to what I want with an evt\_text() handler and manipulating the text with  
ComboBox#set\_value. ComboBox#set\_text\_selection\_range isn’t doing at all  
what I expected, so I’ll probably work around _that_ by displaying the  
list of potential matches somewhere below the combobox control while  
text entry is going on. Kludgy and a lot of work, but at least probably  
_will_ work.

I’ve never stopped being shocked at how crude some of the controls are  
in Gtk and KDE, compared to what I was used to. At least wxWidgets gives  
fairly easy access to the edit control in the combobox, which none of  
the other ruby GUIs do, as far as I could work out.

Thanks for your help!

David

---

<div class="post-metadata">

**Author:** ![David\_Peoples](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f6c823/32.png) [@David\_Peoples](https://www.ruby-forum.com/u/David_Peoples)\
**Post date:** [February 19, 2009, 7:38am UTC](https://www.ruby-forum.com/t/validation-and-combobox/160705/4 "2009-02-19T07:38:03Z")

</div>

David Peoples wrote:

> My application needs to allow fairly high speed heads-down text entry  
> with as close to zero mouse use as possible. The Wx::Choice control has  
> a very clumsy keyboard interface. The app I’m converting was written in  
> Delphi on Windows, and the custom combobox controls there had an  
> excellent keyboard driven autocomplete operation I’m trying to emulate  
> here.

Makes sense. I implemented a control similar to this, can’t find the  
code now but it went roughly like:

```
evt_text(combobox) do
  val = combobox.value
  if @last_val == val # deletion
    val = val[0..-2]
  end
  matches = @combo_choices.grep(/^#{val}/)

  combobox.clear
  combobox.append matches
  if not matches.empty?
    combobox.value = matches.first
    combobox.set_text_selection_range( val.length,
                                       matches.first.length)
  end
  @last_val = val
end

```

Obviously you’d probably want to refine this and turn it into a  
self-contained class. But it should be usable completely  
keyboard-driven.

a
