retain previous value of text box till the form is closed

ravi kumar 331 Reputation points
2021-01-22T04:50:22.593+00:00

Hello all ,

I am very new to c# , and learning to make data entry application,
in my entry form when the user clicks save all the data text boxes are refreshed and saved to database and the text box appears empty to enter gain. This is a continuous process.
Now i want my textbox1 to retain the same value where the user first entered till the form is closed.
Please help me how to achieve this?
i tried this code but no use:

private string value;  
        private void materiaNumberTextBox_TextChanged(object sender, EventArgs e)  
        {  
  
            var oldValue = value;  
            value = ((TextBox)sender).Text; // text1.Text  
  
     
        }  

here's the code that does while saving:

private void btnsave_Click(object sender, EventArgs e)  
        {  
            try  
            {  
                String msg = "Confirm Save?";  
                String caption = "Save Record";  
                MessageBoxButtons buttons = MessageBoxButtons.YesNo;  
                MessageBoxIcon ico = MessageBoxIcon.Question;  
                DialogResult result;  
                result = MessageBox.Show(this, msg, caption, buttons, ico);  
                if (result == DialogResult.Yes)  
                {  
                    generateautoID();  
                    this.iP_SpoolsBindingSource.EndEdit();  
                    MessageBox.Show("The Record saved Successfully:" + outputSpoolNoTextBox.Text, "Save_Update",  
                        MessageBoxButtons.OK, MessageBoxIcon.Information);  
                    this.iP_SpoolsTableAdapter.Update(this.pINQCDataSet.IP_Spools);  
                    this.iP_SpoolsTableAdapter.Fill(this.pINQCDataSet.IP_Spools);  
                    //MessageBox.Show("The Record saved Successfully:", "Save_Update",  
                    //MessageBoxButtons.OK, MessageBoxIcon.Information);  
                    this.iP_SpoolsBindingSource.AddNew();  
                    string strStartenddateformat = "dd-MM-yyyy";  
                    materialTypeComboBox.ValueMember = "Tungsten";  
                    unitComboBox.ValueMember = "Mic";  
                    statusComboBox.ValueMember = "Accepted";  
                    cFComboBox.ValueMember = "";  
                    bowOutOfComboBox.ValueMember = "";  
                    ductilityOutofComboBox.ValueMember = "";  
                    finishUOMComboBox.ValueMember = "Mic";  
                    finishTypeComboBox.ValueMember = "Clean";  
                    rejectReason1ComboBox.ValueMember = "";  
                    rejectReason2ComboBox.ValueMember = "";  
                    rejectReason3ComboBox.ValueMember = "";  
                    lotNoTextBox.Text = "0";  
  
  
  
                    dOEDateTimePicker.Format = DateTimePickerFormat.Custom;  
                    dOEDateTimePicker.CustomFormat = strStartenddateformat;  
                    dOEDateTimePicker.Value = DateTime.Now;  
  
                    dOPDateTimePicker.Format = DateTimePickerFormat.Custom;  
                    dOPDateTimePicker.CustomFormat = strStartenddateformat;  
                    dOPDateTimePicker.Value = DateTime.Now;  
                }  
                else  
                {  
                    return;  
                }  
            }  
            catch (Exception ex)  
            {  
                MessageBox.Show("Saving Failed:" + ex.Message.ToString(), "Save",  
                    MessageBoxButtons.OK, MessageBoxIcon.Error);  
            }  
        }  

here's the image of my form:

59386-image.png

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,858 questions
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,717 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,409 questions
0 comments No comments
{count} votes

Accepted answer
  1. Michael Mathias Hachen 81 Reputation points
    2021-01-22T05:33:30.557+00:00

    Hi there

    You could save the values in some variables in your save method. Line 13:

    string valueTextBox1 = materiaNumberTextBox.Text; 
    

    ...

    After saving the values you could set the old values to the corresponding controls.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful