ComboBox with AutoCompleteMode = SuggestAppend and custom search(Contains)?

L

Lothar Behrens

Hi,

I have a class FlightStation with a Search property that combines two
attributes. I like to search not only for matching strings from the
beginning, but rather from any where in the string. (With the Contains
method or the string class)

What is the best and quickest solution to implement such a search when
using a combobox and not a TextBox for the input?

I know about implementing a derived class like this:

class FlightStationBindingList : BindingList<FlightStation> {
// ...
}

But is there a more quick and dirty solution ?! - Sorry :)

My solution with the TextChanged event doesn't proper work.

Thanks for your suggestions.

Lothar

private void comboBoxFlightStations_TextChanged(object sender,
EventArgs e)
{
ComboBox cb = (ComboBox)sender;
FlightStation fs = null;

string comparing = "";
if (this.comboBoxFlightStations.DroppedDown)
{
comparing = last_comparing + cb.Text;
last_comparing = comparing;

//refillWithSearchResult(cb, ref comparing, ref fs);

return;
}


System.Console.WriteLine("comboBoxFlightStations_TextChanged() " +
comparing);

refillWithSearchResult(cb, ref comparing, ref fs);

if (!this.comboBoxFlightStations.DroppedDown && bsF.Count
== 1)
{
this.comboBoxFlightStations.SelectedItem = fs;

comboBoxFlightStations_SelectionChanged(this.comboBoxFlightStations,
null);
}
else
{
this.comboBoxFlightStations.DroppedDown = true;
this.comboBoxFlightStations.Text = last_comparing;
this.comboBoxFlightStations.SelectionStart =
last_comparing.Length - 1;
}
}

private void refillWithSearchResult(ComboBox cb, ref string
comparing, ref FlightStation fs)
{
this.comboBoxFlightStations.SelectedValueChanged -= new
System.EventHandler(this.comboBoxFlightStations_SelectionChanged);

if (!this.comboBoxFlightStations.DroppedDown)
{
if (comparing == "")
{
comparing = cb.Text;
}
last_comparing = comparing;
comparing = cb.Text;
}

bsF.Clear();

for (int i = 0; i < bsF_initial.Count; i++)
{
fs = (FlightStation)bsF_initial;
System.Console.WriteLine("Comparing " + fs.Suche);
string compare = fs.Suche;
if (compare.Contains(comparing))
{
System.Console.WriteLine("Adding " + fs.Suche);
try
{
bsF.Add(fs);
}
catch (Exception ex)
{

}

}
}
this.comboBoxFlightStations.SelectedValueChanged += new
System.EventHandler(this.comboBoxFlightStations_SelectionChanged);
}


public class FlightStation
{
private static int lastID = 0;
public int _id;
public int ID
{
get
{
return _id;
}
}

public string Suche
{
get
{
return Name + " - " + City;
}
}

public string _name;
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}

public string _city;
public string City
{
get
{
return _city;
}
set
{
_city = value;
}
}

public FlightStation()
{
_id = ++lastID;
}

public FlightStation(string name, string city)
{
_id = ++lastID; Name = name; City = city;
}

public List<Airplane> _airlines = new List<Airplane>();
public List<Airplane> Airlines { get { return _airlines; } }

public List<Staff> _staff = new List<Staff>();
public List<Staff> Staff { get { return _staff; } }

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top