# 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);

# Draw Shapes to Bitmap in Memory

도형그리기를 비트맵이미지로 그리기1273706404.zip
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;
pt.X = 400; pt.Y = 10;
pt.X = 300; pt.Y = 210;
pt.X = 500; pt.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();
}

# ListView Item Edit

private void editToolStripMenuItem1_Click(object sender, EventArgs e)
{
if (listView1.FocusedItem == null) return;
int index = listView1.FocusedItem.Index;
PersonForm personForm1 = new PersonForm();
personForm1.Owner = this;
personForm1.textBox1.Text = listView1.Items[index].SubItems.Text;
personForm1.textBox2.Text = listView1.Items[index].SubItems.Text;
personForm1.textBox3.Text = listView1.Items[index].SubItems.Text;
personForm1.textBox4.Text = listView1.Items[index].SubItems.Text;
if (personForm1.ShowDialog() == DialogResult.OK)
{
Person p = new Person();
p.Name = personForm1.textBox1.Text;
p.ID = int.Parse(personForm1.textBox2.Text);
p.Phone = personForm1.textBox3.Text;
pList[index] = p;
listView1.Items[index] = p.ToListViewItem();
}
}

# 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();
}
}

# Number Only Textbox

// number only textbox using TryParse
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
int value = 0;
e.Handled = !int.TryParse(e.KeyChar.ToString(), out value);
}

// number only textbox using Regex.IsMatch
private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), “\\d+”))
e.Handled = true;
}

// NumberTextBoxLib custom control (inherited from TextBox)
public partial class NumberTextBox : TextBox
{
public NumberTextBox()
{
InitializeComponent();
}

protected override void OnKeyPress(KeyPressEventArgs e)
{
base.OnKeyPress(e);
int value = 0;
e.Handled = !int.TryParse(e.KeyChar.ToString(), out value);
}
}2213568150.zip

# 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();
}
}

# 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);
};

[…]
}

# IEquatable.Equals

http://msdn.microsoft.com/ko-kr/library/ms131190(VS.95).aspx

// Person 클래스
public class Person : IEquatable<Person>
{
// …
public Person(string name, int age)
{
_name = name;
_age = age;
}

public override string ToString()
{
return string.Format(“이름 : {0}\t나이 : {1}”, _name, _age);
}

// needed for ==
public static bool operator == (Person p, Person q)
{
return p.Equals(q);
}

public static bool operator != (Person p, Person q)
{
return !p.Equals(q);
}

public override int GetHashCode()
{
return base.GetHashCode();
}

public override bool Equals(object obj)
{
return Equals((Person)obj);
}

public bool Equals(Person other)
{
if (object.ReferenceEquals(this, other))
return true;
return (Name==other.Name) && (Age==other.Age);
}
}

// aList에 포함된 세 사람 중에 둘리와 일치하는 사람이 있는지 확인한다
private static void Main(string[] args)
{
List<Person> aList = new List<Person>();

Person s = new Person(“둘리”, 1000);
bool result = false;
foreach (Person p in aList)
{
if (p == s)
{
result = true;
break;
}
}

// 또는 foreach를 사용하는 방법대신, List에 Find 또는 FindIndex를 사용한다.
Person f = aList.Find(delegate(Person o)
{
return o == s; // 리스트에 o가 s와 일치하는 것이 있으면 그 Person 반환
}
);

int index = aList.FindIndex(delegate(Person o)
{
return o == s; // 리스트에 o가 s와 일치하는 것이 있으면 그 index 반환
}
);

}