Hi,
Is it possible to customise the incremental search facility in the list box control?
I have a listbox with items like:
Miss Alan
Mr Brown
Mr James
Mrs Jones
Mrs Smith
etc
And I would like to start the search ignoring the Miss/Mr/Mrs/etc.
Is it posible to subclass the control and override a function within it?
Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.
Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.
Hi James,
Unfortunately, it's a rather difficult task to implement this behavior, because the search functionality is implemented via the ListBoxSearch class whose methods are not virtual. However, you can implement your custom ListBoxSearch class based on our source code. To use your class in the ListBoxControl, you need to pass it via the reflection mechanism:
public class MyListBoxSearch: ListBoxSearch { } private void Form1_Load(object sender, EventArgs e) { PropertyInfo pi = typeof(ListBoxControl).GetProperty("Handler", BindingFlags.Instance | BindingFlags.NonPublic); ListBoxControlHandler handler = pi.GetValue(listBoxControl1, null) as ListBoxControlHandler; lbs = new ListBoxSearch(this.listBoxControl1, new ListBoxControlHandler.SingleSelectState(handler)); pi = typeof(ListBoxControlHandler).GetProperty("ControlState", BindingFlags.Instance | BindingFlags.NonPublic); ListBoxControlHandler.ListBoxControlState state = pi.GetValue(handler, null) as ListBoxControlHandler.ListBoxControlState; FieldInfo fi = typeof(ListBoxControlHandler.ListBoxControlState).GetField("search", BindingFlags.Instance | BindingFlags.NonPublic); fi.SetValue(state, lbs); }
Hope this information will help you achieve the desired result.
If you encounter any problem when accomplishing your task, please don't hesitate to contact us.
Thanks
Dimitros
Hi Dimitros,
Thanks for that. That should get me going however from my initial investigation into your code I can see that the highlight is always taken from position zero. Any hints on what I need to change to get the control to highlight the correct text?
Hi Dimotros,
Whilst I can inject my new object derived from ListBoxSearch into the ListBox, my new FindString method is not called.
Can you supply an example showing this working please.
fyi, here's what I have:
public class MyListBoxSearch : ListBoxSearch
{
public MyListBoxSearch(BaseListBoxControl listBox, DevExpress.XtraEditors.ListBoxControlHandler.ListBoxControlState controlState)
: base( listBox, controlState )
{
}
public bool KeyPress(char chr)
{
if ((chr == ' ' && IsEmpty) || chr == '\r')
return false;
string newSearch = GetNewSearch(chr);
return FindNext(newSearch, !IncrementalSearch, true);
}
protected bool FindNext(string s, bool startFromNext, bool updown)
{
if(string.IsNullOrEmpty(s)) {
Reset();
return true;
}
int newIndex = FindString(s, startFromNext, updown);
if(newIndex != -1) {
CurrentSearch = s;
FocusedIndex = newIndex;
SelectedIndex = newIndex;
}
return newIndex != -1;
}
}
I can see the construction of my object being called but none of the other methods.
Hi James,
You can paint the highlighted text manually by using the DrawItem event. To learn how we paint a highlighted text, please see the DrawItemHighlight method in the BaseListBox.cs unit. An easier solution is to use the MultiColorDrawString method of the XPaint class. Such painting is implemented in the SortingBySummary section of the GridMainDemo demo (<DevExpress 2009.> \Components\Demos\XtraGrid\CS\GridMainDemo\GridMainDemo.sln). To review the necessary code, open the SortingBySummary.cs unit and review the gridView1_CustomDrawGroupRow procedure's implementation.
Hope, this information will help you achieve the desired result. If you need any further assistance on this subject, please feel free to reactivate this ticket.
Thanks
Dimitros
Hi Dimitros,
Unfortunately I cannot even even get the "finding" part working, i.e. the KeyPress/FindNext methods are not being invoked in my class.
As I asked can you provide a sample showing at least custom searching.
Hi James,
Please review the attached sample and inform me whether it meets your requirements.
To paint the highlighted text use the DrawItem event.
Thanks
Dimitros