donnacha Posted September 5, 2003 Posted September 5, 2003 Hi folks, has anybody come across an example of a combobox that allowes each entry to be on multiple lines with word-wrap etc... Quote Hamlet
*Gurus* divil Posted September 5, 2003 *Gurus* Posted September 5, 2003 You would have to ownerdraw to get this effect. The framework allows for fairly easy ownerdrawing of listboxes and comboboxes. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
ballisticnylon Posted September 5, 2003 Posted September 5, 2003 (edited) The need to use DrawMode.OwnerDrawVariable comes along so infrequently that I thought I'd whip up a little code for what divil's talking about. Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load 'set the drawmode, so the DrawItem and MeasureItem events will both fire... ComboBox1.DrawMode = DrawMode.OwnerDrawVariable FillComboBox() End Sub Private Sub FillComboBox() Dim item1, item2, item3, item4 As String item1 = "This is the first item." item2 = "This is the second item. Hopefully it will be " & _ "too long to fit on a single line, so it will word-wrap " & _ "within a single item in our ComboBox." item3 = "This is the third item. It is very lengthly " & _ "indeed. The Recording Industry Association of America will " & _ "extend an amnesty program to some individuals involved in " & _ "illegal sharing of copyrighted music files online, according " & _ "to numerous published reports." item4 = "And this is the fourth item" Dim items() As String = {item1, item2, item3, item4} ComboBox1.Items.AddRange(items) End Sub Private Sub ComboBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) _ Handles ComboBox1.DrawItem Try If (e.State And DrawItemState.Selected) Then e.Graphics.FillRectangle(SystemBrushes.Highlight, e.Bounds) e.Graphics.DrawString(ComboBox1.Items(e.Index), Me.Font, SystemBrushes.HighlightText, _ New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height)) Else e.Graphics.FillRectangle(SystemBrushes.Window, e.Bounds) e.Graphics.DrawString(ComboBox1.Items(e.Index), Me.Font, SystemBrushes.ControlText, _ New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height)) End If Catch ex As Exception Debug.WriteLine("Index: " & e.Index & " " & ex.Message) End Try End Sub Private Sub ComboBox1_MeasureItem(ByVal sender As Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) _ Handles ComboBox1.MeasureItem e.ItemHeight = e.Graphics.MeasureString(ComboBox1.Items(e.Index), Me.Font, ComboBox1.Width).Height End Sub Edited September 6, 2003 by ballisticnylon Quote "It may be roundly asserted that human ingenuity cannot concoct a cipher which human ingenuity cannot resolve." - Edgar Allan Poe, 1841 I long to accomplish great and noble tasks, but it is my chief duty to accomplish humble tasks as though they were great and noble. The world is moved along, not only by the mighty shoves of its heroes, but also by the aggregate of the tiny pushes of each honest worker. - Helen Keller
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.