简体   繁体   中英

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. I have two forms and I want to pass a value from PayrollFormulaBuilder.cs to EarnDeductSetup.cs.

The PayrollFormulaBuilder is used by a user to generate a formula and is saved to a string. A user clicks on a calculator button on the EarnDeductSetup form in order to open up the PayrollFormulaBuilder form. The EarnDeductSetup form is still open but in the background. 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. i would also like for this PayrollFormulaBuilder form to close as soon as the apply button is pressed.

Right now, I am unable to show the generated formula on my EarnDeductSetup form

My Code: (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

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

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?

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). Call ShowDialog with "this" as parameter (no quotes).

Subscribe to events on the internal button the parent form, in the child dialog. And do your thing in the event handler.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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