保留文本框的先前值直到窗体关闭

retain previous value of text box till the form is closed

我对c#很陌生,正在学习制作数据输入应用程序, 在我的输入表单中,当用户单击“保存”时,所有数据文本框都会刷新并保存到数据库中,文本框显示为空以输入增益。这是一个连续的过程。 现在我希望我的 textbox1 在关闭表单之前保留用户首次输入的相同值。 请帮助我如何实现这一目标? 我试过这段代码,但文本框仍然是空的:

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

这是保存时执行的代码:

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);
             }
         }

这是我的表格图片:

使用数据 reader 从数据库中获取值,然后使用这些值将值填充到文本框中。

您可以试试下面的代码来保存文本框中第一个输入的文本。

请使用事件textbox_Leave事件。

 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++;
            }
        }

结果: