ARIDITY INDEX

건조지수(Aridity Index)를 구하고, 기후의 상태를 알려주는 프로그램을 작성한다.

4534340817

(1) 사용자에게 연강수량(Precipitation)과 연평균기온(Temperature)를 입력 받는다. 연강수량의 단위는 mm이고, 연평균기온의 단위는 oC이다.

(2) 건조지수를 다음 공식으로 계산하고 결과를 화면에 출력한다.
건조지수 De Martonne Aridity Index = Precipitation/ (Temperature + 10)

(3) 다음 기준에 따라 기후의 상태를 판단해서 화면에 출력한다.
AI >= 60 => Perhumid
30 <= AI < 60 => Humid
20 <= AI < 30 => SubHumid
15 <= AI < 20 => SemiArid
5 <= AI < 15 => Arid
AI < 5 => ExtremelyArid

AridityIndexForm

C# Windows Forms Controls

Label, Button 사용예
http://dis.dankook.ac.kr/lectures/hci09/entry/Controls

Buttons
http://dis.dankook.ac.kr/lectures/hci09/entry/Buttons

GroupBox
http://dis.dankook.ac.kr/lectures/hci09/entry/GroupBox

TextBox
http://dis.dankook.ac.kr/lectures/hci09/entry/TextBox

LinkedLabel
http://dis.dankook.ac.kr/lectures/hci09/entry/Linked-Label

ListBox

http://dis.dankook.ac.kr/lectures/hci09/entry/ListBox

Poll
http://dis.dankook.ac.kr/lectures/hci09/entry/Poll

PictureBox
http://dis.dankook.ac.kr/lectures/hci09/entry/Picture-Timer

TabControl
http://dis.dankook.ac.kr/lectures/hci09/entry/Tab-Control

TreeControl
http://dis.dankook.ac.kr/lectures/hci09/entry/Tree-Control

ListView
http://dis.dankook.ac.kr/lectures/hci09/entry/ListView

Textbox (Number Only)

http://dis.dankook.ac.kr/lectures/hci09/entry/Number-Only-Textbox

Only Double Number Textbox

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && (e.KeyChar != ‘.’))
{
e.Handled = true;
}

// only allow one decimal point
if ((e.KeyChar == ‘.’) && ((sender as TextBox).Text.IndexOf(‘.’) > -1))
{
e.Handled = true;
}
}

Valid double number in C#

텍스트 박스에 “12.5”, “.3”, “-12” 등과 같은 양수/음수의 double 숫자만 입력받을 수 있도록 하려면
(그외에 “abcd”같은 문자나 “12.3.4” 같은 이상한 숫자는 입력받을 수 없도록하기위해)

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
// accept only digit(0-9) with ‘.’ (dot) and ‘-‘ (minus)
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != ‘.’ && e.KeyChar != ‘-‘)
e.Handled = true;
// accept only one decimal point (.32)
if (e.KeyChar == ‘.’ && (sender as TextBox).Text.IndexOf(‘.’) > -1)
e.Handled = true;
// accept only minus sign at the beginning
if (e.KeyChar == ‘-‘ && (sender as TextBox).Text.Length > 0)
e.Handled = true;
}

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

[…]
}

pList.Sort(ComparePersonByID); // pList를 ID로 정렬하기 (Comparison delegate)

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

[…]
}

pList.Sort(new PersonIDComparer()); // pList를 ID로 정렬하기 (IComparer)

public class PersonIDComparer : IComparer<Person>
{
public int Compare(Person p1, Person p2)
{
return p1.ID.CompareTo(p2.ID);
}
}

pList.Sort((p, q) => p.ID.CompareTo(q.ID)); // pList를 ID로 정렬하기 (lambda)