如何使用列表或字典中的数据填充7个文本框? [英] How do I populate 7 textboxes with data from a list or dictionary ?
问题描述
我正在开发一个项目,当我加载表单时,有7个文本框,其中有相应的标签。当表单加载时,我需要使用列表或字典中的第一个数据字符串填充到文本框中。然后我有4个按钮,以循环列表/词典中的其他数据串。
我尝试过:
我对编程很新,还没有真正尝试过。我一直在搜索示例或教程,但还没找到我到底的确切内容。
I am working on a project where when I load a form there is 7 text boxs with there corresponding labels. When the form loads I need to populate with the 1st string of data from a list or dictionary into the text boxes. Then I have 4 buttons in order to cycle through the other strings of data in the list/dictionary.
What I have tried:
I am very new to programming and have not really tried much yet. I have been searching for examples or tutorials but have not found exactly what I am after yet.
推荐答案
您好会员13029812
,
这个问题的简单解决方案是:
1)遍历所有控件形式。
2)看看它是否是一个文本框。如果是,则将文本更改为第一个列表项。
HiMember 13029812
,
The simple solution to the problem is:
1) Loop through all the controls of the form.
2) See if it is a textbox. If it is, then change the text to the first list item..
List<string> MyList = new List<string>();
MyList.Add("text 1");
MyList.Add("text 2");
foreach (Control Ctl in Controls)
if (Ctl is TextBox)
(Ctl as TextBox).Text = MyList[0];
请谷歌关于IS和AS如何运作。
Please Google about how IS and AS works.
我不会这样做,但这会指出你正确的方向:
I wasn't going to but this will point you in the right direction:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace WFDataList
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
employees = GetEmployeesList();
SetButtonState();
SetFields();
}
private int index = 0;
private List<Employee> employees { get; set; }
private List<Employee> GetEmployeesList()
{
return new List<Employee>
{
new Employee("Duane", "Wales", "Taber", "ewales@shaw.ca", "869-9681", "
55,000, 管理),
new 员工( Lori, Wales, Taber, llwales@shaw.ca, 869-9682,
55,000", "Administration"), new Employee("Lori", "Wales", "Taber", "llwales@shaw.ca", "869-9682", "
这篇关于如何使用列表或字典中的数据填充7个文本框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!