简体   繁体   English

关闭另一张表格或通过按钮刷新另一张表格

[英]Refreshing one form on close of another or via a button click on another form

My program allows the user to edit the data in a database. 我的程序允许用户编辑数据库中的数据。 This works by showing the user the "product" on one form and then asks them to insert the correct amount of stock on another. 这通过在一种形式上向用户显示“产品”,然后要求他们在另一种形式上插入正确数量的库存来起作用。

I am trying to make it so that the first form with the "product" is refreshed to show the amended numbers from the second form with either the closure of the second or via a button click on the second. 我正在尝试使带有“产品”的第一个表格刷新,以显示第二个表格的修改数字,或者关闭第二个表格或通过单击第二个按钮。

I unfortunately have no knowledge of how to do this. 不幸的是,我不知道如何执行此操作。

I know that it's not: 我知道不是:

frm1 f1 = new frm1(this);
f1.Show();

Code for amend form: 修改形式代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace InventoryManager
{
    public partial class frmAdjustment : Form
    {
        frmAmendStock _main;

        public string enteredSKU { get; set; }

        public frmAdjustment(frmAmendStock main)
        {
            InitializeComponent();
            _main = main;
        }        

        private void frmAdjustment_Load(object sender, EventArgs e)
        {
            this.AcceptButton = btnSubmit;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            using (OleDbConnection connect = new OleDbConnection())
            {

                connect.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Other\Documents\University Work\USB\Individual Project\Artefact\InventoryManager\InventoryManager\stock.mdb";
                connect.Open();

                OleDbCommand cmd = new OleDbCommand("UPDATE items SET Stock = @stock, Stock_Counted = @counted WHERE SKU LIKE '" +enteredSKU+"'", connect);
                string units = txtAmount.Text;

                    if (connect.State == ConnectionState.Open)
                    {
                        if (string.IsNullOrEmpty(units))
                        {
                            MessageBox.Show("Please enter the correct amount of units.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        }
                        else
                        {
                            cmd.Parameters.Add("@stock", OleDbType.Integer, 5).Value = txtAmount.Text;
                            cmd.Parameters.Add("@counted", OleDbType.Integer, 5).Value = txtAmount.Text;

                            try
                            {
                                cmd.ExecuteNonQuery();
                                MessageBox.Show("Stock Adjusted", "Saved", MessageBoxButtons.OK, MessageBoxIcon.Information);

                                txtAmount.Clear();

                                connect.Close();
                                this.Close();
                            }
                            catch (Exception expe)
                            {
                                MessageBox.Show(expe.ToString());
                                connect.Close();
                            }
                      }
                }
                else
                {
                    MessageBox.Show("Connection Failed");
                }
            }
        }
    }
}
  1. Create a public property "Stock" in the second form and set its value if the user clicks the "Ok" button 在第二种形式中创建一个公共属性“股票”,并在用户单击“确定”按钮时设置其值
  2. Open the second form as a modal dialog 打开第二个窗体作为模式对话框
  3. When the second form closes, check if the "Ok" button was pressed and if so, get the value from the "stock" property. 当第二个窗体关闭时,检查是否按下了“确定”按钮,如果按下了,请从“股票”属性中获取值。 Remember you still have a reference to this form after it's been closed. 请记住,关闭该表单后,您仍然可以引用该表单。

Example :` 例子:

 if (f1.ShowDialog(this) == DialogResult.OK){
     var stock = f1.Stock;
  }

Makes sense ? 说得通 ?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单击按钮从另一个表单刷新DataGridView - Refreshing DataGridView from another form on button click 在另一个表单的按钮单击事件后刷新另一个表单datagridview - Refreshing another forms datagridview upon button click event of another form 如何通过单击另一个表单的按钮来更改一个表单中的按钮的文本? - How to change the Text of a Button in One form with a Button click of another Form? 关闭一个表单并显示另一个 - Close one form and display another 单击按钮将值从一个表单传递到另一个表单 - Passing Values from one Form to another Form in a Button click 关闭Windows窗体可用于一种窗体,但不能应用于另一窗体 - close windows form works on a form but not on another one 启用一种形式的按钮另一种形式的按钮单击 - Enabling button of one form another forms button click 单击按钮通过另一个表单重新加载表单 - Reload a Form through another Form by Button click 关闭子窗体,然后单击C#.net中的用户控件中的按钮,打开另一个窗体? - Close a child form and open another form on click of a button from user control in C#.net? 呼叫按钮单击事件从一种Web表单转移到另一种Web表单 - Call button click event from one web form to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM