Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
291 views
in Technique[技术] by (71.8m points)

c# - retain previous value of text box till the form is closed

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 the textbox is still empty:

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: enter image description here

question from:https://stackoverflow.com/questions/65839497/retain-previous-value-of-text-box-till-the-form-is-closed

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can try the following code to save the first input text in the textbox.

Please use the event textbox_Leave event.

 private void button1_Click(object sender, EventArgs e)
        {
            //Update the database
            MessageBox.Show("Update success");
            textBox1.Text = textbox; // return to the first input in the textbox
     
        }
        int i = 0;
        string textbox = "";
        private void textBox1_Leave(object sender, EventArgs e)
        {
            if (i == 0)
            {
                textbox = textBox1.Text;
                i++;
            }
        }

Result:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...