Jeffrey,
How do you want me to send the sample project? Just post to the newsgroup?
I'll include the test code below but i can send the whole test project if
you would like. It's pretty much just one user control and a form.
The control has a hashtable that uses the index of the item as the
key. If the user double clicks(_listWits_DoubleClick) the item it
set's this value to true. The MeasureItem checks the hashtable in
order to set the new height. The DrawItem also checks this to see if
it should redraw it.
DebugOutput is putting out the correct height in the MeasureItem
method but DrawItem doesn't put out the correct Bounds.Size for it.
Cisco
[1]
private void _listWits_MeasureItem(object sender, MeasureItemEventArgs e) {
if( e.Index < 0 ) return ;
string data = _listWits.Items[e.Index].ToString();
Graphics g = e.Graphics;
SizeF size = g.MeasureString(data, _listWits.Font);
e.ItemWidth = (int)Math.Ceiling(size.Width);
int height = (int)Math.Ceiling(size.Height);
if( _hash[e.Index] != null ) {
height += (int)Math.Ceiling(Font.GetHeight(e.Graphics) * 2);
}
Debug.WriteLine("Measure Item: " + e.Index.ToString() + ", Height: " +
height);
e.ItemHeight = height;
}
/// <summary>
/// Draws the items. If the current item's value in the hashtable is true
/// then write more dummy information for it. This means i want to
increase it's size.
/// </summary>
private void _listWits_DrawItem(object sender, DrawItemEventArgs e) {
if( e.Index < 0 ) return ;
string text = _listWits.GetItemText(e.Index);
// print out the bounds for debugging
Debug.WriteLine("DrawItem: " + string.Format("Item {0}'s bound {1}",
e.Index, e.Bounds) );
Font currentFont = _listWits.Font;
Graphics g = e.Graphics;
if (((e.State & DrawItemState.Focus) == DrawItemState.Focus) && ((e.State
& DrawItemState.NoFocusRect) != DrawItemState.NoFocusRect)) {
ControlPaint.DrawFocusRectangle(g, e.Bounds, e.ForeColor, e.BackColor);
} else {
if( (e.Index + 1) % 2 != 0 ) {
Color color = Color.FromArgb(125, Color.LightBlue);
using( Brush rectBrush = new SolidBrush(color) ) {
g.FillRectangle( rectBrush, e.Bounds );
}
using( Pen rectPen = new Pen(Brushes.Black) ) {
rectPen.DashStyle = DashStyle.Dot;
Rectangle rect = new Rectangle(e.Bounds.Location, e.Bounds.Size);
rect.Size = new Size(e.Bounds.Width-1, e.Bounds.Height-1);
g.DrawRectangle(rectPen, rect);
}
} else {
e.DrawBackground();
}
}
// draw the first part
SizeF sizeFirst = g.MeasureString( text, currentFont );
using( Brush brsh = new SolidBrush(e.ForeColor) ) {
g.DrawString( text, currentFont, brsh, 0, e.Bounds.Top );
}
if( _hash[e.Index] != null ) {
g.DrawString(
e.Index.ToString().PadLeft(2, '0') + " " + DateTime.Now.ToString(),
currentFont, Brushes.Red, (int)Math.Ceiling(sizeFirst.Width),
e.Bounds.Top + currentFont.Height + currentFont.Height/2 );
}
}
private void _listWits_DoubleClick(object sender, System.EventArgs e) {
_hash[_listWits.SelectedIndex] = true;
// tried loading the data source again to see if it would reset the
// bounds
object source = _listWits.DataSource;
_listWits.DataSource = null;
if( source != null )
_listWits.DataSource = source;
_listWits.Refresh();
}
"Jeffrey Tan[MSFT]" said:
Hi cisco,
Thanks for your post.
Can you provide a little sample project to demonstrate out this issue? You
attach your demo sample in a further reply by Outlook Express. Then we can
understand the cause much better.
I will wait for your further feedback. Thanks
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! -
www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.