 |
|
 |
I'm having a trouble with VB.NET conversion. someone please provide me the vb.net equivalent for this class????? URGENT!
|
|
|
|
 |
|
|
 |
|
 |
Very poor article, not fully documented. Doesn't work out of the box. Leaves the reader with too many questions.
|
|
|
|
 |
|
 |
Lots of bugs, little explanation
|
|
|
|
 |
|
 |
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace ImageListBox
{
public partial class ImageListBox : ListBox
{
private int m_LastItemCount;
private int m_LastClientWidth;
private int m_LastImageSize;
private int m_MaxStringHeigth;
private ImageList m_ImageList;
public ImageList ImageList
{
get { return m_ImageList; }
set
{
m_ImageList = value;
}
}
public ImageListBox()
{
InitializeComponent();
}
protected override void OnDrawItem(DrawItemEventArgs e)
{
ImageListBoxItem item = e.Index < 0 || this.DesignMode ? null : Items[e.Index] as ImageListBoxItem;
bool draw = m_ImageList != null && (item != null);
if (draw)
{
e.DrawBackground();
e.DrawFocusRectangle();
Size imageSize = m_ImageList.ImageSize;
if (this.ItemHeight != imageSize.Height + 2) this.ItemHeight = imageSize.Height + 2;
CheckHorizonalScroll(e.Graphics, e.Font);
Rectangle bounds = e.Bounds;
string strTextToDraw = item.ToString();
Color color = e.ForeColor;
if (item.ImageIndex > -1 && item.ImageIndex < m_ImageList.Images.Count)
{
m_ImageList.Draw(e.Graphics, bounds.Left, bounds.Top, item.ImageIndex);
if (item.Color != Color.Empty) color = item.Color;
}
e.Graphics.DrawString(strTextToDraw, e.Font, new SolidBrush(color), 0 + imageSize.Width, bounds.Top + (int)((this.ItemHeight - m_MaxStringHeigth) / 2));
}
else
{
base.OnDrawItem(e);
}
}
private void CheckHorizonalScroll(Graphics g, Font f)
{
int maxStringWidth = 0;
int maxStringHeight = 0;
foreach (object item in Items)
{
string s = item.ToString();
SizeF size = g.MeasureString(s, f);
if (maxStringHeight < size.Height) maxStringHeight = (int)size.Height;
if (maxStringWidth < size.Width) maxStringWidth = (int)size.Width;
}
m_LastImageSize = m_ImageList.ImageSize.Width;
m_LastClientWidth = ClientSize.Width;
m_LastItemCount = Items.Count;
m_MaxStringHeigth = maxStringHeight;
int he = maxStringWidth + m_LastImageSize;
if (HorizontalExtent != he) HorizontalExtent = he;
}
public class ImageListBoxItem
{
private int m_ImageIndex;
private object m_Item;
private Color m_Color;
public int ImageIndex
{
get { return m_ImageIndex; }
set { m_ImageIndex = value; }
}
public object Item
{
get { return m_Item; }
set { m_Item = value; }
}
public Color Color
{
get { return m_Color; }
set { m_Color = value; }
}
public ImageListBoxItem() : this(null) { }
public ImageListBoxItem(object item) : this(-1, item) { }
public ImageListBoxItem(int imageIndex, object item) : this(imageIndex, Color.Empty, item) { }
public ImageListBoxItem(int imageIndex, Color color, object item)
{
m_ImageIndex = imageIndex;
m_Color = color;
m_Item = item;
}
public override string ToString()
{
if (m_Item == null) return "Null";
return m_Item.ToString();
}
}
}
}
|
|
|
|
 |
|
|
 |
|
 |
public override string ToString()
{
return Tag;
}
if use this my items is shorted wrong ... (with tag)
how i can short my item by name ?
if use :
public override string ToString()
{
return Name;
}
i can get tag ...
can u help me ?
ty a lot :p
|
|
|
|
 |
|
 |
I found that this code had alota bugs in it that had to be worked out before it even compile.
Below are the fixes I made to the class
public class GListBox : ListBox
{
private ImageList _myImageList = new ImageList();
public ImageList ImageList
{
get {return _myImageList;}
set {
_myImageList = value;
if(null!=_myImageList)
{
this.ItemHeight = _myImageList.ImageSize.Height;
}
}
}
public GListBox()
{
this.DrawMode = DrawMode.OwnerDrawFixed;
}
protected override void OnDrawItem(System.Windows.Forms.DrawItemEventArgs e)
{
e.DrawBackground();
e.DrawFocusRectangle();
GListBoxItem item;
Rectangle bounds = e.Bounds;
Size imageSize = new Size(0,0);
if(_myImageList!=null)
imageSize = _myImageList.ImageSize;
try
{
item = (GListBoxItem) Items[e.Index];
if (item.ImageIndex != -1)
{
ImageList.Draw(e.Graphics, bounds.Left,bounds.Top,item.ImageIndex);
e.Graphics.DrawString(item.Text, e.Font, new SolidBrush(e.ForeColor),
bounds.Left+imageSize.Width, bounds.Top+2);
}
else
{
e.Graphics.DrawString(item.Text, e.Font,new SolidBrush(e.ForeColor),
bounds.Left, bounds.Top+2);
}
}
catch
{
}
base.OnDrawItem(e);
}
}
public class GListBoxItem
{
private string _myText;
private int _myImageIndex;
public string Text
{
get {return _myText;}
set {_myText = value;}
}
public int ImageIndex
{
get {return _myImageIndex;}
set {_myImageIndex = value;}
}
public GListBoxItem(string text, int index)
{
_myText = text;
_myImageIndex = index;
}
public GListBoxItem(string text): this(text,-1){}
public GListBoxItem(): this(""){}
public override string ToString()
{
return _myText;
}
}
|
|
|
|
 |
|
 |
I think there are pieces of the code missing.
|
|
|
|
 |
|
 |
Hi everyone!!!
I am new to the C# development can you tell me how can I Remove an item from the ListBox collection... It always gives an exception to that...
Brustem
|
|
|
|
 |
|
 |
Can you paste your code you use to remove the item
|
|
|
|
 |
|
 |
When I put long text in this ListBox HorizontalScrollbar isn't showing. Why?
Even when I set ScrollAlwaysVisible to true scrollbar is visible but disable.
|
|
|
|
 |
|
 |
I needed this very good class in VB. So i "translated" it.. But can you control if it good? Thanks
Public Class GListBoxItem
Private _myText As String
Private _myImageIndex As Int16
Public Property Text() As String
Get
Return _myText
End Get
Set(ByVal value As String)
_myText = value
End Set
End Property
Public Property ImageIndex() As Int16
Get
Return _myImageIndex
End Get
Set(ByVal value As Int16)
_myImageIndex = value
End Set
End Property
Public Sub New(ByVal Text As String, ByVal Index As Int16)
_myText = Text
_myImageIndex = Index
End Sub
Public Overrides Function ToString() As String
Return _myText
End Function
End Class
|
|
|
|
 |
|
 |
how can i insert an item to a listBox using C#'s built in functions?
|
|
|
|
 |
|
 |
This is a bit of a tidier way to do the OnDrawItem method, not fully tested but I don't see a problem with it - it does away with the exception throwing which may unnecessarily consume memory... protected override void OnDrawItem(System.Windows.Forms.DrawItemEventArgs e) { e.DrawBackground(); e.DrawFocusRectangle(); GListBoxItem item; Rectangle bounds = e.Bounds; Size imageSize = _myImageList.ImageSize; string strTextToDraw; item = (GListBoxItem)Items[e.Index] as GListBoxItem; if ((item!=null)&&(item.ImageIndex!=-1)) { ImageList.Draw(e.Graphics, bounds.Left, bounds.Top, item.ImageIndex); strTextToDraw = item.Text; } else { strTextToDraw = Items[e.Index].ToString(); } e.Graphics.DrawString(strTextToDraw, e.Font, new SolidBrush(e.ForeColor), bounds.Left + imageSize.Width, bounds.Top); base.OnDrawItem(e); } It will leave a gap as well for items which don't have an image which will probably look tidier if some have an image and some don't...
|
|
|
|
 |
|
 |
To be able to use this control in the designer without staring at a NullReferenceException, fix the OnDrawItem as so:
protected override void OnDrawItem(System.Windows.Forms.DrawItemEventArgs e)
{
if (!this.DesignMode)
{
e.DrawBackground();
e.DrawFocusRectangle();
GListBoxItem item;
Rectangle bounds = e.Bounds;
Size imageSize = _myImageList.ImageSize;
string strTextToDraw;
item = (GListBoxItem)Items[e.Index] as GListBoxItem;
if ((item != null) && (item.ImageIndex != -1))
{
ImageList.Draw(e.Graphics, bounds.Left, bounds.Top, item.ImageIndex);
strTextToDraw = item.Text;
}
else
{
strTextToDraw = Items[e.Index].ToString();
}
e.Graphics.DrawString(strTextToDraw, e.Font, new SolidBrush(e.ForeColor),
bounds.Left + imageSize.Width, bounds.Top);
}
base.OnDrawItem(e);
}
|
|
|
|
 |
|
 |
Thanks for this Code.
It surely works better with this Code. I had Errors in SharpDevelop before, now it works.
|
|
|
|
 |
|
 |
Just so if the picture or font size is changed... items height would change aswell...
protected override void OnDrawItem(System.Windows.Forms.DrawItemEventArgs e)
{
if (!this.DesignMode)
{
e.DrawBackground();
e.DrawFocusRectangle();
GListBoxItem item;
Rectangle bounds = e.Bounds;
Size imageSize = _myImageList.ImageSize;
string strTextToDraw;
item = (GListBoxItem)Items[e.Index] as GListBoxItem;
if ((item != null) && (item.ImageIndex != -1))
{
ImageList.Draw(e.Graphics, bounds.Left, bounds.Top, item.ImageIndex);
strTextToDraw = item.Text;
}
else
{
strTextToDraw = Items[e.Index].ToString();
}
e.Graphics.DrawString(strTextToDraw, e.Font, new SolidBrush(e.ForeColor),
bounds.Left + imageSize.Width, bounds.Top);
this.ItemHeight = e.Font.Height;
if (imageSize.Height > this.ItemHeight)
this.ItemHeight = imageSize.Height;
}
base.OnDrawItem(e);
}
|
|
|
|
 |
|
 |
I appreciate that you took time to write an article to try to help other coders, but you are doing no favor to early learners by taking shortcuts and not putting together a full demo project
|
|
|
|
 |
|
 |
That's agood article
basky
|
|
|
|
 |
|
 |
using System; using System.Windows.Forms; using System.Drawing; namespace GListBox { /// <summary> /// Summary description for Class1. /// </summary> // GListBoxItem class public class GListBoxItem { private string _myText; private int _myImageIndex; // properties public string Text { get {return _myText;} set {_myText = value;} } public int ImageIndex { get {return _myImageIndex;} set {_myImageIndex = value;} } //constructor public GListBoxItem(string text, int index) { _myText = text; _myImageIndex = index; } public GListBoxItem(string text): this(text,-1){} public GListBoxItem(): this(""){} public override string ToString() { return _myText; } }//End of GListBoxItem class // GListBox class public class GListBox : ListBox { private ImageList _myImageList; public ImageList ImageList { get {return _myImageList;} set {_myImageList = value;} } public GListBox() { // Set owner draw mode this.DrawMode = DrawMode.OwnerDrawFixed; } protected override void OnDrawItem(System.Windows.Forms.DrawItemEventArgs e) { e.DrawBackground(); e.DrawFocusRectangle(); GListBoxItem item; Rectangle bounds = e.Bounds; Size imageSize = _myImageList.ImageSize; try { item = (GListBoxItem) Items[e.Index]; if (item.ImageIndex != -1) { ImageList.Draw(e.Graphics, bounds.Left,bounds.Top,item.ImageIndex); e.Graphics.DrawString(item.Text, e.Font, new SolidBrush(e.ForeColor), bounds.Left+imageSize.Width, bounds.Top); } else { e.Graphics.DrawString(item.Text, e.Font,new SolidBrush(e.ForeColor), bounds.Left, bounds.Top); } } catch { if (e.Index != -1) { e.Graphics.DrawString(Items[e.Index].ToString(),e.Font, new SolidBrush(e.ForeColor) ,bounds.Left, bounds.Top); } else { e.Graphics.DrawString(Text,e.Font,new SolidBrush(e.ForeColor), bounds.Left, bounds.Top); } } base.OnDrawItem(e); } }//End of GListBox class } There is a bug at line 66 imageList.Draw(e.Graphics, bounds.Left,bounds.Top,item.ImageIndex); you must correct as follow: ImageList.Draw(e.Graphics, bounds.Left,bounds.Top,item.ImageIndex);
stupid bugs !!!
|
|
|
|
 |
|
 |
5 times I have tried to user this code and 5 times I have failed. No matter what I do I can't get the GListbox to work. And every time that I create it and put it on the form it crashes. All this leads me to wounder did you ACTUALLY try to use this controll after you created it, and if you did how did you ever get the thing to work? If there is ANY BODY out there that has actually been able to get this code to work please let me know as I am at a loss.
|
|
|
|
 |
|
 |
Twice in your code you reference a variable "imageList" but no where is it defined. I could guess that it is a control on a form, but how can the class GListBox know anything about it? Or is the reference in that class a typo and it should be "ImageList?"
|
|
|
|
 |
|
 |
I have a listbox contains a list of images. Now I need to know which image is clicked by the user during the run time. Does anyone know how should I get it?
Well, the DoubleClick member function is not for this, I think.
Please help, Thanks a lot!!!!
|
|
|
|
 |
|
 |
Hi,
I am new to vb.net: Converted your code to Vb. and try to place this control on my form: but unsuccesful: can somebody tell how can i see this control on my form.
Thanks
Hari.
|
|
|
|
 |