Auto Complete on ComboBox, not working with user defined DrawItem!

Hey guys,

        I've got an issue with a Windows application I've written in C# using Visual Studio 2005. With the help of Expert's Exchange, I was able to create a fancy ComboBox that allows different colors for different items. With the use of a special class that was created, I do the following:

I SET / ADD the items in the ComboBox like this:

       ListItemSC oItem = new ListItemSC();
       oItem.SelColor = Color.Blue;
       oItem.Text = strTempstr.ToUpper();
       oItem.Value = dataReader["guidPersonID"].ToString();
       cBxPlayerList.Items.Add(oItem);


I RETREIVE the items like this:

       ListItemSC oItem = (ListItemSC)this.cBxPlayerList.SelectedItem;
       String strValue = oItem.Value;
       String strText = oItem.Text;
       SQL_LoadFromListSelect(strValue)


And I set the DrawItem event of the ComboBox to this:

        private void ComboBox_DrawItem(object sender, DrawItemEventArgs e)
        {
            System.Drawing.Color aColor = new System.Drawing.Color();
            if (e.Index >= 0)
            {
                e.DrawBackground();
                String drawString = this.cBxPlayerList.Items[e.Index].ToString();
                Font drawFont = new Font("Veranda", 10, FontStyle.Bold);
                aColor = ((ListItemSC)this.cBxPlayerList.Items[e.Index]).SelColor;
                SolidBrush drawBrush = new SolidBrush(aColor);
                PointF drawPoint = new PointF(e.Bounds.X, e.Bounds.Y);
                e.Graphics.DrawString(drawString, drawFont, drawBrush, drawPoint);
                cBxPlayerList.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            }
        }




Everything works GREAT! Except I have ONE major problem....

Auto Complete no longer works.

The Auto Complete is currently set like this:

AutoCompleteMode = SuggestAppend
AutoCompleteSource = ListItems
DrownDownStyle = DropDownList
DrawMode = Normal

Something to note, BEFORE I programmed it to accept colors, etc... the auto complete worked PERFECTLY FINE... ever since I coded it to accept colors (using the DrawItem event) it no longer works.

Any idea what I can do?


Thanks guys, I really appreciate any help...
LVL 1
dfinsAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Bob LearnedConnect With a Mentor Commented:
Reference:

ComboBox..::.OnDrawItem Method
http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.ondrawitem.aspx

<Quote>This method is called each time an owner-drawn ComboBox item needs to be drawn and after OnMeasureItem is called. For more information, see DrawItem.

Raising an event invokes the event handler through a delegate. For more information, see Raising an Event.

The OnDrawItem method also allows derived classes to handle the event without attaching a delegate. This is the preferred technique for handling the event in a derived class.

Notes to Inheritors:

When overriding OnDrawItem in a derived class, be sure to call the base class's OnDrawItem method so that registered delegates receive the event. </Quote>
0
 
Bob LearnedCommented:
Hmmm...setting AutoCompleteMode eventually calls a Windows API:

[DllImport("shlwapi.dll")]
public static extern int SHAutoComplete(HandleRef hwndEdit, int flags);

Why would you be setting AutoCompleteMode in the DrawItem event handler?
0
 
dfinsAuthor Commented:
Well, for no other reason than I'm trying everything I possibly can. Auto Complete works fine everywhere else in the application, but on this ONE combobox where I'm manipulating the colors / fonts, it doesn't work. It doesn't work whether or not I put it in the event handler or not... I'm just trying everything possible that I can...
I've tried it with, or without, no difference. The options are already set in the properties window anyway.
 
Any idea?
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Bob LearnedCommented:
I have no idea whatsoever, but maybe I have too narrow a view of your problem.  There might be some other code, like an override that I can't see, that would start to explain the root of your problem.
0
 
dfinsAuthor Commented:
I think the override is the fact that I'm using the OnDrawItem Event... I think that's basically breaking the Auto Complete. The second that I eliminate the OnDrawItem event, the autocomplete starts working again... (but of course the colors / fonts do not).
 
 
0
 
Bob LearnedCommented:
You might try calling base.OnDrawItem(sender, e) in the override.
0
 
dfinsAuthor Commented:
Thanks Learned One, I apologize for the stupid question, but how exactly do I do that?
Where exactly would I put that code?
 
base.OnDrawItem(sender, e)?
By override, do you mean, the OnDrawItem procedure that I wrote that handles the color coding of the text items?
0
 
Bob LearnedCommented:
Yes, it would be something like this:


    public class CustomComboBox : ComboBox
    {
 
        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            base.OnDrawItem(e);
        }
 
    }

Open in new window

0
 
dfinsAuthor Commented:
Thanks, I really appreciate it... this is what I have:
It gives me this error:
 
Error 1 'AppX.ListItemSC.OnDrawItem(System.Windows.Forms.DrawItemEventArgs)': no suitable method found to Override C:\PROJECTS\AppX\AppX\PlayerForm.cs 1583 33 AppX
 
 

    public class ListItemSC
    {
        public ListItemSC()
        {
        }
 
        public ListItemSC(string text, string value, Color color)
        {
            _text = text;
            _value = value;
            _color = color;
        }
        private string _value;
        public string Value
        {
            get { return _value; }
            set { _value = value; }
        }
 
        private string _text;
        public string Text
        {
            get { return _text; }
            set { _text = value; }
        }
 
        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            base.OnDrawItem(e);
        }
 
        public override string ToString()
        {
            return _text;
        }
 
        private Color _color;
        public Color SelColor
        {
            get
            {
                return _color;
            }
            set
            {
                _color = value;
            }
        }
    }

Open in new window

0
 
Bob LearnedCommented:
You need to inherit from ComboBox.

public class ListItemSC : System.Windows.Forms.ComboBox
{
0
 
dfinsAuthor Commented:
Damn, now I get this...
 
Warning 1 'AppX.ListItemSC.Text' hides inherited member 'System.Windows.Forms.ComboBox.Text'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword. C:\PROJECTS\AppX\AppX\PlayerForm.cs 1577 23 AppX

 
No idea...
 
It lets me compile, but same result...
0
 
Bob LearnedCommented:
Here is an example of a custom ComboBox:


using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
 
namespace SampleControls
{
 
    public class CustomComboBox : ComboBox
    {
 
        public CustomComboBox()
        {
            this.DrawMode = DrawMode.OwnerDrawFixed;
        }
 
        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            string folder = this.Items[e.Index].ToString();
 
            Color foreColor = e.ForeColor;
            Color backColor = e.BackColor;
 
            if (!Directory.Exists(folder))
            {
                foreColor = Color.Red;
                if (e.State == DrawItemState.Selected)
                {
                    backColor = Color.LightPink;
                }
            }
            else
            {
                DirectoryInfo info = new DirectoryInfo(folder);
 
                if ((info.Attributes & FileAttributes.Compressed) == FileAttributes.Compressed)
                {
                    foreColor = Color.Blue;
                    if (e.State == DrawItemState.Selected)
                    {
                        backColor = Color.LightBlue;
                    }
                }
                else if ((info.Attributes & FileAttributes.Encrypted) == FileAttributes.Encrypted)
                {
                    foreColor = Color.Green;
                    if (e.State == DrawItemState.Selected)
                    {
                        backColor = Color.LightGreen;
                    }
                }
            }
 
            using (SolidBrush brush = new SolidBrush(backColor))
            {
                e.Graphics.FillRectangle(brush, e.Bounds);
            }
 
            using (SolidBrush brush = new SolidBrush(foreColor))
            {
                e.Graphics.DrawString(folder, e.Font, brush, e.Bounds);
            }
 
            base.OnDrawItem(e);
        }
 
    }
 
}

Open in new window

0
 
Bob LearnedCommented:
.designer code:


            // 
            // customComboBox1
            // 
            this.customComboBox1.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Append;
            this.customComboBox1.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.FileSystem;
            this.customComboBox1.FormattingEnabled = true;
            this.customComboBox1.Location = new System.Drawing.Point(178, 133);
            this.customComboBox1.Name = "customComboBox1";
            this.customComboBox1.Size = new System.Drawing.Size(189, 21);
            this.customComboBox1.TabIndex = 0;

Open in new window

0
 
dfinsAuthor Commented:
Thanks, I'll try to digest this and see if I can get it all to work.
 
 
Thanks!!!
0
 
dfinsAuthor Commented:
THANKS!!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.