473,408 Members | 2,405 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Owner drawn listbox

Hi
I have some code to create an ownerdrawn listbox (derived), and when I add
an item to it, the bold text of the first item (the title, 'Collections and
Maturities') mysteriously seems to get bunched up at the right, i.e. squashed
up! any idea why?

The main bit of the code is as such
// (in progressReporter.cs)

protected struct LBRow //a row of the listbox, whether it be the title or a
table listing
{
public string Text;
public int Rows;
public bool IsTitle;
public LBRow(string text, int rows, bool istitle)
{
Text = text;
Rows = rows;
IsTitle = istitle;
}
}

public void AddReport(ProgressReport p)
{
Items.Add(new LBRow(p.JobName, 0, true));
foreach(ProgressReport.ProgressReportItem pi in p.TablesCopied)
Items.Add(new LBRow(pi.TableName, pi.Rows, false));
}

public struct ProgressReport
{
public string JobName;
public ProgressReportItem[] TablesCopied;
public struct ProgressReportItem
{
public string TableName;
public int Rows;
public ProgressReportItem(string tablename, int rows)
{
TableName = tablename;
Rows = rows;
}
}
public ProgressReport(string jobname, params ProgressReportItem[]
tablescopied)
{
JobName = jobname;
TablesCopied = tablescopied;
}
}
// (in frmMain)

private void Form1_Load(object sender, System.EventArgs e)
{
listBox2.AddReport(new ProgressReporter.ProgressReport(
"Collections and Maturities",
new ProgressReporter.ProgressReport.ProgressReportItem ("Table1", 100000),
new ProgressReporter.ProgressReport.ProgressReportItem ("Table2", 500000)));
}
Nov 16 '05 #1
1 2840
Here is a class (OwnerDrawListBox) I wrote that puts items in a listbox with
the option of a bold font. Pass a reference of the listbox to the
OwnerDrawListBox object, and use the objects Add method for adding items to
the Listbox. Obviously the value of this example is how to implement
drawItem() and measureItem(). I think that's the cause of your problem.
Modify the class to implement your logic.

HTH,
kevin aubuchon

Imports System.Windows.Forms
Imports System.Drawing
Imports System.Drawing.Text

Public Class OwnerDrawListBox
'
' Kevin Aubuchon 5/6/2004
'
'

' Utility class to handle owner draw listboxes. Individual line items
' may be drawn with different fonts, colors, etc.
'
' Currently if a row is 'special', then the current font is
' used but made bold. Add implementation as needed for more
' exotic treatment (different background colors, etc.)
'
Private lb As ListBox
Private bBold As Boolean = True
Private boldFont As Font
'
' helper class to hold 'special' status
'
Class helper
Inherits Object
Dim txt As String
Friend special As Boolean
Sub New(ByVal s As String, ByVal sp As Boolean)
txt = s
special = sp
End Sub
Public Overrides Function ToString() As String
Return txt
End Function
End Class
Public Sub New(ByVal theListBox As ListBox)
If IsNothing(theListBox) Then Throw New
NullReferenceException("ListBox object empty")
lb = theListBox
boldFont = New System.Drawing.Font(lb.Parent.Font, FontStyle.Bold)
lb.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable
AddHandler lb.DrawItem, AddressOf drawItem
AddHandler lb.MeasureItem, AddressOf measureItem

End Sub
Public Property UseBoldFont() As Boolean
Get
Return bBold
End Get
Set(ByVal Value As Boolean)
bBold = Value
End Set
End Property
Public Sub Add(ByVal s As String, ByVal special As Boolean)
lb.Items.Add(New helper(s, special))
End Sub
Public Sub Clear()
lb.Items.Clear()
End Sub
Private Sub drawItem(ByVal sender As Object, ByVal e As
DrawItemEventArgs)
Dim brush As Brush
' The following method should generally be called before drawing.
' It is actually superfluous here, since the subsequent drawing
' will completely cover the area of interest.
e.DrawBackground()

If e.Index < 0 OrElse e.Index > Me.lb.Items.Count - 1 Then Exit Sub

'The system provides the context
'into which the owner custom-draws the required graphics.
'The context into which to draw is e.graphics.
'The index of the item to be painted is e.Index.
'The painting should be done into the area described by e.Bounds.

brush = Brushes.White
e.Graphics.FillRectangle(brush, e.Bounds)

Dim f As Font
Dim h As helper = CType(lb.Items.Item(e.Index), helper)
If Not IsNothing(h) AndAlso h.special Then
f = boldFont
Else
f = lb.Parent.Font
End If

e.Graphics.DrawString(h.ToString(), f, Brushes.Black, e.Bounds.X,
e.Bounds.Y)

End Sub

'Return the height of the item to be drawn
Private Sub measureItem(ByVal sender As Object, ByVal e As
MeasureItemEventArgs)
Dim displayText As String
Dim stringSize As SizeF

If e.Index < 0 OrElse e.Index > Me.lb.Items.Count - 1 Then Exit Sub

'Work out what the text will be
Dim h As helper = CType(lb.Items.Item(e.Index), helper)
displayText = h.ToString()

'Get width & height of string
stringSize = e.Graphics.MeasureString(displayText, lb.Parent.Font)

'Account for top margin
stringSize.Height += 3
e.ItemHeight = CInt(stringSize.Height)
End Sub
End Class

"Patty O'Dors" <Pa********@discussions.microsoft.com> wrote in message
news:E7**********************************@microsof t.com...
Hi
I have some code to create an ownerdrawn listbox (derived), and when I add
an item to it, the bold text of the first item (the title, 'Collections and Maturities') mysteriously seems to get bunched up at the right, i.e. squashed up! any idea why?

The main bit of the code is as such
// (in progressReporter.cs)

protected struct LBRow //a row of the listbox, whether it be the title or a table listing
{
public string Text;
public int Rows;
public bool IsTitle;
public LBRow(string text, int rows, bool istitle)
{
Text = text;
Rows = rows;
IsTitle = istitle;
}
}

public void AddReport(ProgressReport p)
{
Items.Add(new LBRow(p.JobName, 0, true));
foreach(ProgressReport.ProgressReportItem pi in p.TablesCopied)
Items.Add(new LBRow(pi.TableName, pi.Rows, false));
}

public struct ProgressReport
{
public string JobName;
public ProgressReportItem[] TablesCopied;
public struct ProgressReportItem
{
public string TableName;
public int Rows;
public ProgressReportItem(string tablename, int rows)
{
TableName = tablename;
Rows = rows;
}
}
public ProgressReport(string jobname, params ProgressReportItem[]
tablescopied)
{
JobName = jobname;
TablesCopied = tablescopied;
}
}
// (in frmMain)

private void Form1_Load(object sender, System.EventArgs e)
{
listBox2.AddReport(new ProgressReporter.ProgressReport(
"Collections and Maturities",
new ProgressReporter.ProgressReport.ProgressReportItem ("Table1", 100000),
new ProgressReporter.ProgressReport.ProgressReportItem ("Table2", 500000))); }

Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Bernie Yaeger | last post by:
I now know how to gather the file type icons and I'm able to use them in a listview. But a listbox does not have a .smallimagelist or .largeimagelist member, so I don't know how to translate that...
2
by: Eric | last post by:
I implemented owner drawing on the main and context menus of my main form. It works fine. There is also a notify icon control that has a context menu. When the icon is in the status bar, it...
0
by: John R. | last post by:
I want to add a textbox next to each item in a Listbox on a Windows Form. The items need to be scrollable so I thought that maybe this is possible with an Owner Draw listbox. Searching the web...
2
by: Petrus | last post by:
Hi, I can't seem to figure out how to dynamically add new items with different font colors to a listbox. I have followed the ColorListBox example at...
2
by: dan heskett | last post by:
I am owner-drawing a listbox, in an attempt to create a nice list with some custom "fields" and text layout. Essentially it works, but I must be missing something big, conceptually, because I...
0
by: Brian Henry | last post by:
Ok whats going on here... got a couple problems... this is relevant now to this group since .NET 2.0 is finally RTM 1) draws really slow when making it a large form (resizing speed is jerky) to...
0
by: Steve Marshall | last post by:
Hi All, I'm working on an owner-drawn combo box which will display multiple columns in the list, either from a table in an associated DataSourse or from delimited strings in the Items...
0
by: LostInMd | last post by:
Hi All, I've got an owner drawn listBox where I draw and measure the items that I add to the listBox. For example, I have a listBox that can only display 10 characters on each horizontal line. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.