Solved

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

Posted on 2008-10-01
15
1,480 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
  • 8
  • 7
15 Comments
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 1

Author Comment

by:dfins
Comment Utility
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
Comment Utility
You might try calling base.OnDrawItem(sender, e) in the override.
0
 
LVL 1

Author Comment

by:dfins
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Author Comment

by:dfins
Comment Utility
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
Comment Utility
You need to inherit from ComboBox.

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

Author Comment

by:dfins
Comment Utility
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
Comment Utility
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
Comment Utility
.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
Comment Utility
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
Comment Utility
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
Comment Utility
THANKS!!!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now