dfins
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(oI tem);
I RETREIVE the items like this:
ListItemSC oItem = (ListItemSC)this.cBxPlayer List.Selec tedItem;
String strValue = oItem.Value;
String strText = oItem.Text;
SQL_LoadFromListSelect(str Value)
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].To String();
Font drawFont = new Font("Veranda", 10, FontStyle.Bold);
aColor = ((ListItemSC)this.cBxPlaye rList.Item s[e.Index] ).SelColor ;
SolidBrush drawBrush = new SolidBrush(aColor);
PointF drawPoint = new PointF(e.Bounds.X, e.Bounds.Y);
e.Graphics.DrawString(draw String, drawFont, drawBrush, drawPoint);
cBxPlayerList.AutoComplete Mode = AutoCompleteMode.SuggestAp pend;
}
}
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...
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"]
cBxPlayerList.Items.Add(oI
I RETREIVE the items like this:
ListItemSC oItem = (ListItemSC)this.cBxPlayer
String strValue = oItem.Value;
String strText = oItem.Text;
SQL_LoadFromListSelect(str
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
Font drawFont = new Font("Veranda", 10, FontStyle.Bold);
aColor = ((ListItemSC)this.cBxPlaye
SolidBrush drawBrush = new SolidBrush(aColor);
PointF drawPoint = new PointF(e.Bounds.X, e.Bounds.Y);
e.Graphics.DrawString(draw
cBxPlayerList.AutoComplete
}
}
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...
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'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.
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.
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?
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);
}
}
ASKER
Thanks, I really appreciate it... this is what I have:
It gives me this error:
Error 1 'AppX.ListItemSC.OnDrawIte m(System.W indows.For ms.DrawIte mEventArgs )': no suitable method found to Override C:\PROJECTS\AppX\AppX\Play erForm.cs 1583 33 AppX
It gives me this error:
Error 1 'AppX.ListItemSC.OnDrawIte
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;
}
}
}
You need to inherit from ComboBox.
public class ListItemSC : System.Windows.Forms.Combo Box
{
public class ListItemSC : System.Windows.Forms.Combo
{
ASKER
Damn, now I get this...
Warning 1 'AppX.ListItemSC.Text' hides inherited member 'System.Windows.Forms.Comb oBox.Text' . To make the current member override that implementation, add the override keyword. Otherwise add the new keyword. C:\PROJECTS\AppX\AppX\Play erForm.cs 1577 23 AppX
No idea...
It lets me compile, but same result...
Warning 1 'AppX.ListItemSC.Text' hides inherited member 'System.Windows.Forms.Comb
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);
}
}
}
.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;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks, I'll try to digest this and see if I can get it all to work.
Thanks!!!
Thanks!!!
ASKER
THANKS!!!
[DllImport("shlwapi.dll")]
public static extern int SHAutoComplete(HandleRef hwndEdit, int flags);
Why would you be setting AutoCompleteMode in the DrawItem event handler?