0

I am looking for the solution to save and reload what I enter into my text boxes or what I filled in my ComboBoxes. Here is the code I current have, but I am not 100% sure it is all correct. I am only posting the code I need help with. If you need the entire batch of code I can post it, but I did not think it was relevant to what I am asking for. This is what I have done so far, which I am not sure it will actually save the info that is typed in, and I am completely unsure as to what to put in the loadLastBtn method. I Just want it to be saved to memory even when the textbox texts have changed and then I want to be able to retrieve that from memory when I hit the 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;
        }
    }

Here is the user object I created. I am just unsure how to call it now when I want to save and reload.

    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;
        }
    }
E_T_A
  • 39
  • 1
  • 1
  • 10
  • What do you mean by "save the info". Do you want to store it on the file system. Or just keep it in memory even after the textbox texts have changed? – Chief Wiggum Nov 17 '15 at 21:03
  • Save to where? To the disk or memory? Whatever the target, the string array's scope is the click event and it will not be available after this method completes it's execution. If you can tell to what storage you want to save the data, we can help. ( for the disk case, you have the application data option to save the data under the appdata folder ) – Oguz Ozgul Nov 17 '15 at 21:04
  • That'll assign the values to your array yes, but how are you defining "save" and "load"? Do you want it written to / read from a text file, or a database? Or simply Just passing data between controls via the Submit and Load buttons? If you want the last one, simply define `string[] s1` outside of the scope of your `submitBtn_Click` event so that it can also be accessed by `loadLastBtn_Click`, then just do `firstNameTxt.Text = s1[0];` etc – sab669 Nov 17 '15 at 21:05
  • I am sorry for not being clearer. I just want to save it to memory even after the textbox texts have changed. I am unsure how to do that and then be able to retrieve that data. @JamesBlond – E_T_A Nov 17 '15 at 21:06
  • that works perfectly. Now what if I submit more than one, how can I continually load the last. ex. if I enter one users info, submit, then clean, then submit another users info, submit then clear. then I want to reload all the way through to the first user I entered? @sab669 – E_T_A Nov 17 '15 at 21:14
  • 1
    You could use a `List` to create a `List` of `string[]`. A `List` is very similar to an array, except they are of a dynamic length. You'd just call `myList.Add()` and pass it a new `string[]`. Then you can iterate over that collection of arrays in Load to get all of your data. But at this point it sounds like you should consider breaking this information off into a class (`student` I'm guessing?) and maintain a `List` or whatever your class would be called. – sab669 Nov 17 '15 at 21:19
  • Just saw your edit with your `Members` class, so basically instead of using a array simply declare your `List lstMembers = new List();` outside of the save button event, then do something like `Member mbr = new Member(); ... mbr.FirstName = firstNameTxt.Text; ... lstMembers.Add(mbr);` inside the event. Then in your load you just access them each by doing `for (int i = 0; i < lstMembers.Count; i++) firstNameTxt.Text = listMembers[i].FirstName; ...`. – sab669 Nov 17 '15 at 21:32
  • under the submitBtn I added the list array. Can you let me know if that looks right for the saving feature or if I need to edit it? @sab669 – E_T_A Nov 17 '15 at 21:34
  • Noticing that one value is a password, you may want to consider using a [SecureString](https://msdn.microsoft.com/en-us/library/system.security.securestring(v=vs.110).aspx) – u8it Nov 17 '15 at 21:36
  • I just added the iteration in my load last and it is still only loading the last entry and not going all the way through? @sab669 – E_T_A Nov 17 '15 at 21:48
  • please let me know if you can help. this is the last part of my code and then I am complete. thank you. @sab669 – E_T_A Nov 17 '15 at 22:13

2 Answers2

0

Well, most common way to do it without any hard implementation would be writing all of your variables with ';' between them within a file. Then read it from file and re-assign it into your form elements when user press down a button.Check this guide to how to write into file:

https://msdn.microsoft.com/en-us/library/8bh11f1k.aspx

I'll give an simple example:

For example my form consist of username and password TextFields. After user entered them and pressed login, I save them into file for next time user logs in, this method will save it.

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];
}
Orkun Kocyigit
  • 1,137
  • 10
  • 21
0

I would suggest creating a 'POCO' (Plain old c# object) or struct that will hold your data. When you want to save it, just serialize it to disk (either using MS Serialization (more here, or by using JSON serialize method (more here).
This way, you don't worry about what the user might have entered (less worries about sanitizing the data and making sure he didn't use your escape character).

Noctis
  • 11,507
  • 3
  • 43
  • 82