简体   繁体   中英

how to show Selected index from listbox to text box

I am creating a program that allows the user to click on an ID in the list box and it will show a picture, first name, last name, and hobby from a csv data file. I am having problems when trying to get the names to show and the error I keep getting is you cant convert a char to string. on line 32 where it try to input the data to txtLast.text is where the error is. It says cant implicitly convert char to string and that it returns a zero base index.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace StudentInformation
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            
        }
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string[] lines = System.IO.File.ReadAllLines("class.csv");
            foreach (var line in lines)
            {
                String[] words = line.Split(',');
                string StudentId = words[0];
                string last = words[1];
                string first = words[2];
                string hobby = words[3];
                listBox1.Items.Add(StudentId);
                txtLast.Text = last [listBox1.SelectedIndex];
                //txtLast.Text = last[listBox1.SelectedIndex];
            }
            Image[] images = { Properties.Resources.Image1, Properties.Resources.Image1, Properties.Resources.Image1,
                Properties.Resources.Image1,Properties.Resources._700684235,Properties.Resources.Image1,Properties.Resources.Image1,
                Properties.Resources._700640699,Properties.Resources._700690019,Properties.Resources._700653005,Properties.Resources._700688218,
                Properties.Resources._700696692,Properties.Resources.Image1,Properties.Resources.Image1,Properties.Resources._700661732,
                Properties.Resources.Image1,Properties.Resources._700645894,Properties.Resources._700658141,Properties.Resources._700644980,
                Properties.Resources._700683782,Properties.Resources._700672657,Properties.Resources._700690042,Properties.Resources._700684588,};

        }

        private void btnSave_Click(object sender, EventArgs e)
        {

        }
    }
}

我不确定你到底想做什么,但如果你在 char 之后添加 .ToString() 它将把 char 转换回字符串,防止错误(除非你的 listBox1.SelectedIndex 大于长度学生的姓氏)。

last[listBox1.SelectedIndex].ToString();

txtLast.Text = last; will work

It Should work fine because last is already string.

You are actually getting char from string by passing list box index. Thus receiving only one character.

Seems you are trying to bind data to ListBox on item selection changed method Hope below instructions will helps.

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            PopulateListBoxData();
        }
        
        private void PopulateListBoxData()
        {
            //1. Select data from CSV File
            string[] lines = System.IO.File.ReadAllLines("class.csv");
            //2. Convert it to datatable
            //3. Bind Data To Listbox
            listBox1.DataSource = oTable;
            listBox1.ValueMember = "ID";
            listBox1.DisplayMember = "LastName"; //What ever name(First+Last)
        }
        
        
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //4. When Item list clicked this method will fire
            string value = listBox1.SelectedValue.ToString().Trim();
            string seletedtext = listBox1.SelectedItem.ToString().Trim();


            if (string.IsNullOrEmpty(value))
            { 
                //Save to DB 0r Display Item
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {

        }
    }


*CSV Format(class.csv)
 
 ID,LastName,FirstName,Hobby 
 1,Tom,William,Game 
 2,Adams,Smile,Tennis
 3,Kei,Hetath,Circket*

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM