Category Archives: C# Programming

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[0].Text;
            personForm1.textBox2.Text = listView1.Items[index].SubItems[1].Text;
            personForm1.textBox3.Text = listView1.Items[index].SubItems[2].Text;
            personForm1.textBox4.Text = listView1.Items[index].SubItems[3].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;
                p.Address = personForm1.textBox4.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

GDI+

GDI+ A Higher Level API
http://www.csharphelp.com/archives3/files/archive593/GDI.pdf

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

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

Image
http://dis.dankook.ac.kr/lectures/hci09/entry/DrawImage

ImageAttribute
http://dis.dankook.ac.kr/lectures/hci09/entry/ImageAttributes

ImageTransform
http://dis.dankook.ac.kr/lectures/hci09/entry/DrawImageTransform

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

Graphic Path
http://dis.dankook.ac.kr/lectures/hci09/entry/PathScribble

Draw Freedraw line & rubber band line
http://dis.dankook.ac.kr/lectures/hci10/entry/Rubber-band-line-drawing

Draw shapes
http://dis.dankook.ac.kr/lectures/hci10/entry/Draw-Shape

DrawImageObjects
http://dis.dankook.ac.kr/lectures/hci10/entry/DrawImageObjects

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

    […]
}

PersonListView

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