ImageComboBox in DataGridView – .NET – Fórum – Programujte.com
 x   TIP: Přetáhni ikonu na hlavní panel pro připnutí webu

ImageComboBox in DataGridView – .NET – Fórum – Programujte.comImageComboBox in DataGridView – .NET – Fórum – Programujte.com

 

CoSpi0
Duch
27. 6. 2008   #1
-
0
-

Ahoj, mám takový problém ve funkci Paint ve třídě DataGridViewImageComboBoxColumnCell. Jde o to, že v proměnné nemám svůj objekt ImageComboBoxItem, ale je tam jen string, vybraná hodnota v ComboBoxu. Já bych potřeboval ale celou tu třídu, abych zjistil jaký ImageIndex je přiřazen k vybrané položce, abych mohl vykreslit daný obrázek. Jinak by to vše mělo fungovat. Součástí toho je i ImageComboBox normálně na formulář a ne do gridu a ten funguje bez problémů.
Díky moc za pomoc



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

namespace Controls.ImageComboBox
{
public class ImageComboBox : ComboBox
{
private ImageList _imgList = new ImageList();

public ImageList ImageList
{
get { return _imgList; }
set { _imgList = value; }
}

public ImageComboBox()
{
this.DrawMode = DrawMode.OwnerDrawFixed;
this.DropDownStyle = ComboBoxStyle.DropDownList;
}

protected override void OnDrawItem(DrawItemEventArgs e)
{
e.DrawBackground();
e.DrawFocusRectangle();

ImageComboBoxItem item = new ImageComboBoxItem();

try
{
item = (ImageComboBoxItem)this.Items[e.Index];
int boundsLeft = e.Bounds.Left;

if ((item.ImageIndex != -1) && (this.ImageList != null) && (item.ImageIndex < this.ImageList.Images.Count))
{
this.ImageList.Draw(e.Graphics, e.Bounds.Left, e.Bounds.Top, item.ImageIndex);
boundsLeft += _imgList.ImageSize.Width;
}

e.Graphics.DrawString(item.DisplayText, e.Font, new SolidBrush(e.ForeColor), boundsLeft, e.Bounds.Top);
}
catch
{
e.Graphics.DrawString(this.Text, e.Font, new SolidBrush(e.ForeColor), e.Bounds.Left, e.Bounds.Top);
}
base.OnDrawItem(e);
}
}

public class ImageComboBoxItem
{
private string _displayText;
private object _value;
private int _imageIndex;

public string DisplayText
{
get { return _displayText; }
set { _displayText = value; }
}

public object Value
{
get { return _value; }
set { _value = value; }
}

public int ImageIndex
{
get { return _imageIndex; }
set { _imageIndex = value; }
}

public ImageComboBoxItem()
{
this._displayText = String.Empty;
this._value = String.Empty;
this._imageIndex = -1;
}

public ImageComboBoxItem(string displayText)
{
this._displayText = displayText;
this._value = displayText;
this._imageIndex = -1;
}

public ImageComboBoxItem(string displayText, object value)
{
this._displayText = displayText;
this._value = value;
this._imageIndex = -1;
}

public ImageComboBoxItem(string displayText, int imageIndex)
{
this._displayText = displayText;
this._value = displayText;
this._imageIndex = imageIndex;
}

public ImageComboBoxItem(string displayText, object value, int imageIndex)
{
this._displayText = displayText;
this._value = value;
this._imageIndex = imageIndex;
}
}

public class DataGridViewImageComboBoxColumn : DataGridViewComboBoxColumn
{
private ImageList _imgList = new ImageList();

public ImageList ImageList
{
get { return _imgList; }
set { _imgList = value; }
}

public DataGridViewImageComboBoxColumn()
{
this.CellTemplate = new DataGridViewImageComboBoxColumnCell();
}
}

public class DataGridViewImageComboBoxColumnCell : DataGridViewComboBoxCell
{
public DataGridViewImageComboBoxColumnCell()
: base()
{
this.DisplayMember = "DisplayText";
//this.ValueMember = "Value";
}

public override Type EditType
{
get { return typeof(DataGridViewImageComboBoxEditingControl); }
}

protected override object GetFormattedValue(object value, int rowIndex, ref DataGridViewCellStyle cellStyle, System.ComponentModel.TypeConverter valueTypeConverter, System.ComponentModel.TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context)
{
/*if (value != null)
return ((ImageComboBoxItem)value).DisplayText;
else
return String.Empty;*/
return value;
}

public override Type ValueType
{
get { return typeof(ImageComboBoxItem); }
}

public override object DefaultNewRowValue
{
get { return new ImageComboBoxItem(); }
}

protected override void Paint(Graphics graphics,
Rectangle clipBounds, Rectangle cellBounds, int rowIndex,
DataGridViewElementStates elementState, object value,
object formattedValue, string errorText,
DataGridViewCellStyle cellStyle,
DataGridViewAdvancedBorderStyle advancedBorderStyle,
DataGridViewPaintParts paintParts)
{
formattedValue = null;

base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue,
errorText, cellStyle, advancedBorderStyle, paintParts);


Rectangle ImageBoxRect = new Rectangle();
RectangleF TextBoxRect = new RectangleF();
GetDisplayLayout(cellBounds, ref ImageBoxRect, ref TextBoxRect);
ImageList il = ((DataGridViewImageComboBoxColumn)OwningColumn).ImageList;

if ((paintParts & DataGridViewPaintParts.Background) == DataGridViewPaintParts.Background)
{
//value = this.DataGridView.CurrentCell.Value;
if (value != null && value.GetType() == typeof(ImageComboBoxItem))
{
int boundsLeft = cellBounds.Left;
if ((((ImageComboBoxItem)value).ImageIndex != -1) && (((DataGridViewImageComboBoxColumn)OwningColumn).ImageList != null))
{
((DataGridViewImageComboBoxColumn)OwningColumn).ImageList.Draw(graphics, cellBounds.X, cellBounds.Y, ((ImageComboBoxItem)value).ImageIndex);
boundsLeft += ((DataGridViewImageComboBoxColumn)OwningColumn).ImageList.ImageSize.Width;
}

graphics.DrawString(((ImageComboBoxItem)value).DisplayText, cellStyle.Font, new SolidBrush(cellStyle.ForeColor), TextBoxRect);
}
}
}

protected virtual void GetDisplayLayout(Rectangle CellRect, ref Rectangle imageBoxRect, ref RectangleF textBoxRect)
{
const int DistanceFromEdge = 2;

imageBoxRect.X = CellRect.X + DistanceFromEdge;
imageBoxRect.Y = CellRect.Y + 1;
imageBoxRect.Size = new Size((int)(1.5 * 17), CellRect.Height - (2 * DistanceFromEdge));

textBoxRect = RectangleF.FromLTRB(imageBoxRect.X + imageBoxRect.Width + 5, imageBoxRect.Y + 2, CellRect.X + CellRect.Width - DistanceFromEdge, imageBoxRect.Y + imageBoxRect.Height);
}

public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
{
base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
DataGridViewImageComboBoxEditingControl ctrl = DataGridView.EditingControl as DataGridViewImageComboBoxEditingControl;
ctrl.ImageList = ((DataGridViewImageComboBoxColumn)OwningColumn).ImageList;
if (ctrl.ImageList != null)
ctrl.ItemHeight = ctrl.ImageList.ImageSize.Height;
}
}

public class DataGridViewImageComboBoxEditingControl : DataGridViewComboBoxEditingControl
{
private ImageList _imgList = new ImageList();

public ImageList ImageList
{
get { return _imgList; }
set { _imgList = value; }
}

public DataGridViewImageComboBoxEditingControl()
: base()
{
this.DrawMode = DrawMode.OwnerDrawFixed;
this.DropDownStyle = ComboBoxStyle.DropDownList;
}

protected override void OnDrawItem(DrawItemEventArgs e)
{
e.DrawBackground();
e.DrawFocusRectangle();

ImageComboBoxItem item = new ImageComboBoxItem();
if (e.Index != -1)
item = (ImageComboBoxItem)this.Items[e.Index];

if (item != null)
{
int boundsLeft = e.Bounds.Left;

if ((item.ImageIndex != -1) && (_imgList != null) && (item.ImageIndex < _imgList.Images.Count))
{
_imgList.Draw(e.Graphics, e.Bounds.Left, e.Bounds.Top, item.ImageIndex);
boundsLeft += _imgList.ImageSize.Width;
}

e.Graphics.DrawString(item.DisplayText, e.Font, new SolidBrush(e.ForeColor), boundsLeft, e.Bounds.Top);
}
else
e.Graphics.DrawString(this.Text, e.Font, new SolidBrush(e.ForeColor), e.Bounds.Left, e.Bounds.Top);

base.OnDrawItem(e);
}
}
}

Nahlásit jako SPAM
IP: 195.39.13.–
Zjistit počet nových příspěvků

Přidej příspěvek

Toto téma je starší jak čtvrt roku – přidej svůj příspěvek jen tehdy, máš-li k tématu opravdu co říct!

Ano, opravdu chci reagovat → zobrazí formulář pro přidání příspěvku

×Vložení zdrojáku

×Vložení obrázku

Vložit URL obrázku Vybrat obrázek na disku
Vlož URL adresu obrázku:
Klikni a vyber obrázek z počítače:

×Vložení videa

Aktuálně jsou podporována videa ze serverů YouTube, Vimeo a Dailymotion.
×
 
Podporujeme Gravatara.
Zadej URL adresu Avatara (40 x 40 px) nebo emailovou adresu pro použití Gravatara.
Email nikam neukládáme, po získání Gravatara je zahozen.
-
Pravidla pro psaní příspěvků, používej diakritiku. ENTER pro nový odstavec, SHIFT + ENTER pro nový řádek.
Sledovat nové příspěvky (pouze pro přihlášené)
Sleduj vlákno a v případě přidání nového příspěvku o tom budeš vědět mezi prvními.
Reaguješ na příspěvek:

Uživatelé prohlížející si toto vlákno

Uživatelé on-line: 0 registrovaných, 48 hostů

Podobná vlákna

DataGridView — založil Turbo1

Datagridview — založil vlak

Refresh datagridview — založil Petr

Checkbox v datagridview — založil Martin

Tisk datagridview — založil dargorar

 

Hostujeme u Českého hostingu       ISSN 1801-1586       ⇡ Nahoru Webtea.cz logo © 20032024 Programujte.com
Zasadilo a pěstuje Webtea.cz, šéfredaktor Lukáš Churý