CODING LISTBOX
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 LatihanListBox
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender,EventArgs e)
{
listBox1.BackColor = Color.Black;
listBox1.ForeColor = Color.Red;
listBox1.Font = new Font("Arial", 12);
listBox1.Items.Add("Carl");
listBox1.Items.Add("Dylan");
listBox1.Items.Add("Alfreud");
listBox1.Items.Add("Zayn Malik");
listBox1.Items.Add("Steffi");
listBox1.Items.Add("Cynthia");
}
private void cmdAmbilData_Click(object sender,EventArgs e)
{
string nama = "";
foreach (object item in listBox1.Items)
{
nama = nama + (item.ToString()) + "\n";
}
MessageBox.Show(nama);
}
private void cmdAmbilDataItem1_Click(objectsender, EventArgs e)
{
MessageBox.Show(listBox1.Items[listBox1.SelectedIndex].ToString());
}
private void cmdClear_Click(object sender,EventArgs e)
{
listBox1.ClearSelected();
}
private void cmdCari_Click(object sender,EventArgs e)
{
listBox1.ClearSelected();
int index = listBox1.FindString(txtCari.Text);
if (index < 0)
{
MessageBox.Show("Data Tidak Ditemukan !");
txtCari.Clear();
txtCari.Focus();
}
else
{
listBox1.SelectedIndex = index;
}
}
private voidlistBox1_SelectedIndexChanged(object sender,EventArgs e)
{
if ((listBox1.SelectedItem) != null)
txtEdit.Text = listBox1.SelectedItem.ToString();
else
txtEdit.Clear();
}
private void cmdTambah_Click(object sender,EventArgs e)
{
if (txtEdit.Text != "")
{
listBox1.Items.Add(txtEdit.Text);
}
}
private void cmdUbah_Click(object sender,EventArgs e)
{
listBox1.Items[listBox1.SelectedIndex] = txtEdit.Text;
}
private void cmdHapus_Click(object sender,EventArgs e)
{
listBox1.Items.RemoveAt(listBox1.SelectedIndex);
}
private void cmdAmbilDataItems_Click(objectsender, EventArgs e)
{
string nama = "";
foreach (object item in listBox1.SelectedItems)
{
nama = nama + item.ToString() + "\n";
}
MessageBox.Show(nama);
}
private void cmdExit_Click(object sender,EventArgs e)
{
Application.Exit();
}
}
}


Tidak ada komentar:
Posting Komentar