 |
Visual C++ Questions specific to Microsoft's Visual C++. For questions not specific to this Microsoft version, use the C++ Programming forum instead. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Visual C++ section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|

June 30th, 2015, 10:04 AM
|
Authorized User
|
|
Join Date: Jun 2012
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Using DrawLine to make a multiCombo Box
I'm baffled!
Making a multiple Column Combo box using DrawString and Drawline.
I have seven columns but the muliticolumnCombobox just won't put them in the right order!
The seven columns are garbled together and the line divider seems to cut through the data at the wrong places.
Any idea what I'm off on?
Thanks!
Here is the code.
Code:
// ID: int, Name: string, Name2: string, Address: string, City: string, State: string, Zip: string
//
DataTable dtTest = new DataTable();
dtTest.Columns.Add("ID", typeof(int));
dtTest.Columns.Add("Name", typeof(string));
dtTest.Columns.Add("Name2", typeof(string));
dtTest.Columns.Add("Address", typeof(string));
dtTest.Columns.Add("City", typeof(string));
dtTest.Columns.Add("State", typeof(string));
dtTest.Columns.Add("Zip", typeof(string));
dtTest.Rows.Add(1, "John", "Doe", "3493 West Newt", "Tyler", "TX", "76704");
dtTest.Rows.Add(2, "Amy","Shale", "401 Lake St.", "Tyler", "TX", "33838");
dtTest.Rows.Add(3, "Tony", "Busco", "302 Handal Lane", "Tyler", "TX", "33838");
dtTest.Rows.Add(4, "Bruce", "Lee", "433 Baker St.", "Tyler", "TX", "33838");
dtTest.Rows.Add(5, "Allen", "Shondo", "Lando Lane Nol 1.", "Cushing", "TX", "33838");
// Bind the ComboBox to the DataTable
this.comboBox1.DataSource = dtTest;
this.comboBox1.DisplayMember = "Name";
this.comboBox1.ValueMember = "ID";
// Enable the owner draw on the ComboBox.
this.comboBox1.DrawMode = DrawMode.OwnerDrawFixed;
// Handle the DrawItem event to draw the items.
this.comboBox1.DrawItem += delegate(object cmb, DrawItemEventArgs args)
{
// Draw the default background
args.DrawBackground();
// The ComboBox is bound to a DataTable,
// so the items are DataRowView objects.
DataRowView drv = (DataRowView)this.comboBox1.Items[args.Index];
// Retrieve the value of each column.
string id = drv["id"].ToString();
string name = drv["name"].ToString();
string name2 = drv["name2"].ToString();
string Address = drv["Address"].ToString();
string City = drv["City"].ToString();
string State = drv["State"].ToString();
string Zip = drv["Zip"].ToString();
//********************************************************************************
// Get the bounds for the first column *
//********************************************************************************
Rectangle r1 = args.Bounds; r1.Width /= 2;
// Draw the text on the first column
using (SolidBrush sb = new SolidBrush(args.ForeColor))
{
// String, Font, Brush, Pointf
args.Graphics.DrawString(id, args.Font, sb, r1);
}
// Draw a line to isolate the columns
using (Pen p = new Pen(Color.Black))
{
// pen, point 1 (x,x), point 2 (x,x) from point one to point 2.
args.Graphics.DrawLine(p, r1.Right, 0, r1.Right, r1.Bottom);
}
//********************************************************************************
// Get the bounds for the second column *
//********************************************************************************
Rectangle r2 = args.Bounds; r2.X = args.Bounds.Width / 2; r2.Width /= 2;
// Draw the text on the second column
using (SolidBrush sb = new SolidBrush(args.ForeColor))
{
// String, Font, Brush, Pointf
args.Graphics.DrawString(name, args.Font, sb, r2);
}
// Draw a line to isolate the columns
using (Pen p = new Pen(Color.Black))
{
args.Graphics.DrawLine(p, r2.Right, 0, r2.Right, r2.Bottom);
}
//********************************************************************************
// Get the bounds for the third column *
//********************************************************************************
Rectangle r3 = args.Bounds; r3.X = args.Bounds.Width / 3; r3.Width /= 3;
// Draw the text on the third column
using (SolidBrush sb = new SolidBrush(args.ForeColor))
{
args.Graphics.DrawString(name2, args.Font, sb, r3);
}
// Draw a line to isolate the columns
using (Pen p = new Pen(Color.Black))
{
args.Graphics.DrawLine(p, r3.Right, 0, r3.Right, r3.Bottom);
}
//********************************************************************************
// Get the bounds for the fourth column *
//********************************************************************************
Rectangle r4 = args.Bounds; r4.X = args.Bounds.Width / 4; r4.Width /= 4;
// Draw the text on the forth column
using (SolidBrush sb = new SolidBrush(args.ForeColor))
{
args.Graphics.DrawString(Address, args.Font, sb, r4);
}
// Draw a line to isolate the columns
using (Pen p = new Pen(Color.Black))
{
args.Graphics.DrawLine(p, r4.Right, 0, r4.Right, r4.Bottom);
}
//********************************************************************************
// Get the bounds for the fifth column *
//********************************************************************************
Rectangle r5 = args.Bounds; r5.X = args.Bounds.Width / 5; r5.Width /= 5;
// Draw the text on the fifth column
using (SolidBrush sb = new SolidBrush(args.ForeColor))
{
args.Graphics.DrawString(City, args.Font, sb, r5);
}
// Draw a line to isolate the columns
using (Pen p = new Pen(Color.Black))
{
args.Graphics.DrawLine(p, r5.Right, 0, r5.Right, r5.Bottom);
}
//********************************************************************************
// Get the bounds for the sixth column *
//********************************************************************************
Rectangle r6 = args.Bounds; r6.X = args.Bounds.Width / 6; r6.Width /= 6;
// Draw the text on the sixth column
using (SolidBrush sb = new SolidBrush(args.ForeColor))
{
args.Graphics.DrawString(State, args.Font, sb, r6);
}
// Draw a line to isolate the columns
using (Pen p = new Pen(Color.Black))
{
args.Graphics.DrawLine(p, r6.Right, 0, r6.Right, r6.Bottom);
}
//********************************************************************************
// Get the bounds for the Seventh column *
//********************************************************************************
Rectangle r7 = args.Bounds; r7.X = args.Bounds.Width / 7; r7.Width /= 7;
// Draw the text on the third column
using (SolidBrush sb = new SolidBrush(args.ForeColor))
{
args.Graphics.DrawString(Zip, args.Font, sb, r7);
}
// Draw a line to isolate the columns
using (Pen p = new Pen(Color.Black))
{
args.Graphics.DrawLine(p, r7.Right, 0, r7.Right, r7.Bottom);
}
Thanks again,
|
|
 |