我想点击列表框中的项目,并将传递到该列表框的属性显示为多行文本框。
以下是我在表单初始化时编写的代码
public Form1()
{
InitializeComponent();
ReadFromFile.Read("sample.GED");
foreach (KeyValuePair<int, Individual> kvp in ReadFromFile.individuals)
{
listBox2.Items.Add("ID = " + kvp.Value.id + " Name = " + kvp.Value.name.givenName + " " + kvp.Value.name.surname + " DoB = " + kvp.Value.birth.date);
}
int testIndividual = 94;
string genderOut = "";
if (ReadFromFile.individuals[testIndividual].gender == "M")
{
genderOut = "MALE";
}
else if (ReadFromFile.individuals[testIndividual].gender == "F")
{
genderOut = "FEMALE";
}
try
{
textBox1.AppendText(
"Name = " + ReadFromFile.individuals[testIndividual].name.givenName + " "
+ ReadFromFile.individuals[testIndividual].name.surname
+ Environment.NewLine + "Gender = " + genderOut
+ Environment.NewLine + "Birth date = " + ReadFromFile.individuals[testIndividual].birth.date
+ Environment.NewLine + "Birth place = " + ReadFromFile.individuals[testIndividual].birth.place
+ Environment.NewLine + "Death date = " + ReadFromFile.individuals[testIndividual].death.date
+ Environment.NewLine + "Death place = " + ReadFromFile.individuals[testIndividual].death.place);
}
catch
{
MessageBox.Show("This individual doesnt exist");
}
}
}
我想添加更多内容,以便点击列表框项目,该项目的详细信息将显示在文本框中
我觉得我可能必须覆盖ToString()方法或正则表达式。我还是一个新手程序员,所以对我很轻松:)谢谢你
答案 0 :(得分:0)
您需要处理列表框的SelectedIndexChanged
事件。
执行此操作的一种方法是调出Form1.cs [Design]并选择列表框。在属性网格(Alt + Enter)中,单击如下所示的图标:
找到事件SelectedIndexChanged
并双击它。这将在自动生成的Form1.cs.designer文件中为您连接一个事件处理程序。
接下来,使用以下代码替换Form1类的代码:
public partial class Form1 : Form
{
private Dictionary<int, Individual> _individuals;
public Form1()
{
InitializeComponent();
ReadFromFile.Read("sample.GED");
_individuals = ReadFromFile.individuals;
listBox1.DataSource = _individuals.Select(individual => individual.Value).ToList();
listBox1.DisplayMember = "name";
listBox1.ValueMember = "id";
}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Clear();
var individual = listBox1.SelectedItem as Individual;
string genderOut = (individual.Gender == "M") ? "MALE" : "FEMALE";
var displayText
= String.Format("Name = {0} {1}\r\n" +
"Gender = {2}\r\n" +
"Birth date = {3}\r\n" +
"Birth place = {4}\r\n" +
"Death date = {5}\r\n" +
"Death place = {6}"
, individual.name.givenName
, individual.name.surname
, genderOut
, individual.birth.date
, individual.birth.place
, individual.death.date
, individual.death.place);
textBox1.AppendText(displayText);
}
}
关于我已经改变的一些事情的一些注释。
SelectedIndexChanged
事件处理程序Environment.NewLine
重复都是混乱的。)DataSource
属性而不是foreach循环设置列表框的数据。此外,您会注意到的一件事是列表框中的列表项不会显示正确的文本。这是因为您似乎正在使用一些自定义类或结构来表示个人的姓名,出生和死亡?要解决此问题,您需要向Individual
类添加一个新属性,如下所示:
public class Individual
{
// ... your code
public string DisplayName
{
get { return String.Format("{0} {1}), name.givenName, name.surname; }
}
// ... the rest of your code
}
然后你需要更改上面代码中的行,如下所示:
listBox1.DisplayMember = "name";
到此:
listBox1.DisplayMember = "DisplayName";
最后注意事项:您应该使用“Upper Camel Case”作为您的房产名称。这意味着它们以大写字母开头,然后每个单词的第一个字母也是大写字母。例如,name.givenName
应为Name.GivenName
。这是一种广泛使用的惯例。