wxAutoComboBox

If you have a cool piece of software to share, but you are not hosting it officially yet, please dump it in here. If you have code snippets that are useful, please donate!
Post Reply
gururamnath
Super wx Problem Solver
Super wx Problem Solver
Posts: 466
Joined: Sat Sep 18, 2004 2:49 am
Location: California, USA

wxAutoComboBox

Post by gururamnath »

Inspired by the AutoTextCtrl by Priyank and a Delphi Component called LookUp ComboBox, I have created a combobox which completes the partially typed value with the one available in the list.
Currently it handles only the alpha numeric characters, if you have a suggestion for filtering a broad set of keyboard accessible characters, please let me know.

Thanks,
Guru Kathiresan

Edit 1 : I replaced the alpha numeric check code. Currently a wide range of acceptable chars are allowed to be part of the auto invoke text.

wxAutoComboBox.h

Code: Select all

///////////////////////////////////////////////////////////////////////////////
// Name:        wxAutoComboBox.h
// Purpose:     wxAutoComboBox class declaration.
// Author:      Guru Kathiresan
// Modified by:
// Created:     03/24/2006
// Copyright:   (c) Guru Kathiresan
// Licence:     BSD licence
///////////////////////////////////////////////////////////////////////////////

#pragma once

// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
	#pragma hdrstop
#endif

#ifndef WX_PRECOMP
	#include "wx/wx.h"
#endif

#if wxUSE_COMBOBOX

class wxAutoComboBox : public wxComboBox
{
	DECLARE_DYNAMIC_CLASS(wxAutoComboBox)

public:
	wxAutoComboBox();

	/**
	 * Constructor
	 * \param parent	Parent window pointer
	 * \param id		Control ID
	 * \param value		Text control value
	 * \param pos		Position of the control
	 * \param size		Size of the control
	 * \param choices	List of String Items
	 * \param style		Style values for the text control
	 * \param validator	Validator for validating the user input
	 * \param name		Name of the control.
	 */
	wxAutoComboBox(wxWindow* parent, wxWindowID id = wxID_ANY, const wxString& value = wxEmptyString, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, const wxArrayString& choices = NULL, long style = 0, const wxValidator& validator = wxDefaultValidator, const wxString& name = wxT("Auto Text Control"));

	/**
	 * Default Destructor.
	 */
	virtual ~wxAutoComboBox(void);

protected:
	/**
	 * Event handler for key Up.
	 * \detail For the alpha numeric characters the partial match of the combo items will be done.
	 * \param &event 
	 */
	void OnKeyUp(wxKeyEvent &event);
private:
	// Any class wishing to process wxWindows events must use this macro
	DECLARE_EVENT_TABLE()
};

#endif // wxUSE_TEXTCTRL

wxAutoComboBox.cpp

Code: Select all


///////////////////////////////////////////////////////////////////////////////
// Name:        wxAutoComboBox.cpp
// Purpose:     wxAutoComboBox class implementation.
// Author:      Guru Kathiresan
// Modified by:
// Created:     03/24/2006
// Copyright:   (c) Guru Kathiresan
// Licence:     BSD licence
///////////////////////////////////////////////////////////////////////////////

#include "wxAutoCombobox.h"

#if wxUSE_COMBOBOX

IMPLEMENT_DYNAMIC_CLASS(wxAutoComboBox, wxComboBox)

BEGIN_EVENT_TABLE(wxAutoComboBox, wxComboBox)
	EVT_KEY_UP(wxAutoComboBox::OnKeyUp)
END_EVENT_TABLE()

wxAutoComboBox::wxAutoComboBox()
{

}

wxAutoComboBox::wxAutoComboBox(wxWindow* parent, wxWindowID id, const wxString& value, const wxPoint& pos, const wxSize& size, const wxArrayString& choices, long style, const wxValidator& validator, const wxString& name)
: wxComboBox(parent, id, value, pos, size, choices, style, validator, name)
{
}


wxAutoComboBox::~wxAutoComboBox(void)
{
}

void wxAutoComboBox::OnKeyUp(wxKeyEvent &event)
{
	int nKeyCode = event.GetKeyCode();
	if ( ((nKeyCode >= 32) && (nKeyCode <= 126)) || ((nKeyCode >= 128) && (nKeyCode <= 254)))
	{
		wxString txtValue = GetValue();

		long start=-1, end=-1;
		GetSelection(&start, &end);

		wxString EnteredValue = txtValue.Left(start);
		int Idx = -1;
		for( size_t i=0; i<this->GetCount(); i++ )
		{
			if (this->GetString(i).Left(EnteredValue.Length()).CmpNoCase(EnteredValue) == 0)
			{
				Idx = i;
				break;
			}			
		}
		if (Idx != -1)
		{
			SetSelection(Idx);
			SetSelection(start,GetValue().Length());			
			return;
		}		
		
    }
	event.Skip(true);
}
#endif // wxUSE_COMBOBOX


priyank_bolia
wxWorld Domination!
wxWorld Domination!
Posts: 1339
Joined: Wed Aug 03, 2005 8:10 am
Location: BANGALORE, INDIA
Contact:

Post by priyank_bolia »

Cool, it will fulfill requests of lot of people.
vdell
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 536
Joined: Fri Jan 07, 2005 3:44 pm
Location: Finland
Contact:

Post by vdell »

Thank you! Should become very handy.
Visual C++ 9.0 / Windows XP Pro SP3 / wxWidgets 2.9.0 (SVN) | Colligere
Post Reply