Wednesday, April 06, 2005

Draw a Line between Two Checked List Items of Two CheckedListBoxes

I saw a message on the SLDotnetDevelopers2005 ( http://groups.yahoo.com/group/SLDotNetDevelopers2005/ ) posted by a sri lankan developer named shiran.
Shiran wanted to draw a line between two checkedlistItems of two checkedlistboxes on a windows form. Similar to the SQL server Design Query View. Given below is my answer I gave to him.
( Hey hey shiran you must be upto something very interesting .....??? )
Hello Shiran,
I have given below the most primitive approch that you can get this solved using the GDI+.
I think you may have time to improve it more to suite your need.
////////////////////////
//Add Items to the 1st CheckedListBox
checkedListBox1.Items.Add"ABC",true);
checkedListBox1.Items.Add("DEF",true);
checkedListBox1.Items.Add("GHI",true);
checkedListBox1.Items.Add("JKL",true);
//Add Items to the 2nd CheckedListBox
checkedListBox2.Items.Add("123",true);
checkedListBox2.Items.Add("456",true);
checkedListBox2.Items.Add("789",true);
checkedListBox2.Items.Add("111",true);
checkedListBox2.Items.Add("222",true);
checkedListBox2.Items.Add("333",true);
// Get the Fourth Items Rectangle of the First CheckedListBox
System.Drawing.Rectangle FromRectangle = this.RectangleToClient(checkedListBox1.RectangleToScreen(checkedListBox1.GetItemRectangle(3)));
// Get the Third Items Rectangle of the Second CheckedListBox
System.Drawing.Rectangle ToRectangle = this.RectangleToClient(checkedListBox2.RectangleToScreen(checkedListBox2.GetItemRectangle(2)));
//Create a Graphics Object
System.Drawing.Graphics graphics = this.CreateGraphics();
//Create Points From Screen Rectangles
Point FromPoint = FromRectangle.Location;
Point ToPoint = ToRectangle.Location;
//Draw the Line From Start Point To the End Point
graphics.DrawLine(Pens.Black,
FromRectangle.X,
FromRectangle.Y,
ToRectangle.X,
ToRectangle.Y);
//Dispose Graphics Object
graphics.Dispose();
////////////////////////////////
If you have any more questions do not hesitate to throw it to us.
Bye
Lolitha Samarajeewa
P.S:
I have searched google for you and came up with one link(since i don't have much time these days ;-) )
You better search on MSDN also where you get more samples.

0 Comments:

Post a Comment

<< Home