Monday 7 January 2013

ListBox

Tampilan Pada Design :


Keterangan :

Coding Program:

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 WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            listBox1.BackColor = Color.RoyalBlue;
            listBox1.ForeColor = Color.SkyBlue;
            listBox1.Font = new Font("New Times Roman", 14);

            listBox1.Items.Add("Dionisius");
            listBox1.Items.Add("Ryan");
            listBox1.Items.Add("Share");
            listBox1.Items.Add("Ilmu");
            listBox1.Items.Add(".com");
        }

        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(object sender, EventArgs e)
        {
            MessageBox.Show(listBox1.Items[listBox1.SelectedIndex].ToString());
        }

        private void cmdClrSelected_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 void listBox1_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(object sender, EventArgs e)
        {
            string nama = "";
            foreach (object item in listBox1.SelectedItems)
            {
                nama = nama + item.ToString() + "\n";
            }
            MessageBox.Show(nama);
}
       

     }

 }




0 komentar:

Post a Comment

Note: only a member of this blog may post a comment.