Generic List Class Sort Method

public void Sort(Comparison<T> comparison)
http://msdn.microsoft.com/en-us/library/w56d4y5z.aspx

List<Person> pList = new List<Person>();
pList.Sort(ComparePersonByID); // pList를 ID로 정렬하기

[…]
public class Person : IComparable<Person>
{
    public static Comparison<Person> ComparePersonByID =
        delegate(Person p1, Person p2)      
        {
             return p1.ID.CompareTo(p2.ID);        
        };

[…]
}

How to: Sort ListView Items

How to: Sort ListView Items
http://msdn.microsoft.com/en-us/library/ms229643(VS.80).aspx

class ListViewItemComparer : IComparer
{
    private int column;
    public ListViewItemComparer() : this(0) {}
    public ListViewItemComparer(int column)  
    {      
        this.column = column;
     }
     
    public int Compare(object x, object y) // string compare
    {       
          return String.Compare(((ListViewItem)x).SubItems[column].Text,  
                                       ((ListViewItem)y).SubItems[column].Text);
   }
}

/////
public partial class Form1 : Form
{
[…]
private void listView1_ColumnClick(object sender, ColumnClickEventArgs e)
{
           
   // Set the column number that is to be sorted; default to ascending.            
   this.listView1.ListViewItemSorter = new ListViewItemComparer(e.Column);
           
   // Perform the sort with these new sort options.
   this.listView1.Sort();
}
}

To Prevent Multiple Show of the same Modeless Form

private void findToolStripMenuItem_Click(object sender, EventArgs e)
{
   // 현재 열려있는 비모달형 폼 중복생성 방지
   bool formExist = false;

   foreach (Form f in Application.OpenForms)
   {
     if (f.GetType() == typeof(
FindPersonForm))
     {
       f.Activate();
       formExist = true;
     }
  }
 
  if (!formExist)
 
{
     FindPersonForm f = new FindPersonForm();
     f.Owner = this;
     f.Show();
     f.Activate();
   }
}

PersonListViewDialog

PersonListViewDialog
public List<Person> pList = new List<Person>();

-FileIO
       private void LoadFile(string path)
        {
            if (File.Exists(path))
            {
                System.Diagnostics.Trace.WriteLine(path);

                // 한글처리시 필요함
                using (StreamReader sr = new StreamReader(path, System.Text.Encoding.Default))
                {
                    string line = “”;
                    char[] delimiter = { ‘,’ };
                    while ((line = sr.ReadLine()) != null)
                    {
                        Console.WriteLine(line);
                        // cvs file format parse
                        string[] items = line.Split(delimiter);
                        foreach (string s in items)
                        {
                            Console.WriteLine(s);
                        }
                        // add Person into the List
                        Person p = new Person();
                        p.Name = items[0];
                        p.ID = int.Parse(items[1]);
                        p.Phone = items[2];
                        p.Address = items[3];
                        pList.Add(p);
                        // add Person into ListView Control
                        listView1.Items.Add(p.ToListViewItem());
                    }
                    sr.Close();
                }
            }
        }

        private void WriteFile(string path)
        {
            // 한글처리시 필요함
            using (StreamWriter sw = new StreamWriter(path, false, System.Text.Encoding.Default))
            {
                foreach (Person p in pList)
                    sw.WriteLine(p);                // Person에 ToString() 사용
                sw.Close();
            }
        }

-Modal형 AddDialog
-Modeless형 FindDialog

PersonListView

PersonListView
-Person 정보 FileIO (csv format) 파일 읽기, 저장하기
-리스트뷰에 파일읽어서 열기 및 저장하기
-여기에 리스트뷰의 칼럼을 클릭하면 그 순서대로 전체 항목을 정렬하기 추가해보기
-여기에 사람정보 (이름, ID, 전화번호, 주소) 입력을 위한 TextBox. 그리고 ID는 숫자만 입력가능한 모달형 대화상자 추가해보기
-여기에 사람을 찾는 모달리스형 대화상자 추가해보기

Draw Shapes to Bitmap in Memory

도형그리기를 비트맵이미지로 그리기

private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;

// Create a Bitmap image in memory and set its CompositingMode
Bitmap bmp = new Bitmap(this.Size.Width, this.Size.Height,
PixelFormat.Format32bppArgb);
Graphics gBmp = Graphics.FromImage(bmp);
gBmp.CompositingMode = CompositingMode.SourceCopy;

// Pen으로 대각선과 사각형 그리기 (to bitmap in memory)
Pen p = new Pen(Color.Black, 3);
Rectangle r = new Rectangle(10, 10, this.Size.Width/2, this.Size.Height/2);
gBmp.DrawLine(p, 10, 10, this.Size.Width / 2 + 10, this.Size.Height / 2 + 10);
gBmp.DrawLine(p, this.Size.Width / 2 + 10, 10, 10, this.Size.Height / 2 + 10);
gBmp.DrawRectangle(p, r);

// Brush로 파란사각형 그리기 (to bitmap in memory)
Brush b = new SolidBrush(Color.Blue);
Rectangle r2 = new Rectangle(this.Size.Width / 2 + 10, this.Size.Height / 2 + 10,
this.Size.Width / 2 – 10, this.Size.Height / 2 – 10);
gBmp.FillRectangle(b, r2);

// Brush로 빨간삼각형 그리기 (to bitmap in memory)
Brush b2 = new SolidBrush(Color.Red);
Point[] pt = new Point[3];
pt[0].X = 400; pt[0].Y = 10;
pt[1].X = 300; pt[1].Y = 210;
pt[2].X = 500; pt[2].Y = 210;
gBmp.FillPolygon(b2, pt);

// HatchBrush로 타원 그리기 (to bitmap in memory)
HatchBrush hb = new HatchBrush(HatchStyle.DiagonalCross,
Color.Yellow, Color.Brown);
gBmp.FillEllipse(hb, 10, 220, 200, 100);

// DrawArc 메소드 사용하여 호 그리기 (to bitmap in memory)
Pen p2 = new Pen(Color.Green, 3);
Rectangle r3 = new Rectangle(220, 220, 200, 100);
gBmp.DrawArc(p2, r3, 0, 180);

// draw the bitmap on our window
g.DrawImage(bmp, 0, 0, bmp.Width, bmp.Height);
bmp.Dispose();
gBmp.Dispose();
}

Convert (Longitude, Latitude) to (X, Y) on a map

// Get the latitude as a value between 0 and 180,
// where the negative values represent values South of the Equator
double latitude = lat + 90.0;
// Get the longitude as a value between 0 and 360,
// where the negative values represent values West of the Greenwich Meridian.
double longitude = lon + 180.0;

// calculate how many pixels are needed to represent each degree for width and height
double pixelsWidth = map.Width/360.0;
double pixelsHeight = map.Height/180.0;

// Calculate how many pixels we need to represent our latitude value
double latitudePixels = latitude * pixelsHeight;
// Calculate how many pixels we need to represent our longitude value
double longitudePixels = longitude * pixelsWidth;

// The X coordinate of our point is the longitudePixels value
float x = (float)longitudePixels;
// The Y coordinate of our point is 180-latitude
// since the Y axis is facing down but the map is facing up
float y = (float) ((180.0 * pixelsHeight) – latitudePixels);