简体   繁体   中英

C# textbox to Array

I'm sure this is really simple to do but i'm struggling.

private void button1_Click(object sender, EventArgs e)
    {
        SaveBtn();


        void SaveBtn()
        {

            string savetext = textBox1.Text;
            string savetext2 = textBox2.Text;


            File.AppendAllText(@"C:\Riot Games\AccountSwitcher.txt", savetext + Environment.NewLine + savetext2 + Environment.NewLine + Environment.NewLine);
            MessageBox.Show("Your ID: " + savetext + " and you PWD: " + savetext2 + " has been saved.");


        }

    }

As you can see i have 2 textbox and when i'm clicking the button "save" both input are saved into a file.txt. This code works like a charm but i'd rather save these 2 inputs into an array so i could use them individually.

Thanks your help, i'm pretty noob as you can see so please keep it simple :D <3

采用:

string[] savetexts = new string[]{ savetext , savetext2 };

Alternatively, you could convert the entire string and save it in a char array.

char[] savetext = savetext.ToCharArray();
char[] savetext2 = savetext2.ToCharArray();

Hope this helps.!

PS It is much easier to use a List instead of hard-coded arrays like above.

List<String> myStrings = new List<String>();
myStrings.add(savetext);
myStrings.add(saveText2);

.....etc

then to get them back you iterate over myStrings

foreach(String s in myStrings){
  Console.writeline(s);
}

Or you can access them directly

String text1 = myStrings[0];
String text2 = myString[1];

This is a bit more than what you are asking, but using List becomes much easier in the long run. Best of luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM