Home » » C# ListView selected index to text box and picture box

C# ListView selected index to text box and picture box

Hello all, now i can share to you about C# ListView Selected Index Event, and I using Microsoft visual Studio 2010, ok let’s start
First you can make new project, see the picture



You can make desain like this picture,  textbox ID = txtID, textbox Name = txtName, Picture box = pictureBox1, ListView = listView1





And you can make new void

string img;  //for image on listView Selected Index

void listViewItem()
        {
            //List View desain

            listView1.Items.Clear();
            listView1.Columns.Add("ID", 25, HorizontalAlignment.Right);
            listView1.Columns.Add("Name", 100, HorizontalAlignment.Center);
            listView1.Columns.Add("Picture Sources", 150, HorizontalAlignment.Left);

            listView1.GridLines = true;
            listView1.BackColor = Color.SkyBlue;
            listView1.FullRowSelect = true;
            listView1.Activation = ItemActivation.Standard;
            listView1.View = View.Details;

            //add item to listView1

            ListViewItem item = new ListViewItem("1");
            item.SubItems.Add("Nike Elastico");
            item.SubItems.Add(@"D:\Sepatu-Futsal-Nike-Elastico-Teal-Green.jpg");

            listView1.Items.Add(item);

            ListViewItem item1 = new ListViewItem("2");
            item1.SubItems.Add("Error Windows");
            item1.SubItems.Add(@"D:\Untitled.png");

            listView1.Items.Add(item1);
        }


And this code on Form1 Load

private void Form1_Load(object sender, EventArgs e)
        {
            // load void listViewItem
            listViewItem();
        }


And now this code on listView1 Selected Index Change Event

private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (listView1.SelectedItems.Count > 0)
                {
                    txtID.Text = listView1.SelectedItems[0].SubItems[0].Text;
                    txtName.Text = listView1.SelectedItems[0].SubItems[1].Text;
                    img = listView1.SelectedItems[0].SubItems[2].Text;  //Selected image
                    pictureBox1.Image = Image.FromFile(img);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Warning" + ex);
            }
        }  

Execute (f5)























And finish, good luck brother…..



1 comments:

Search This Blog

Like This Yoo

 
Modify by : Antonio Francisco
Copyright © 2013. Relax - All Rights Reserved
Template Created by Creating Website Published by Mas Template
Proudly powered by Blogger