简体   繁体   中英

C# Saving TextBox input

I'm making a Windows Form using C#, and I'm quite new to it all.. Now, this is what I want to achieve:

The textboxes have a default text set, but when the user modifies this, I'd like the modified text to load next time the form is opened. (Only if checkbox is checked)

My question is: Is this possible? And if so, how? Like I said I'm really new to C#, so even after searching alot I couldn't really find how to do it. Here's my code:

using System;
using Sulakore;
using System.Linq;
using Sulakore.Habbo;
using System.Drawing;
using Kendax.Properties;
using Sulakore.Protocol;
using System.Windows.Forms;
using System.Threading.Tasks;
using Sulakore.Communication;
using Sulakore.Protocol.Encryption;

namespace Kendax
{
    public partial class UpdateHeaders : Form
    {
        public UpdateHeaders()
        {
            InitializeComponent();
        }
        private void UpdateHeadersBtn_Click(object sender, EventArgs e)
        {
            HHeaders.Motto = ushort.Parse(UpdateMotto.Text);
            HHeaders.Say = ushort.Parse(UpdateSay.Text);
            HHeaders.Walk = ushort.Parse(UpdateWalk.Text);
            HHeaders.Pong = ushort.Parse(UpdatePong.Text);
            HHeaders.Dance = ushort.Parse(UpdateDance.Text);
            HHeaders.Sign = ushort.Parse(UpdateSign.Text);
            HHeaders.Shout = ushort.Parse(UpdateShout.Text);
            HHeaders.Rotate = ushort.Parse(UpdateRotate.Text);
            HHeaders.Stance = ushort.Parse(UpdateStance.Text);
            HHeaders.Like = ushort.Parse(UpdateLike.Text);
            HHeaders.Trade = ushort.Parse(UpdateTrade.Text);
            HHeaders.Respect = ushort.Parse(UpdateRespect.Text);
            HHeaders.AddFriend = ushort.Parse(UpdateAddFriend.Text);
            HHeaders.Clothes = ushort.Parse(UpdateClothes.Text);
            HHeaders.Gesture = ushort.Parse(UpdateGesture.Text);
            HHeaders.Navigate = ushort.Parse(UpdateMotto.Text);
            HHeaders.Exit = ushort.Parse(UpdateExit.Text);
            HHeaders.JoinGroup = ushort.Parse(UpdateJoinGroup.Text); ;
            HHeaders.LeaveGroup = ushort.Parse(UpdateLeaveGroup.Text);
            HHeaders.HelpRequest = ushort.Parse(UpdateHelpRequest.Text);
            HHeaders.Scratch = ushort.Parse(UpdateScratch.Text);
            HHeaders.RideHorse = ushort.Parse(UpdateRideHorse.Text);
            HHeaders.MakeRoom = ushort.Parse(UpdateMakeRoom.Text);
            HHeaders.Facing = ushort.Parse(UpdateFacing.Text);
            HHeaders.Whisper = ushort.Parse(UpdateWhisper.Text);
            HHeaders.ChangeName = ushort.Parse(UpdateChangeName.Text);
        }
        private void ResetDefaults_Click(object sender, EventArgs e)
        {
            HHeaders.Motto = 3117;
            HHeaders.Say = 2632;
            HHeaders.Walk = 2383;
            HHeaders.Pong = 516;
            HHeaders.Dance = 250;
            HHeaders.Sign = 3133;
            HHeaders.Shout = 269;
            HHeaders.Rotate = 3390;
            HHeaders.Stance = 2049;
            HHeaders.Like = 2947;
            HHeaders.Trade = 1384;
            HHeaders.Respect = 1724;
            HHeaders.AddFriend = 20;
            HHeaders.Clothes = 3713;
            HHeaders.Gesture = 3976;
            HHeaders.Navigate = 2219;
            HHeaders.Exit = 2520;
            HHeaders.JoinGroup = 3134;
            HHeaders.LeaveGroup = 3816;
            HHeaders.HelpRequest = 3540;
            HHeaders.Scratch = 1292;
            HHeaders.RideHorse = 3621;
            HHeaders.MakeRoom = 3156;
            HHeaders.Facing = 1462;
            HHeaders.Whisper = 2382;
            HHeaders.ChangeName = 809;
// These are the textboxes and their default values
            UpdateMotto.Text = 3117.ToString();
            UpdateSay.Text = 2632.ToString();
            UpdateWalk.Text = 2383.ToString();
            UpdatePong.Text = 516.ToString();
            UpdateDance.Text = 250.ToString();
            UpdateSign.Text = 3133.ToString();
            UpdateShout.Text = 269.ToString();
            UpdateRotate.Text = 3390.ToString();
            UpdateStance.Text = 2049.ToString();
            UpdateLike.Text = 2947.ToString();
            UpdateTrade.Text = 1384.ToString();
            UpdateRespect.Text = 1724.ToString();
            UpdateAddFriend.Text = 20.ToString();
            UpdateClothes.Text = 3713.ToString();
            UpdateGesture.Text = 3976.ToString();
            UpdateNavigate.Text = 2219.ToString();
            UpdateExit.Text = 2520.ToString();
            UpdateJoinGroup.Text = 3134.ToString();
            UpdateLeaveGroup.Text = 3816.ToString();
            UpdateHelpRequest.Text = 3540.ToString();
            UpdateScratch.Text = 1292.ToString();
            UpdateRideHorse.Text = 3621.ToString();
            UpdateMakeRoom.Text = 3156.ToString();
            UpdateFacing.Text = 1462.ToString();
            UpdateWhisper.Text = 2382.ToString();
            UpdateChangeName.Text = 809.ToString();
// End of textboxes and their default values

        }
    }
}

The user is supposed to remove the default text, and needs to input new text. That input should be saved when the form gets closed, and loaded when the form is opened again IF the checkbox is checked. ELSE , no input should be saved and the default text should be loaded when form is opened.

Can anyone please help me with this? I know it might be a little vague but once again, I'm new to this and I'm also not English (I'm Dutch). Thanks A LOT in advance, I really hope someone understands what I'm trying to achieve.. If not, I'm sorry!

Again, thanks in advance!

UPDATE:

Okay, so I've tried saving the input for 1 textbox, and this is what I did:

http://i.gyazo.com/950c85a0d80ce942d103dc75668094d1.png (I can't upload images yet because of too low reputation)

That's what I made in settings, then this is where it loads the default value:

public UpdateHeaders()
{
    InitializeComponent();
    UpdateMotto.Text = Settings.Default.UpdateMotto.ToString();
}

And this is where it's supposed to save the user input:

private void UpdateHeaders_FormClosed(object sender, FormClosedEventArgs e)
{
    Settings.Default.UpdateMotto = ushort.Parse(UpdateMotto.Text);
    if (RememberMe.Checked)
    {
        Settings.Default.Save();
        Settings.Default.Reload();
    }
    else
    {
        this.Close();
    }
}

It didn't save the input for that textbox, but loaded the default input instead. The .config file has been made but isn't updated and still holds the default value. What am I doing wrong?

Again thanks alot in advance :)

If you are using Visual Studio, you can go to the project properties window, click the Settings tab, and add "User" settings there. In code, you use Properties.Settings.Default to access the individual settings you add.

From the title to your question, it seems like you've already gotten that far. So all you have left is, after you actually assign values to the properties in the Default settings object, call the Save() method. This will write out the settings as an XML file under the user's profile directory, and will be loaded automatically the next time that same user runs the program.

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