简体   繁体   English

C# 将值从一个表单传递到另一个打开的表单

[英]C# Passing values from one form to another open form

I am learning the C# programming language and am making a Payroll program addon for SAP Business One.我正在学习 C# 编程语言,并正在为 SAP Business One 制作工资单程序插件。 I have two forms and I want to pass a value from PayrollFormulaBuilder.cs to EarnDeductSetup.cs.我有两个 forms,我想将 PayrollFormulaBuilder.cs 中的值传递给 EarnDeductSetup.cs。

The PayrollFormulaBuilder is used by a user to generate a formula and is saved to a string.用户使用 PayrollFormulaBuilder 生成公式并保存为字符串。 A user clicks on a calculator button on the EarnDeductSetup form in order to open up the PayrollFormulaBuilder form.用户单击 EarnDeductSetup 表单上的计算器按钮以打开 PayrollFormulaBuilder 表单。 The EarnDeductSetup form is still open but in the background. EarnDeductSetup 表单仍处于打开状态,但在后台。 I want the generated formula to show on my EarnDeductSetup form (I have a textbox, txt_formula_template.Text) as soon as a user clicks on an 'Apply button on the PayrollFormulaBuilder form.一旦用户单击 PayrollFormulaBuilder 表单上的“应用”按钮,我希望生成的公式显示在我的 EarnDeductSetup 表单(我有一个文本框,txt_formula_template.Text)上。 i would also like for this PayrollFormulaBuilder form to close as soon as the apply button is pressed.我还希望在按下应用按钮后立即关闭此 PayrollFormulaBuilder 表单。

Right now, I am unable to show the generated formula on my EarnDeductSetup form现在,我无法在 EarnDeductSetup 表单上显示生成的公式

My Code: (EarnDeductSetupForm)我的代码:(EarnDeductSetupForm)

namespace EIM_Payroll_Application
{
    public partial class EarnDeductSetupForm : Form
    {
        private SAPbobsCOM.Company company;

        public string SAPCodePD { get; set; }

        public EarnDeductSetupForm(SAPbobsCOM.Company co)
        {
            InitializeComponent();
            this.company = co;
        }

...

private void btn_calculator_Click(object sender, EventArgs e)
        {
            PayrollFormulaBuilder PC = new PayrollFormulaBuilder();
            PC.ShowDialog();
        }

... ...

    if (rb_calculated_amt.Checked == true)
    {
        txt_formula_template.Text = SAPUtility._formulaVariable;

        formulaTemplate = txt_formula_template.Text;
    }

SAPUtility.cs SAPUtility.cs

namespace Payroll.Util.Helpers
{
    public static class SAPUtility
    {
        public static string _formulaVariable = String.Empty;

        public static string variable
        {
            get { return _formulaVariable; }
            set { _formulaVariable = value; }
        }

...

PayrollFormulaBuilder.cs PayrollFormulaBuilder.cs

private void btnApply_Click(object sender, EventArgs e)
{
    SAPUtility._formulaVariable = formula_display.Text;

    this.Close();

    EarnDeductSetupForm.ActiveForm.ShowDialog();
}

My question is, how do I get this formula to show on my txt_formula_template.Text textbox on my EarnDeductSetupForm as soon as a user presses apply on the PayrollFormulaBuilder form?我的问题是,一旦用户按下 PayrollFormulaBuilder 表单上的应用,我如何让这个公式显示在我的 EarnDeductSetupForm 上的 txt_formula_template.Text 文本框中?

Set the button in the parent form to internal, then use the parent or parentform property in the dialog to access it, by casting it to the parent for type (this should expose the internal button with intellisense).将父表单中的按钮设置为 internal,然后使用对话框中的 parent 或 parentform 属性来访问它,方法是将其强制转换为类型的父项(这应该使用智能感知公开内部按钮)。 Call ShowDialog with "this" as parameter (no quotes).以“this”作为参数调用 ShowDialog(无引号)。

Subscribe to events on the internal button the parent form, in the child dialog.在子对话框中订阅父表单的内部按钮上的事件。 And do your thing in the event handler.并在事件处理程序中做你的事情。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM