Windows Develop Bookmark and Share   
 index > Windows Forms General > Custom ListBoxxes
 

Custom ListBoxxes

Okay, this is going to sound weird, and I may not get it described correctly, but I'm going to try. I need to create a custom list box that will get information from a database(info is unimportant), and then display 3 parts of that information. So that on one line you see Blah, next line indented you see Foo, next line indented you see Something. And it is all selectable as one item. Any ideas?
Ripler  Wednesday, May 21, 2008 9:23 PM
Add a new class to your project and paste this code:

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

public class ThreeLineListBox : ListBox {
public ThreeLineListBox() {
this.DrawMode = DrawMode.OwnerDrawVariable;
}
protected override void OnMeasureItem(MeasureItemEventArgs e) {
base.OnMeasureItem(e);
e.ItemHeight *= 3;
}
protected override void OnDrawItem(DrawItemEventArgs e) {
e.DrawBackground();
if (e.Index >= 0 && e.Index < this.Items.Count) {
string[] lines = this.Items[e.Index].ToString().Split('~');
int cnt = lines.Length;
if (cnt > 3) cnt = 3;
for (int ix = 0; ix < cnt; ++ix) {
Point pos = new Point(e.Bounds.Left + 10 * ix, e.Bounds.Top + this.Font.Height * ix);
Color fore = this.ForeColor;
if ((e.State & DrawItemState.Selected) != 0) fore = SystemColors.HighlightText;
TextRenderer.DrawText(e.Graphics, lines[ix], this.Font, pos, fore);
}
}
e.DrawFocusRectangle();
}
}

Compile. Drop the new control from the top of the toolbox onto your form. Select the Items property in the Properties window and click the button. Add strings like "one", "two~three". "four~five~six". I used the tilde to act as the string separator, you can make it anything you want. "\t" is a good choice if you fill the control in code.
nobugz  Saturday, May 24, 2008 9:30 PM

This is possible. You will need to make a custom list box which has rows 3 times higher than normal, and render the text on all 3 lines.

Do a google for "Custom ListBox" and you'll see quite a few articles on creating list boxes with custom items.

John Arlen  Wednesday, May 21, 2008 10:15 PM
By next line do you mean another line(index)....or 3 cols in one line?

cablehead  Thursday, May 22, 2008 8:50 AM

If your talking about 3 lines: This "forces" the selection of "Blah"...and then selects all three,

private void Form1_Load(object sender, EventArgs e)

{

listBox1.SelectionMode = SelectionMode.MultiExtended;

for (int i = 0; i < 5; i++)

{

listBox1.Items.Add("Blah");

listBox1.Items.Add(" Foo");

listBox1.Items.Add(" Something");

}

}

private void listBox1_Click(object sender, EventArgs e)

{

if (listBox1.Items[listBox1.SelectedIndex].ToString() != "Blah")

{

if (listBox1.SelectedIndex < listBox1.Items.Count - 3)

{

for (int i = listBox1.SelectedIndex; i < listBox1.Items.Count; i++)

{

if (listBox1.ItemsIdea.ToString() == "Blah")

{

listBox1.SelectedIndex = -1;

listBox1.SetSelected(i, true);

listBox1.SetSelected(i + 1, true);

listBox1.SetSelected(i + 2, true);

break;

}

}

}

else

{

listBox1.SelectedIndex = -1;

listBox1.SetSelected(listBox1.Items.Count -1, true);

listBox1.SetSelected(listBox1.Items.Count - 2, true);

listBox1.SetSelected(listBox1.Items.Count - 3, true);

}

}

else

{

listBox1.SetSelected(listBox1.SelectedIndex + 1, true);

listBox1.SetSelected(listBox1.SelectedIndex + 2, true);

}

}

cablehead  Thursday, May 22, 2008 8:59 AM
Add a new class to your project and paste this code:

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

public class ThreeLineListBox : ListBox {
public ThreeLineListBox() {
this.DrawMode = DrawMode.OwnerDrawVariable;
}
protected override void OnMeasureItem(MeasureItemEventArgs e) {
base.OnMeasureItem(e);
e.ItemHeight *= 3;
}
protected override void OnDrawItem(DrawItemEventArgs e) {
e.DrawBackground();
if (e.Index >= 0 && e.Index < this.Items.Count) {
string[] lines = this.Items[e.Index].ToString().Split('~');
int cnt = lines.Length;
if (cnt > 3) cnt = 3;
for (int ix = 0; ix < cnt; ++ix) {
Point pos = new Point(e.Bounds.Left + 10 * ix, e.Bounds.Top + this.Font.Height * ix);
Color fore = this.ForeColor;
if ((e.State & DrawItemState.Selected) != 0) fore = SystemColors.HighlightText;
TextRenderer.DrawText(e.Graphics, lines[ix], this.Font, pos, fore);
}
}
e.DrawFocusRectangle();
}
}

Compile. Drop the new control from the top of the toolbox onto your form. Select the Items property in the Properties window and click the button. Add strings like "one", "two~three". "four~five~six". I used the tilde to act as the string separator, you can make it anything you want. "\t" is a good choice if you fill the control in code.
nobugz  Saturday, May 24, 2008 9:30 PM

You can use google to search for other answers

Custom Search

More Threads

• DataGrid throwing a System.NullReferenceException
• having trouble running application over network but works locally
• Cannot create very large bitmap-Parameter is not valid
• Preventing "owner" Form from losing focus when showing another Form?
• Outlook Express 6
• Datagrid sorting
• Data Exchange (like Session, Querystring in ASP.NET)
• Preventing SettingsProvider from creating an application "version" folder
• Stored Procedure Failure
• How to check string is number or not in C#