Solved

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

Posted on 2008-10-01
15
1,553 Views
Last Modified: 2013-12-17
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...
0
Comment
Question by:dfins
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
15 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 22623893
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
 
LVL 1

Author Comment

by:dfins
ID: 22623937
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 22624149
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
Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

 
LVL 1

Author Comment

by:dfins
ID: 22624202
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 22624254
You might try calling base.OnDrawItem(sender, e) in the override.
0
 
LVL 1

Author Comment

by:dfins
ID: 22627506
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 22627538
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
 
LVL 1

Author Comment

by:dfins
ID: 22627631
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 22627679
You need to inherit from ComboBox.

public class ListItemSC : System.Windows.Forms.ComboBox
{
0
 
LVL 1

Author Comment

by:dfins
ID: 22627720
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 22627934
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 22627938
.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
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 22627945
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
 
LVL 1

Author Comment

by:dfins
ID: 22627948
Thanks, I'll try to digest this and see if I can get it all to work.
 
 
Thanks!!!
0
 
LVL 1

Author Closing Comment

by:dfins
ID: 31501958
THANKS!!!
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question