1

I have this following code and don't know how to go further. I simply want to update the default text of the textBox in the Form_Load() method, when I'm writing a new text on it, and clicking on the "Save" button.

Thanks in advance

private void Form7_Load(object sender, EventArgs e)
    {
        textBox1.ForeColor = System.Drawing.Color.Gray;   
        textBox1.Text =                 
    }

    private void textBox1_Click(object sender, EventArgs e)
    {
        textBox1.Clear();
        textBox1.ForeColor = textBox1.ForeColor = SystemColors.WindowText;
    }

    private void buttonSave_Click(object sender, EventArgs e)
    {
        string def = ret();
    }

    private string ret()
    {
        string text = textBox1.Text;
        return text;
    }
user3812509
  • 131
  • 1
  • 3
  • 12
  • I'm not sure I understand what your problem is... just set `textBox1.Text = "some text"` in the `Form_Load` event? – JW Lim Jul 07 '14 at 13:35
  • what is happening currently any error, exception, anything?? – Neel Jul 07 '14 at 13:37
  • Do you want to save the changed text and then load this value next time the application starts? – JohnSaps Jul 07 '14 at 13:43
  • The point is that I want to write a text in the textbox and click on the "Save" button, then the textbox should update its default value to the written text, so the newly written text is shown in the textbox, every time the Form is loaded or opened. – user3812509 Jul 07 '14 at 13:44
  • JohnSaps. Yes, this is exactly what I want. – user3812509 Jul 07 '14 at 13:46
  • Even though this answer was about VB I believe it is still applicable in your case. http://stackoverflow.com/questions/10712407/simple-way-to-save-and-load-data-visual-basic/10712544#10712544 – Mark Hall Jul 07 '14 at 13:47

2 Answers2

2

To save/restore values between application restarts, it's best to use the .NET settings mechanism. Go to the project's properties and click the tab "Settings". Add some setting, like "TextBoxDefaultValue", make sure it is of type String and marked as User setting.

In your code, you can access the setting like this

textBox1.Text = Properties.Settings.Default.TextBoxDefaultValue;

To change and persist the setting, use this:

Properties.Settings.Default.TextBoxDefaultValue := textBox1.Text;
Properties.Settings.Default.Save();

This setting is now persisted on a per-user basis.

So in your code that would read:

private void Form7_Load(object sender, EventArgs e)
{
    textBox1.ForeColor = System.Drawing.Color.Gray;   
    textBox1.Text = Properties.Settings.Default.TextBoxDefaultValue;
}

private void buttonSave_Click(object sender, EventArgs e)
{
    Properties.Settings.Default.TextBoxDefaultValue = ret();
    Properties.Settings.Default.Save();
}

By the way: you should think about what would happen if the user doesn't enter the text box by clicking it, but by tabbing into it. Maybe you should look into the OnEnter and OnLeave events...

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
0

There is no default value of TextBox.Text, or If you wish, it's blank when control is created. To have default value in there you need to implement it (well, by setting TextBox.Text) and chosing Form.Load is not a bad choice.

For default value to persist between application starts you have to save this value somewhere somehow: into a file, database, registry, on a server, etc. See this question.

I'd myself use simple XmlSerializer:

public class MyData
{
    public string Text; // to save textbox text, could be public field or property
}

// to save
var data = new MyDate() { Text = textBox.Text };
using (var stream = new FileStream("somefile", FileMode.OpenOrCreate))
{
    var serializer = new XmlSerializer(data.GetType()); // or typeof(MyData)
    serializer.Serialize(fileStream, data);
}

// to load
using (var stream = new FileStream("somefile", FileMode.Open, FileAccess.Read))
{
    var serializer = new XmlSerializer(typeof(MyData));
    var data = serializer.Deserialize(stream) as MyData;
    textBox.Text = data.Text;
}
Community
  • 1
  • 1
Sinatr
  • 20,892
  • 15
  • 90
  • 319