Hi shakil,
Check this example. Now please take its reference and correct your code.
FormDesign
Code
C#
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
listBox1.DataSource = GetCarData();
listBox1.ValueMember = "Number";
listBox1.DisplayMember = "Number";
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
CarDetail detail = (CarDetail)listBox1.SelectedItem;
txtNumber.Text = detail.Number;
txtName.Text = detail.Name;
txtYear.Text = detail.Year;
}
private static List<CarDetail> GetCarData()
{
List<CarDetail> car = new List<CarDetail>();
CarDetail details = new CarDetail();
details.Number = "Car001";
details.Name = "Maruti Suzuki";
details.Year = "2001";
car.Add(details);
details = new CarDetail();
details.Number = "Car002";
details.Name = "Hyundai";
details.Year = "2002";
car.Add(details);
details = new CarDetail();
details.Number = "Car003";
details.Name = "Tata";
details.Year = "2003";
car.Add(details);
return car;
}
public class CarDetail
{
public string Number { get; set; }
public string Name { get; set; }
public string Year { get; set; }
}
}
VB.Net
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ListBox1.DataSource = GetCarData()
ListBox1.ValueMember = "Number"
ListBox1.DisplayMember = "Number"
End Sub
Private Sub listBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ListBox1.SelectedIndexChanged
Dim detail As CarDetail = CType(ListBox1.SelectedItem, CarDetail)
txtNumber.Text = detail.Number
txtName.Text = detail.Name
txtYear.Text = detail.Year
End Sub
Private Shared Function GetCarData() As List(Of CarDetail)
Dim car As List(Of CarDetail) = New List(Of CarDetail)()
Dim details As CarDetail = New CarDetail()
details.Number = "Car001"
details.Name = "Maruti Suzuki"
details.Year = "2001"
car.Add(details)
details = New CarDetail()
details.Number = "Car002"
details.Name = "Hyundai"
details.Year = "2002"
car.Add(details)
details = New CarDetail()
details.Number = "Car003"
details.Name = "Tata"
details.Year = "2003"
car.Add(details)
Return car
End Function
Public Class CarDetail
Public Property Number As String
Public Property Name As String
Public Property Year As String
End Class
End Class
Screenshot