我正在寻找解决方案来保存和重新加载我输入到文本框中的内容或我填写的ComboBoxes。这是我目前的代码,但我不是100%确定它是正确的。我只发布了我需要帮助的代码。如果您需要整批代码我可以发布它,但我认为它与我所要求的无关。这是我到目前为止所做的,我不确定它实际上会保存输入的信息,我完全不确定要在loadLastBtn方法中放入什么。我只是希望它保存到内存中,即使文本框文本已经更改,然后我希望能够在我按下loatLastbtn时从内存中检索它。
private void submitBtn_Click(object sender, EventArgs e)
{
List<Members> lstMembers = new List<Members>();
if (string.IsNullOrEmpty(firstNameTxt.Text) || string.IsNullOrEmpty(lastNameTxt.Text)
|| string.IsNullOrEmpty(userNameTxt.Text) ||
string.IsNullOrEmpty(passwordTxt.Text) || string.IsNullOrEmpty(confPassTxt.Text)
|| string.IsNullOrEmpty(majorBox.Text) || string.IsNullOrEmpty(specialtyBox.Text))
{
MessageBox.Show("You must enter in all fields before moving forward");
}
else
{
//would this save my data to the array and how can I reload this?
Members m1 = new Members(firstNameTxt.Text, lastNameTxt.Text, userNameTxt.Text,
passwordTxt.Text, confPassTxt.Text, majorBox.Text,
specialtyBox.Text);
lstMembers.Add(m1);
}
}
private void loadLastBtn_Click(object sender, EventArgs e)
{
for(int i = 0; i < lstMembers.Count; i++)
{
firstNameTxt.Text = lstMembers[i].FirstName;
lastNameTxt.Text = lstMembers[i].LastName;
userNameTxt.Text = lstMembers[i].UserName;
passwordTxt.Text = lstMembers[i].Password;
confPassTxt.Text = lstMembers[i].ConfPassword;
majorBox.Text = lstMembers[i].Major;
specialtyBox.Text = lstMembers[i].Specialty;
}
}
这是我创建的用户对象。当我想保存并重新加载时,我只是不确定如何调用它。
class Members
{
private string firstName;
private string lastName;
private string userName;
private string password;
private string confPassword;
private string major;
private string specialty;
public Members(string firstName, string lastName, string userName, string password,
string confPassword, string major, string specialty)
{
this.firstName = firstName;
this.lastName = lastName;
this.userName = userName;
this.password = password;
this.confPassword = confPassword;
this.major = major;
this.specialty = specialty;
}
public string FirstName
{
get
{
return firstName;
}
set
{
FirstName = firstName;
}
}
public string LastName
{
get
{
return lastName;
}
set
{
LastName = lastName;
}
}
public string UserName
{
get
{
return userName;
}
set
{
UserName = userName;
}
}
public string Password
{
get
{
return password;
}
set
{
Password = password;
}
}
public string ConfPassword
{
get
{
return confPassword;
}
set
{
ConfPassword = confPassword;
}
}
public string Major
{
get
{
return major;
}
set
{
Major = major;
}
}
public string Specialty
{
get
{
return specialty;
}
set
{
Specialty = specialty;
}
}
答案 0 :(得分:0)
嗯,最常见的方法是在没有任何硬实现的情况下执行它,而是使用';'编写所有变量他们之间的文件。然后从文件中读取它并在用户按下按钮时将其重新分配到表单元素中。查看本指南以了解如何写入文件:
https://msdn.microsoft.com/en-us/library/8bh11f1k.aspx
我将举一个简单的例子:
例如,我的表单由用户名和密码TextFields组成。用户输入并按下登录后,我将其保存到文件中以供下次用户登录,此方法将保存。
public void save(){
String username = txtUsername.Text;
String password = txtPassword.Text;
File.WriteAllText(path, username + ";" + password);
}
public void load(){
String parameters = File.ReadAllText(path);
String[] splitted = parameters.split(';');
txtUsername.Text = splitted[0];
txtPassword.Text = splitted[1];
}
答案 1 :(得分:0)