첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.

728x90
반응형
728x170

TestProject.zip
0.02MB

▶ BTreeNode.cs

using System;
using System.Windows.Forms;

namespace TestProject
{
    /// <summary>
    /// B 트리 노드
    /// </summary>
    public class BTreeNode<T>
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region Field

        /// <summary>
        /// 사용 키 카운트
        /// </summary>
        public int KeyCountUsed = 0;

        /// <summary>
        /// 키 배열
        /// </summary>
        public string[] KeyArray = new string[KEY_COUNT_PER_NODE];

        /// <summary>
        /// 값 배열
        /// </summary>
        public T[] ValueArray = new T[KEY_COUNT_PER_NODE];

        /// <summary>
        /// 자식 배열
        /// </summary>
        public BTreeNode<T>[] ChildArray = new BTreeNode<T>[CHILD_COUNT_PER_NODE];

        #endregion

        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 키 1/2 카운트
        /// </summary>
        private const int KEY_HALF_COUNT = 2;

        /// <summary>
        /// 노드당 키 카운트
        /// </summary>
        private const int KEY_COUNT_PER_NODE = 2 * KEY_HALF_COUNT;

        /// <summary>
        /// 노드당 자식 카운트
        /// </summary>
        private const int CHILD_COUNT_PER_NODE = KEY_COUNT_PER_NODE + 1;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 항목 추가하기 - AddItem(key, value, parentKey, parentValue, parentNode)

        /// <summary>
        /// 항목 추가하기
        /// </summary>
        /// <param name="key">키</param>
        /// <param name="value">값</param>
        /// <param name="parentKey">부모 키</param>
        /// <param name="parentValue">부모 값</param>
        /// <param name="parentNode">부모 노드</param>
        public void AddItem(string key, T value, ref string parentKey, ref T parentValue, ref BTreeNode<T> parentNode)
        {
            int spot = 0;

            while(spot < KeyCountUsed)
            {
                if(string.Compare(KeyArray[spot], key) >= 0)
                {
                    break;
                }

                spot++;
            }

            if((spot < KeyCountUsed) && (KeyArray[spot] == key))
            {
                throw new Exception($"Insert error. Cannot insert item with duplicate key value '{key}'");
            }

            if(ChildArray[0] == null)
            {
                AddItemToNode(spot, key, value, null, ref parentKey, ref parentValue, ref parentNode);
            }
            else
            {
                ChildArray[spot].AddItem(key, value, ref parentKey, ref parentValue, ref parentNode);

                if(parentNode != null)
                {
                    AddItemToNode(spot, parentKey, parentValue, parentNode, ref parentKey, ref parentValue, ref parentNode);
                }
            }
        }

        #endregion
        #region 항목 제거하기 - RemoveItem(key)

        /// <summary>
        /// 항목 제거하기
        /// </summary>
        /// <param name="key">키</param>
        public void RemoveItem(string key)
        {
            int spot = 0;

            while(spot < KeyCountUsed)
            {
                if(string.Compare(KeyArray[spot], key) >= 0)
                {
                    break;
                }

                spot++;
            }

            if((spot < KeyCountUsed) && (KeyArray[spot] == key))
            {
                if(ChildArray[0] == null)
                {
                    RemoveItemFromNode(spot);
                }
                else
                {
                    string rightMostKey = string.Empty;

                    T rightMostValue = default(T);

                    ChildArray[spot].SwapRightMost(key, ref rightMostKey, ref rightMostValue);

                    KeyArray  [spot] = rightMostKey;
                    ValueArray[spot] = rightMostValue;

                    ChildArray[spot].RemoveItem(key);
                }
            }
            else
            {
                if(ChildArray[0] == null)
                {
                    throw new Exception($"Delete error. Cannot find item with key value '{key}'");
                }

                ChildArray[spot].RemoveItem(key);
            }

            if(ChildArray[0] != null)
            {
                if(ChildArray[spot].KeyCountUsed < KEY_HALF_COUNT)
                {
                    if((spot > 0) && (ChildArray[spot - 1].KeyCountUsed > KEY_HALF_COUNT))
                    {
                        RebalanceSiblings(ChildArray[spot - 1], ChildArray[spot], ref KeyArray[spot - 1], ref ValueArray[spot - 1]);
                    }
                    else if((spot < KEY_HALF_COUNT - 1) && (ChildArray[spot + 1].KeyCountUsed > KEY_HALF_COUNT))
                    {
                        RebalanceSiblings(ChildArray[spot], ChildArray[spot + 1], ref KeyArray[spot], ref ValueArray[spot]);
                    }
                    else
                    {
                        if(spot > 0)
                        {
                            MergeSiblings(spot - 1, spot, KeyArray[spot - 1], ValueArray[spot - 1]);
                        }
                        else
                        {
                            MergeSiblings(spot, spot + 1, KeyArray[spot], ValueArray[spot]);
                        }
                    }
                }
            }
        }

        #endregion
        #region 항목 찾기 - FindItem(key)

        /// <summary>
        /// 항목 찾기
        /// </summary>
        /// <param name="key">키</param>
        /// <returns>항목</returns>
        public T FindItem(string key)
        {
            int spot = 0;

            while(spot < KeyCountUsed)
            {
                if(string.Compare(KeyArray[spot], key) >= 0)
                {
                    break;
                }

                spot++;
            }

            if((spot < KeyCountUsed) && (KeyArray[spot] == key))
            {
                return ValueArray[spot];
            }

            if(ChildArray[spot] == null)
            {
                return default(T);
            }

            return ChildArray[spot].FindItem(key);
        }

        #endregion

        #region 리스트 박스에 추가하기 - AddToListBox(listBox, int indent)

        /// <summary>
        /// 리스트 박스에 추가하기
        /// </summary>
        /// <param name="listBox">리스트 박스</param>
        /// <param name="indent"></param>
        public void AddToListBox(ListBox listBox, int indent)
        {
            for(int i = 0; i < KeyCountUsed; i++)
            {
                if(ChildArray[i] != null)
                {
                    ChildArray[i].AddToListBox(listBox, indent + 4);
                }

                listBox.Items.Add(new string(' ', indent) + ValueArray[i].ToString());
            }

            if(KeyCountUsed > 0)
            {
                if(ChildArray[KeyCountUsed] != null)
                {
                    ChildArray[KeyCountUsed].AddToListBox(listBox, indent + 4);
                }
            }
        }

        #endregion

        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region 공간을 갖는 노드에서 항목 추가하기 - AddItemInNodeWithRoom(spot, key, value, node)

        /// <summary>
        /// 공간을 갖는 노드에서 항목 추가하기
        /// </summary>
        /// <param name="spot">스팟</param>
        /// <param name="key">키</param>
        /// <param name="value">값</param>
        /// <param name="node">노드</param>
        private void AddItemInNodeWithRoom(int spot, string key, T value, BTreeNode<T> node)
        {
            Array.Copy(KeyArray  , spot    , KeyArray  , spot + 1, KeyCountUsed - spot);
            Array.Copy(ValueArray, spot    , ValueArray, spot + 1, KeyCountUsed - spot);
            Array.Copy(ChildArray, spot + 1, ChildArray, spot + 2, KeyCountUsed - spot);

            KeyArray[spot] = key;

            ValueArray[spot] = value;

            ChildArray[spot + 1] = node;

            KeyCountUsed++;
        }

        #endregion
        #region 노드 분리하기 - SplitNode(spot, key, value, node, praentKey, parentValue, parentNode)

        /// <summary>
        /// 노드 분리하기
        /// </summary>
        /// <param name="spot">스팟</param>
        /// <param name="key">키</param>
        /// <param name="value">값</param>
        /// <param name="node">노드</param>
        /// <param name="praentKey">부모 키</param>
        /// <param name="parentValue">부모 값</param>
        /// <param name="parentNode">부모 노드</param>
        private void SplitNode(int spot, string key, T value, BTreeNode<T> node, ref string praentKey, ref T parentValue, ref BTreeNode<T> parentNode)
        {
            string[] keyArray = new string[KEY_COUNT_PER_NODE + 1];

            Array.Copy(KeyArray, 0, keyArray, 0, spot);

            keyArray[spot] = key;

            Array.Copy(KeyArray, spot, keyArray, spot + 1, KEY_COUNT_PER_NODE - spot);

            T[] valueArray = new T[KEY_COUNT_PER_NODE + 1];

            Array.Copy(ValueArray, 0, valueArray, 0, spot);

            valueArray[spot] = value;

            Array.Copy(ValueArray, spot, valueArray, spot + 1, KEY_COUNT_PER_NODE - spot);

            BTreeNode<T>[] nodeArray = new BTreeNode<T>[CHILD_COUNT_PER_NODE + 1];

            Array.Copy(ChildArray, 0, nodeArray, 0, spot + 1);

            nodeArray[spot + 1] = node;

            Array.Copy(ChildArray, spot + 1, nodeArray, spot + 2, KEY_COUNT_PER_NODE - spot);

            Array.Copy(keyArray  , 0, KeyArray  , 0, KEY_HALF_COUNT    );
            Array.Copy(valueArray, 0, ValueArray, 0, KEY_HALF_COUNT    );
            Array.Copy(nodeArray , 0, ChildArray, 0, KEY_HALF_COUNT + 1);

            Array.Clear(KeyArray  , KEY_HALF_COUNT    , KEY_HALF_COUNT);
            Array.Clear(ValueArray, KEY_HALF_COUNT    , KEY_HALF_COUNT);
            Array.Clear(ChildArray, KEY_HALF_COUNT + 1, KEY_HALF_COUNT);

            KeyCountUsed = KEY_HALF_COUNT;

            praentKey   = keyArray[KEY_HALF_COUNT];
            parentValue = valueArray[KEY_HALF_COUNT];
            parentNode  = new BTreeNode<T>();

            Array.Copy(keyArray  , KEY_HALF_COUNT + 1, parentNode.KeyArray  , 0, KEY_HALF_COUNT    );
            Array.Copy(valueArray, KEY_HALF_COUNT + 1, parentNode.ValueArray, 0, KEY_HALF_COUNT    );
            Array.Copy(nodeArray , KEY_HALF_COUNT + 1, parentNode.ChildArray, 0, KEY_HALF_COUNT + 1);

            parentNode.KeyCountUsed = KEY_HALF_COUNT;
        }

        #endregion
        #region 노드에 항목 추가하기 - AddItemToNode(spot, key, value, node, parentkey, parentValue, parentNode)

        /// <summary>
        /// 노드에 항목 추가하기
        /// </summary>
        /// <param name="spot">스팟</param>
        /// <param name="key">키</param>
        /// <param name="value">값</param>
        /// <param name="node">노드</param>
        /// <param name="parentkey">부모 키</param>
        /// <param name="parentValue">부모 값</param>
        /// <param name="parentNode">부모 노드</param>
        private void AddItemToNode(int spot, string key, T value, BTreeNode<T> node, ref string parentkey, ref T parentValue, ref BTreeNode<T> parentNode)
        {
            if(KeyCountUsed < KEY_COUNT_PER_NODE)
            {
                AddItemInNodeWithRoom(spot, key, value, node);

                parentkey   = null;
                parentValue = default(T);
                parentNode  = null;
            }
            else
            {
                SplitNode(spot, key, value, node, ref parentkey, ref parentValue, ref parentNode);
            }
        }

        #endregion
        #region 노드에서 항목 제거하기 - RemoveItemFromNode(spot)

        /// <summary>
        /// 노드에서 항목 제거하기
        /// </summary>
        /// <param name="spot">스팟</param>
        private void RemoveItemFromNode(int spot)
        {
            Array.Copy(KeyArray  , spot + 1, KeyArray  , spot    , KeyCountUsed - spot - 1);
            Array.Copy(ValueArray, spot + 1, ValueArray, spot    , KeyCountUsed - spot - 1);
            Array.Copy(ChildArray, spot + 2, ChildArray, spot + 1, KeyCountUsed - spot - 1);

            KeyCountUsed--;

            KeyArray  [KeyCountUsed    ] = null;
            ValueArray[KeyCountUsed    ] = default(T);
            ChildArray[KeyCountUsed + 1] = null;
        }

        #endregion
        #region 가장 오른쪽 노드와 교환하기 - SwapRightMost(key, rightMostKey, rightMostValue)

        /// <summary>
        /// 가장 오른쪽 노드와 교환하기
        /// </summary>
        /// <param name="key">키</param>
        /// <param name="rightMostKey">가장 오른쪽 키</param>
        /// <param name="rightMostValue">가장 오른쪽 값</param>
        private void SwapRightMost(string key, ref string rightMostKey, ref T rightMostValue)
        {
            if(ChildArray[0] == null)
            {
                rightMostKey   = KeyArray  [KeyCountUsed - 1];
                rightMostValue = ValueArray[KeyCountUsed - 1];

                KeyArray[KeyCountUsed - 1] = key;
            }
            else
            {
                ChildArray[KeyCountUsed].SwapRightMost(key, ref rightMostKey, ref rightMostValue);
            }
        }

        #endregion
        #region 이웃과 재조정하기 - RebalanceSiblings(leftNode, rightNode, middleKey, middleValue)

        /// <summary>
        /// 이웃과 재조정하기
        /// </summary>
        /// <param name="leftNode">왼쪽 노드</param>
        /// <param name="rightNode">오른쪽 노드</param>
        /// <param name="middleKey">중간 키</param>
        /// <param name="middleValue">중간 값</param>
        private static void RebalanceSiblings(BTreeNode<T> leftNode, BTreeNode<T> rightNode, ref string middleKey, ref T middleValue)
        {
            int keyCount = leftNode.KeyCountUsed + rightNode.KeyCountUsed + 1;

            int middleIndex = leftNode.KeyCountUsed;

            string[] keyArray = new string[keyCount];

            Array.Copy(leftNode.KeyArray, 0, keyArray, 0, leftNode.KeyCountUsed);

            keyArray[middleIndex] = middleKey;

            Array.Copy(rightNode.KeyArray, 0, keyArray, middleIndex + 1, rightNode.KeyCountUsed);

            T[] valueArray = new T[keyCount];

            Array.Copy(leftNode.ValueArray, 0, valueArray, 0, leftNode.KeyCountUsed);

            valueArray[middleIndex] = middleValue;

            Array.Copy(rightNode.ValueArray, 0, valueArray, middleIndex + 1, rightNode.KeyCountUsed);

            BTreeNode<T>[] childArray = new BTreeNode<T>[keyCount + 1];

            Array.Copy(leftNode.ChildArray , 0, childArray, 0              , leftNode.KeyCountUsed  + 1);
            Array.Copy(rightNode.ChildArray, 0, childArray, middleIndex + 1, rightNode.KeyCountUsed + 1);

            int leftCount = (int)((keyCount - 1) / 2);

            Array.Copy(keyArray  , 0, leftNode.KeyArray  , 0, leftCount    );
            Array.Copy(valueArray, 0, leftNode.ValueArray, 0, leftCount    );
            Array.Copy(childArray, 0, leftNode.ChildArray, 0, leftCount + 1);

            Array.Clear(leftNode.KeyArray  , leftCount    , KEY_COUNT_PER_NODE - leftCount);
            Array.Clear(leftNode.ValueArray, leftCount    , KEY_COUNT_PER_NODE - leftCount);
            Array.Clear(leftNode.ChildArray, leftCount + 1, KEY_COUNT_PER_NODE - leftCount);

            leftNode.KeyCountUsed = leftCount;

            middleKey   = keyArray  [leftCount];
            middleValue = valueArray[leftCount];

            int rightCount = keyCount - 1 - leftCount;

            Array.Copy(keyArray  , leftCount + 1, rightNode.KeyArray  , 0, rightCount    );
            Array.Copy(valueArray, leftCount + 1, rightNode.ValueArray, 0, rightCount    );
            Array.Copy(childArray, leftCount + 1, rightNode.ChildArray, 0, rightCount + 1);

            rightNode.KeyCountUsed = rightCount;
        }

        #endregion
        #region 이웃과 병합하기 - MergeSiblings(leftSpot, rightSpot, middleKey, middleValue)

        /// <summary>
        /// 이웃과 병합하기
        /// </summary>
        /// <param name="leftSpot">왼쪽 스팟</param>
        /// <param name="rightSpot">오른쪽 스팟</param>
        /// <param name="middleKey">중간 키</param>
        /// <param name="middleValue">중간 값</param>
        private void MergeSiblings(int leftSpot, int rightSpot, string middleKey, T middleValue)
        {
            int middleIndex = ChildArray[leftSpot].KeyCountUsed;

            ChildArray[leftSpot].KeyArray  [middleIndex] = middleKey;
            ChildArray[leftSpot].ValueArray[middleIndex] = middleValue;

            Array.Copy(ChildArray[rightSpot].KeyArray  , 0, ChildArray[leftSpot].KeyArray  , middleIndex + 1, ChildArray[rightSpot].KeyCountUsed    );
            Array.Copy(ChildArray[rightSpot].ValueArray, 0, ChildArray[leftSpot].ValueArray, middleIndex + 1, ChildArray[rightSpot].KeyCountUsed    );
            Array.Copy(ChildArray[rightSpot].ChildArray, 0, ChildArray[leftSpot].ChildArray, middleIndex + 1, ChildArray[rightSpot].KeyCountUsed + 1);

            ChildArray[leftSpot].KeyCountUsed += ChildArray[rightSpot].KeyCountUsed + 1;

            Array.Copy(KeyArray  , leftSpot  + 1, KeyArray  , leftSpot , KeyCountUsed - leftSpot - 1);
            Array.Copy(ValueArray, leftSpot  + 1, ValueArray, leftSpot , KeyCountUsed - leftSpot - 1);
            Array.Copy(ChildArray, rightSpot + 1, ChildArray, rightSpot, KeyCountUsed - rightSpot   );

            KeyCountUsed--;

            KeyArray  [KeyCountUsed    ] = null;
            ValueArray[KeyCountUsed    ] = default(T);
            ChildArray[KeyCountUsed + 1] = null;
        }

        #endregion
    }
}

 

728x90

 

▶ BTree.cs

using System.Windows.Forms;

namespace TestProject
{
    /// <summary>
    /// B 트리
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class BTree<T>
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 루트 노드
        /// </summary>
        private BTreeNode<T> rootNode = new BTreeNode<T>();

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 항목 추가하기 - AddItem(key, value)

        /// <summary>
        /// 항목 추가하기
        /// </summary>
        /// <param name="key">키</param>
        /// <param name="value">값</param>
        public void AddItem(string key, T value)
        {
            string       parentKey   = string.Empty;
            T            parentValue = default(T);
            BTreeNode<T> parentNode  = null;

            this.rootNode.AddItem(key, value, ref parentKey, ref parentValue, ref parentNode);

            if(parentNode != null)
            {
                BTreeNode<T> rootNode = new BTreeNode<T>();

                rootNode.KeyArray  [0] = parentKey;
                rootNode.ValueArray[0] = parentValue;
                rootNode.ChildArray[0] = this.rootNode;
                rootNode.ChildArray[1] = parentNode;
                rootNode.KeyCountUsed  = 1;

                this.rootNode = rootNode;
            }
        }

        #endregion
        #region 항목 제거하기 - RemoveItem(key)

        /// <summary>
        /// 항목 제거하기
        /// </summary>
        /// <param name="key">키</param>
        public void RemoveItem(string key)
        {
            this.rootNode.RemoveItem(key);

            if(this.rootNode.KeyCountUsed < 1)
            {
                if(this.rootNode.ChildArray[0] != null)
                {
                    this.rootNode = this.rootNode.ChildArray[0];
                }
            }
        }

        #endregion
        #region 항목 찾기 - FindItem(key)

        /// <summary>
        /// 항목 찾기
        /// </summary>
        /// <param name="key">키</param>
        /// <returns>항목</returns>
        public T FindItem(string key)
        {
            return this.rootNode.FindItem(key);
        }

        #endregion

        #region 리스트 박스에 추가하기 - AddToListBox(listBox)

        /// <summary>
        /// 리스트 박스에 추가하기
        /// </summary>
        /// <param name="listBox">리스트 박스</param>
        public void AddToListBox(ListBox listBox)
        {
            this.rootNode.AddToListBox(listBox, 0);
        }

        #endregion

        #region 문자열 구하기 - ToString()

        /// <summary>
        /// 문자열 구하기
        /// </summary>
        /// <returns>문자열</returns>
        public override string ToString()
        {
            return this.rootNode.ToString();
        }

        #endregion
    }
}

 

300x250

 

▶ MainForm.cs

using Microsoft.VisualBasic;
using System;
using System.Windows.Forms;

namespace TestProject
{
    /// <summary>
    /// 메인 폼
    /// </summary>
    public partial class MainForm : Form
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// B 트리
        /// </summary>
        private BTree<Person> bTree = new BTree<Person>();

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainForm()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainForm()
        {
            InitializeComponent();

            this.addButton.Click              += addButton_Click;
            this.removeButtton.Click          += removeButton_Click;
            this.addRandomButton.Click        += addRandomButton_Click;
            this.findButton.Click             += findButton_Click;
            this.listBox.SelectedIndexChanged += listBox_SelectedIndexChanged;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 추가 버튼 클릭시 처리하기 - addButton_Click(sender, e)

        /// <summary>
        /// 추가 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void addButton_Click(object sender, EventArgs e)
        {
            try
            {
                Person person = new Person(this.ssnTextBox.Text, this.firstNameTextBox.Text, this.lastNameTextBox.Text);

                if(person.FirstName.Length == 0)
                {
                    person.FirstName = "first" + person.SSN;
                }

                if(person.LastName.Length == 0)
                {
                    person.LastName = "last" + person.SSN;
                }

                this.bTree.AddItem(person.SSN, person);

                this.listBox.Items.Clear();

                this.bTree.AddToListBox(this.listBox);
            }
            catch(Exception exception)
            {
                MessageBox.Show
                (
                    exception.Message,
                    "ERROR",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Exclamation
                );
            }

            this.ssnTextBox.Clear();
            this.firstNameTextBox.Clear();
            this.lastNameTextBox.Clear();

            this.ssnTextBox.Focus();
        }

        #endregion
        #region 제거 버튼 클릭시 처리하기 - removeButton_Click(sender, e)

        /// <summary>
        /// 제거 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void removeButton_Click(object sender, EventArgs e)
        {
            try
            {
                this.bTree.RemoveItem(this.ssnTextBox.Text);
            }
            catch(Exception exception)
            {
                MessageBox.Show
                (
                    exception.Message,
                    "ERROR",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Exclamation
                );
            }

            this.ssnTextBox.Clear();

            this.listBox.Items.Clear();

            this.bTree.AddToListBox(this.listBox);

            this.ssnTextBox.Focus();
        }

        #endregion
        #region 랜덤 추가 버튼 클릭시 처리하기 - addRandomButton_Click(sender, e)

        /// <summary>
        /// 랜덤 추가 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void addRandomButton_Click(object sender, EventArgs e)
        {
            string count = Interaction.InputBox("사람 수를 입력해 주시기 바랍니다.", "CONFIRMATION", "", -1, -1);

            if(count.Length == 0)
            {
                return;
            }

            for(int i = 1; i <= int.Parse(count); i++)
            {
                Person person = new Person();

                this.bTree.AddItem(person.SSN, person);
            }

            this.listBox.Items.Clear();

            this.bTree.AddToListBox(this.listBox);

            this.ssnTextBox.Focus();
        }

        #endregion
        #region 찾기 버튼 클릭시 처리하기 - findButton_Click(sender, e)

        /// <summary>
        /// 찾기 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void findButton_Click(object sender, EventArgs e)
        {
            Person person = this.bTree.FindItem(this.ssnTextBox.Text);

            if(person == null)
            {
                MessageBox.Show
                (
                    "트리에 이 사람이 없습니다.",
                    "INFORMATION",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Information
                );
            }
            else
            {
                MessageBox.Show
                (
                    person.SSN + "\n" + person.FirstName + "\n" + person.LastName,
                    "INFORMATION",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Information
                );
            }

            this.ssnTextBox.Focus();
        }

        #endregion
        #region 리스트 박스 선택 인덱스 변경시 처리하기 - listBox_SelectedIndexChanged(sender, e)

        /// <summary>
        /// 리스트 박스 선택 인덱스 변경시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void listBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.ssnTextBox.Clear();
            this.firstNameTextBox.Clear();
            this.lastNameTextBox.Clear();

            if(this.listBox.SelectedIndex < 0)
            {
                return;
            }

            string text = this.listBox.SelectedItem.ToString().Trim();

            this.ssnTextBox.Text = text.Split(new char[]{':'})[0].Trim();
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요