ใส่โค้ด
namespace listboxAddEditDelete
{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } int indexSelecting = 0; private void Form1_Load(object sender, EventArgs e) { listBox1.Items.Add("English"); listBox1.Items.Add("Math"); listBox1.Items.Add("Science"); listBox1.SelectedIndex = 0; label1.Text = "จำนวนรายการ : " + listBox1.Items.Count.ToString(); } private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { string dataSelecting; indexSelecting = listBox1.SelectedIndex; //อ่านค่า index dataSelecting = (string)listBox1.SelectedItem; //อ่านค่าข้อมูล textBox1.Text = dataSelecting; textBox1.Focus(); textBox1.SelectAll(); } private void buttonUp_Click(object sender, EventArgs e) { if(listBox1.SelectedIndex > 0) { listBox1.SelectedIndex--; } } private void buttonDown_Click(object sender, EventArgs e) { if(listBox1.SelectedIndex < listBox1.Items.Count - 1) { listBox1.SelectedIndex++; } } private void buttonAdd_Click(object sender, EventArgs e) { if(textBox1.Text.Trim() != "") { if(listBox1.FindString(textBox1.Text, -1) < 0) { listBox1.Items.Add(textBox1.Text); listBox1.Text = ""; listBox1.Focus(); } else { MessageBox.Show(textBox1.Text+" already exists in the list.", "Result"); } label1.Text = "จำนวนรายการ : " + listBox1.Items.Count.ToString(); listBox1.SelectedIndex = listBox1.Items.Count - 1; } } private void buttonEdit_Click(object sender, EventArgs e) { if(listBox1.SelectedIndex != -1) { listBox1.Items[indexSelecting] = textBox1.Text.Trim(); } label1.Text = "จำนวนรายการ : " + listBox1.Items.Count.ToString(); } private void buttonDelete_Click(object sender, EventArgs e) { if (listBox1.SelectedIndex != -1) { listBox1.Items.RemoveAt(indexSelecting); } label1.Text = "จำนวนรายการ : " + listBox1.Items.Count.ToString(); } } }
listBox1.FindString = Finds the first item in the ListBox that starts with the specified string.
listBox1.SelectedIndex = ตัวเลข index
listBox1.Items[เลข index] = สตริงข้อมูลของรายการ
listBox1.Items.Count = ตัวเลขจำนวนทั้งหมด
listBox1.Items.Add(สตริงข้อความ) = เพิ่ม
listBox1.Items.RemoveAt(เลข index) = ลบ
Sign up here with your email
ConversionConversion EmoticonEmoticon