Click here to Skip to main content
15,887,214 members
Articles / Programming Languages / C#

An editable multi-line listbox for .NET

Rate me:
Please Sign up or sign in to vote.
4.85/5 (42 votes)
4 Aug 2002Ms-PL3 min read 411.9K   7.8K   80  
An owner drawn listbox that supports multi-line word-wrapped text as well as in-place editing.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace MultiLineListBoxDemo
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private NishBox.MultiLineListBox multiLineListBox1;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			string s = "There was this guy from Trivandrum " +
				"who wanted to travel all round the world. " +
				"But this guy from Trivandrum could not manage " +
				"to do that and he's heart broken. All the queen's " +
				"soldiers and all the queen's men " +
				"couldn't put his poor heart together again";

			multiLineListBox1.Items.Add(s);

			s = "Colin Davies, James Johnson, Jack Handy, Shog9, " +
				"Roger Wright, Roger Allen, PJ, Nnamdi, Mike Dunn, " +
				"Christian Graus, Nish, Cathy, Smitha, Lauren, " +
				"Kannan Kalyanaraman, Simon Walton, Isaac Sasson, " +
				"Matt Newman, Paul Watson, Andrew Peace - these fellows " +
				"are often found on Bob's HungOut";

			multiLineListBox1.Items.Add(s);

			s = "Chris Maunder and David Cunningham run the Code Project web site";

			multiLineListBox1.Items.Add(s);

			s = "I thoth I tho a puthy cath. I thoth I tho another puthy cath. " +
				"I deed. I deed tho a puthy cath and I deed tho another puthy cath";

			multiLineListBox1.Items.Add(s);

			s = "I believe I can fly, I believe I can touch the sky. " +
				"I think about it every night and day, spread my wings " +
				"and I fly away. I belive I can fly.";

			for(int i=0; i<3; i++)
				multiLineListBox1.Items.Add(s);

		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.multiLineListBox1 = new NishBox.MultiLineListBox();
			this.SuspendLayout();
			// 
			// multiLineListBox1
			// 
			this.multiLineListBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable;
			this.multiLineListBox1.Location = new System.Drawing.Point(16, 24);
			this.multiLineListBox1.Name = "multiLineListBox1";
			this.multiLineListBox1.ScrollAlwaysVisible = true;
			this.multiLineListBox1.Size = new System.Drawing.Size(296, 280);
			this.multiLineListBox1.TabIndex = 0;
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(338, 318);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.multiLineListBox1});
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.MaximizeBox = false;
			this.Name = "Form1";
			this.Text = "Editable multi-line listbox - Nish";
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
United States United States
Nish Nishant is a technology enthusiast from Columbus, Ohio. He has over 20 years of software industry experience in various roles including Chief Technology Officer, Senior Solution Architect, Lead Software Architect, Principal Software Engineer, and Engineering/Architecture Team Leader. Nish is a 14-time recipient of the Microsoft Visual C++ MVP Award.

Nish authored C++/CLI in Action for Manning Publications in 2005, and co-authored Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on CodeProject.com and another 250+ blog articles on his WordPress blog. Nish is experienced in technology leadership, solution architecture, software architecture, cloud development (AWS and Azure), REST services, software engineering best practices, CI/CD, mentoring, and directing all stages of software development.

Nish's Technology Blog : voidnish.wordpress.com

Comments and Discussions