簡體   English   中英

如何顯示從列表框到文本框的選定索引

[英]how to show Selected index from listbox to text box

我正在創建一個程序,允許用戶單擊列表框中的 ID,它將顯示 csv 數據文件中的圖片、名字、姓氏和愛好。 我在嘗試顯示名稱時遇到問題,我不斷收到的錯誤是您無法將字符轉換為字符串。 在第 32 行,它嘗試將數據輸入到 txtLast.text 是錯誤所在。 它說不能將 char 隱式轉換為字符串,並且它返回一個零基索引。

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 已經是字符串。

您實際上是通過傳遞列表框索引從字符串中獲取字符。 因此只接收一個字符。

似乎您正在嘗試在項目選擇更改方法上將數據綁定到 ListBox 希望下面的說明會有所幫助。

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*

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM