본문 바로가기

   
Programming/Winform

리스트 박스, 문자열

반응형
리스트박스는 문자열이 들어가 있고 컨트롤스 컬렉션 대신 items 배열에 들어가 있다.


 using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

 

namespace Test

{

       public partial class ListBox01 : Form

       {

             public ListBox01()

             {

                    InitializeComponent();

             }

 

             private void button1_Click(object sender, EventArgs e)

             {

                    //listBox1.SelectedIndex : 항목의 위치(int)

                    //listBox1.SelectedItem : 항목 자체(String)

 

                    //선택된 항목이 반드시 존재할때만...

                    if (listBox1.SelectedIndex > -1)

                    {

                          

                    }

 

                    MessageBox.Show(listBox1.SelectedIndex.ToString());

                    MessageBox.Show(listBox1.SelectedItem.ToString());

             }

 

             private void button2_Click(object sender, EventArgs e)

             {

                    //동적으로 항목추가

                    listBox1.Items.Add("추가항목");

             }

 

             private void button3_Click(object sender, EventArgs e)

             {

                    //항목 수정하기 -> 누구?

                    listBox1.Items[2] = "three";

             }

 

             private void button4_Click(object sender, EventArgs e)

             {

                    //1. 선택된 항목이 존재하는지?

                    //2. 선택된 항목 얻어오기

                    //2. 그 항목의 값을 수정하기

                    if (listBox1.SelectedIndex > -1)

                    {

                           listBox1.Items[listBox1.SelectedIndex] = "수정";

                          

                    }

 

                    else

                    {

                           MessageBox.Show("항목을 선택하세요");

                    }

             }

 

             private void button5_Click(object sender, EventArgs e)

             {

                    if (listBox1.SelectedIndex > -1)

                    {

                           //항목 삭제

                           listBox1.Items.RemoveAt(listBox1.SelectedIndex);

                    }

 

                    else

                    {

                           MessageBox.Show("항목을 선택하세요");

                    }

             }

 

             private void button6_Click(object sender, EventArgs e)

             {

                    //특정 항목을 선택하기(클릭처럼)

                    this.listBox1.SelectedIndex = 3;

             }

 

             private void listBox1_SelectedIndexChanged(object sender, EventArgs e)

             {

                    label1.Text = listBox1.SelectedIndex.ToString();

             }

 

             private void listBox2_SelectedIndexChanged(object sender, EventArgs e)

             {

 

             }

 

             private void ListBox01_Load(object sender, EventArgs e)

             {

                    for (int i = 0; i < 9; i++)

                    {

                           listBox2.Items.Add((i * 100).ToString());    

                    }

                   

             }

 

             private void button7_Click(object sender, EventArgs e)

             {

                    //선택된 항목들

                    //listBox2.SelectedIndices

                    //listBox2.SelectedItems

 

                    for (int i = 0; i < listBox2.SelectedIndices.Count; i++)

                    {

                           label2.Text += "\r\n" + listBox2.SelectedIndices[i].ToString();

                    }

 

                    for (int i = 0; i < listBox2.SelectedItems.Count; i++)

                    {

                           label2.Text += "\r\n" + listBox2.SelectedItems[i].ToString();

                    }

             }

       }

}

 

 
 

반응형