Aroueterra
Aroueterra

Reputation: 377

C# Exposing public methods/properties on one form then calling them from another

Here is the thread where it was suggested Rufus' Suggestion, but I can't seem to call Formtoshowonclosing because I'm only receiving a null pointer exception, am I right in thinking that the first form, QR is not being read? or are the methods from form QR unable to be read? It's not throwing any other errors.

Legend: Form A = QR, is loaded first, then is hidden. Form B = Account, is loaded next, where the user selects criteria for a database. Button is pressed, Account is closed and QR is shown, methods of QR are triggered from Account upon closing and database is loaded.

Here is what I've got so far, tell me what I seem to be missing.

Exposed properties on QR:

    public string BUName { get; set; }
    public string DOCUName { get; set; }

on public void QR()

            InitializeComponent();
            this.Hide();
            Account AccountForms = new Account();
            AccountForms.FormToShowOnClose = this;
            AccountForm.ShowDialog();

LoadData (Loads criteria from Account in preparation for Refresher(), which is a standard Select * from [Table]):

    public void LoadData()
    {
        txtBUnow.Text = BUName;
        txtDOCUnow.Text = DOCUName;
        if (BUName != "")
        {
            int BUtoQRConv = Convert.ToInt32(BUName);
            theDictionary = new DictionaryInit();
            DictionaryFindTable(BUtoQRConv, theDictionary.BUtoQRs);
            Refresher();
        }
    }

Account form:

    public static string passedBU;
    public static string passedDOCU;
    public QueryRefiner FormToShowOnClose { get; set; }

Button press:

        passedBU = lblBUitem.Text;
        passedDOCU = lblDOCUitem.Text;
        this.Close();

On Form closing:

    private void Account_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (FormToShowOnClose != null)
        {
        Console.WriteLine(passedBU + "!");
        FormToShowOnClose.BUName = passedBU;
        FormToShowOnClose.DOCUName = passedDOCU;
        FormToShowOnClose.LoadData();
        FormToShowOnClose.Show();
        }
    }

As it stands, there are no compiler errors, when it runs, the if statement doesn't seem to be triggering, and when extracted from the if statement, I do hit a nullpointer exception at FormToShowonClose. Any idea what I might be missing here? QR is already initialized as far as I can tell, just hidden.

Upvotes: 1

Views: 135

Answers (1)

long
long

Reputation: 232

Move this code to Load event handler of QR Form:

this.Hide();
Account AccountForms = new Account();
AccountForms.FormToShowOnClose = this;
AccountForm.ShowDialog();

Upvotes: 1

Related Questions