Skip to content

Instantly share code, notes, and snippets.

@cmpunches
Created October 1, 2015 05:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmpunches/aab575767f690e2f2921 to your computer and use it in GitHub Desktop.
Save cmpunches/aab575767f690e2f2921 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace gui_20150930_winform
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
// Calculate the size of an item.
private int ItemMargin = 5;
private void lstJobs_MeasureItem(object sender,
MeasureItemEventArgs e)
{
// Get the ListBox and the item.
ListBox lst = sender as ListBox;
string txt = (string)lst.Items[e.Index];
// Measure the string.
SizeF txt_size = e.Graphics.MeasureString(txt, this.Font);
// Set the required size.
e.ItemHeight = (int)txt_size.Height + 2 * ItemMargin;
e.ItemWidth = (int)txt_size.Width;
}
// Draw the item.
private void lstJobs_DrawItem(object sender,
DrawItemEventArgs e)
{
// Get the ListBox and the item.
ListBox lst = sender as ListBox;
string txt = (string)lst.Items[e.Index];
// Draw the background.
e.DrawBackground();
// See if the item is selected.
if ((e.State & DrawItemState.Selected) ==
DrawItemState.Selected)
{
// Selected. Draw with the system highlight color.
e.Graphics.DrawString(txt, this.Font,
SystemBrushes.HighlightText, e.Bounds.Left,
e.Bounds.Top + ItemMargin);
}
else
{
// Not selected. Draw with ListBox's foreground color.
using (SolidBrush br = new SolidBrush(Color.Black))
{
e.Graphics.DrawString(txt, this.Font, br,
e.Bounds.Left, e.Bounds.Top + ItemMargin);
}
}
// Draw the focus rectangle if appropriate.
e.DrawFocusRectangle();
}
private void btnAddJob_Click(object sender, EventArgs e)
{
lstJobs.DrawMode = DrawMode.OwnerDrawVariable;
// Create some items.
lstJobs.Items.Add("Name: Mercury\nMass: 0.055 Earths\nYear: 87.9691 Earth days\nTemp: 183 °C to 427 °C");
lstJobs.Items.Add("Name: Venus\nMass: 0.815 Earths\nYear: 243 Earth days");
lstJobs.Items.Add("Name: Earth\nMass: 1.0 Earths\nYear: 365.256 Earth days");
lstJobs.Items.Add("Name: Mars\nMass: 0.107 Earths\nYear: 6");
}
private void lstJobs_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment