如何从 ListBox 中获取特定项目以显示在 txt 框中 [英] How do I get a specific item from my ListBox to show in a txt box

查看:29
本文介绍了如何从 ListBox 中获取特定项目以显示在 txt 框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在制作一个程序,让用户选择一个 ID 号,用户从列表框中选择的 ID 号将在三个单独的 txt 框中显示名字、姓氏和爱好.你从ID、姓名和爱好中得到的数据都是从一个csv文件中读取的.我究竟如何选择一个 ID 来在 txt 框中显示我想要的数据.

I'm currently making a program that lets the user select an ID number and the one that the user picks from a list box will show the first name, last name, and hobby in three separate txt boxes. The data you get from the ID, names, and hobbies are all read from a csv file. How exactly do I select one ID to show the data I want in the txt boxes.

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();
        }
        List<ID> events = new List<ID>();
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string[] lines = System.IO.File.ReadAllLines("class.csv");
            {
                foreach(var line in lines)
                {
                    String[] words = line.Split(',');
                    string Id = words[0];
                    string last = words[1];
                    string first = words[2];
                    string hobby = words[3];
                    listBox1.Items.Add(last);
                    double.Parse(listBox1.SelectedItems) == 
                }
            }
        }
    }
}

第二个文件

using System;
using System.Collections.Generic;
using System.Text;

namespace StudentInformation
{
    class ID
    {
        string Id;
        string FirstName;
        string LastName;
        string Hobby;
        public ID(string eId,string eFirstName, string eLastName, string eHobby)
        {
            Id = eId;
            FirstName = eFirstName;
            LastName = eLastName;
            Hobby = eHobby;
        }
        public override string ToString() => $"{Id}";
    }
}

推荐答案

您可以在后端设置一个字典 - 您从下拉列表中选择的项目将是您的 Dictionary 的键.(您在解析 csv 时会设置)

You could set up a dictionary in the back end - the item you select from your dropdown would be the key for your Dictionary<string,ID> (which you would set up when you parse your csv)

然后在您的列表框索引中更改代码,您可以执行类似的操作

then inside your listbox index changed code you could do something like

if(string.IsNullOrWhiteSpace(listbox1.Text))
   {
           //clear textboxes here
    }
  else
  {
       var student = studentDictionary[listbox1.Text];
       //populate textboxes here
   }

尽管有一些轻微的通用编码技巧,但可以处理您的类和对象命名约定.永远不要将类命名为ID"——即使您的类代表一个 ID,也要更具体,因为 ID 在任何地方都被用作属性.根据您对这个项目的关心程度,您可能还需要在将 csv 加载到字典中时进行一些检查,以确保没有重复的学生、带有空白值的学生或正在加载空白 csv 行.您应该可能还将 ID 设为公共类,并将其字段设为公共.

Some slight generic coding tips though, work on your class and object naming conventions. Never name a class 'ID' - even if your class represents an ID, be more specific as ID gets used as a property everywhere. Depending on how much you care about this project, you might also want to do some checks when loading the csv into the dictionary to make sure you don't have duplicate students, students with blank values or blank csv lines being loaded in. You should probably also make ID a public class, and make it's fields public as well.

您可能还想考虑切换到带有 dropdownstyle dropDownList 的组合框 - 似乎它可能更适合您的情况

You may also want to consider switching to a combobox with dropdownstyle dropDownList - it seems like it might be more applicable to your situation

这篇关于如何从 ListBox 中获取特定项目以显示在 txt 框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆