-2

I am trying to save the username entered in the "enter playername" field in order to create a highscore list, can anyone help me with that ?

namespace TikTakTo
{
    public partial class Anmeldung : Form
    {
        public Anmeldung()
        {
            InitializeComponent();
        }

        private void button_play_Click(object sender, EventArgs e)
        {
            Form1.setSpielerName(Spieler_1.Text, Spieler_2.Text);

            Form1 frm = new Form1();
            frm.Show();

            this.Hide();
        }

        private void Spieler_2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar.ToString() == "\r")
                button_play.PerformClick();
        }
    }
}
east1000
  • 1,240
  • 1
  • 10
  • 30
Medicute
  • 1
  • 2

3 Answers3

0

Basically you have two options, you can either save the info to file, or save it to database. This should help you: Best way to store data locally in .NET (C#)

Always try to make your own search before asking a question!

Klemikaze
  • 369
  • 6
  • 15
0

you can write in txt file in your project folder , and after restarting the app, you can control the file in that location , if its exist you can read data and put the form. like a cookie. example code.

public static string _debugPath { get { return Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location); } }
        
public static void WriteToText(string txt)
{
  DateTime _dt = DateTime.Now;
  using (StreamWriter wr = File.AppendText(Path.Combine(_debugPath, "Info.Inc")))
  {
    wr.WriteLine(txt+"|");
    wr.Close();
  }
}
public static bool ReadInfo()
{
    FileInfo fi = new FileInfo(Path.Combine(_debugPath , "Info.Inc"));
    if (fi.Exists)
    {
          using (StreamReader rd = new StreamReader(Path.Combine(_debugPath , "Info.Inc")))
          {
                          
                  //take data in here and put the form.        
                           
          }
     }
}
  • 1
    In general, you shouldn't write to files in the application folder. If this is later *installed* properly, then your program doesn't even have write access to that folder at all. – Lasse V. Karlsen Sep 06 '21 at 12:26
0

Most recommanded option for windows-forms applications is a settings file located in the Properties folder. The data there can be saved per-user and per-application, and stored in the application's AppData folder.
You can read the settings-table fields, change their values and save your changes.

e.g:
Properties.Settings.Default["SomeProperty"] = "Some Value";
Properties.Settings.Default.Save();

Read this question for more info.

R.E.L
  • 181
  • 3