C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Create an Image Combo


Posted Date: 09 Jun 2008    Resource Type: Code Snippets    Category: Winforms Controls
Author: Debasmit SamalMember Level: Gold    
Rating: Points: 10




using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

using System.Drawing.Design;
using System.ComponentModel.Design;

namespace Dotnetrix_Samples
{
///
/// Summary description for ImageCombo.
///

public class ImageCombo : System.Windows.Forms.ComboBox
{
///
/// Required designer variable.
///

private System.ComponentModel.Container components = null;

public ImageCombo()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();

// TODO: Add any initialization after the InitializeComponent call
base.DrawMode = DrawMode.OwnerDrawVariable;

}

///
/// Clean up any resources being used.
///

protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
if (this.Items != null)
{
foreach (Object o in this.Items)
{
if (o is ImageComboItem)
((ImageComboItem)o).Dispose();
}
}
}
base.Dispose( disposing );
}

#region Component Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///

private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion

private int currentIndex = -1;

[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public new DrawMode DrawMode
{
get{return DrawMode.OwnerDrawFixed;}
}


[Editor(typeof(ImageComboItemEditor), typeof(UITypeEditor))]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public new ObjectCollection Items
{
get{return base.Items;}
}


private class ImageComboItemEditor : CollectionEditor
{
public ImageComboItemEditor(Type type):base(type){}

protected override Type CreateCollectionItemType()
{
return typeof(ImageComboItem);
}


}


protected override void OnDrawItem(DrawItemEventArgs e)
{
if (e.Index == -1 || e.Index > this.Items.Count - 1)
return;

e.DrawBackground();

Rectangle imageRect = new Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Height, e.Bounds.Height);
RectangleF textRectF = RectangleF.FromLTRB(imageRect.Right + 2, e.Bounds.Top, e.Bounds.Right, e.Bounds.Bottom);

if (Items[e.Index] is ImageComboItem )
{
ImageComboItem Item = (ImageComboItem)Items[e.Index];

if (Item.Image != null)
e.Graphics.DrawImage(Item.Image, imageRect);
}

SolidBrush TextBrush = new SolidBrush(this.ForeColor);
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
TextBrush.Color = SystemColors.HighlightText;

StringFormat sf = new StringFormat(StringFormatFlags.NoWrap);
sf.LineAlignment = StringAlignment.Center;
sf.Trimming = StringTrimming.EllipsisCharacter;

e.Graphics.DrawString(Items[e.Index].ToString(), this.Font, TextBrush, textRectF, sf);
TextBrush.Dispose();
}

protected override void OnSelectedIndexChanged(EventArgs e)
{
if (this.SelectedIndex != this.currentIndex)
{
this.currentIndex = this.SelectedIndex;
base.RefreshItem(this.SelectedIndex);
}
else
{
base.OnSelectedIndexChanged (e);
}

}

}

[DesignTimeVisible(false)]
public class ImageComboItem : Component
{

private Object m_object;
private Image m_Image;

[TypeConverter(typeof(StringConverter))]
public Object Item
{
get{return m_object;}
set{m_object = value;}
}

public Image Image
{
get{return m_Image;}
set{m_Image = value;}
}

public override string ToString()
{
if (m_object == null)
return String.Empty;
else
return m_object.ToString();
}


}

}




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Hiding and Showing Tabpages in a Tabcontrol.
Previous Resource: Drag and Drop of treenode in a treeview
Return to Discussion Resource Index
Post New Resource
Category: Winforms Controls


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use