Link to home
Start Free TrialLog in
Avatar of dfins
dfinsFlag for United States of America

asked on

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...
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

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?
Avatar of dfins

ASKER

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?
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.
Avatar of dfins

ASKER

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).
 
 
You might try calling base.OnDrawItem(sender, e) in the override.
Avatar of dfins

ASKER

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?
Yes, it would be something like this:


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

Open in new window

Avatar of dfins

ASKER

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

You need to inherit from ComboBox.

public class ListItemSC : System.Windows.Forms.ComboBox
{
Avatar of dfins

ASKER

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...
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

.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

ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dfins

ASKER

Thanks, I'll try to digest this and see if I can get it all to work.
 
 
Thanks!!!
Avatar of dfins

ASKER

THANKS!!!