Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending Data from child form to Parent Form TextBox

I have a Parent Form that holds a "HUD" with First Name, Last Name, etc. One of the child forms is a Search Form. When the user selects a member from the results that are displayed in a DataGrid I want the pertinent information to fill in the HUD. I created a HUD class with variables for each value and a method called UpdateHUD(). I am unsure how to get this working. I have a reference to the Search Form of the Parent form containing the HUD, like so:

public frmWWCModuleHost _frmWWCModuleHost;

This is the code I use to embed forms. I am not using MDI.

 public static void ShowFormInContainerControl(Control ctl, Form frm)
    {
        frm.TopLevel = false;
        frm.FormBorderStyle = FormBorderStyle.None;
        frm.Dock = DockStyle.Fill;
        frm.Visible = true;
        ctl.Controls.Add(frm);
    }

Here is the code I am running on Cell Click on the Search Form. This is from before I tried implementing the HUD class.

private void dgvSearchResults_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        _frmWWCModuleHost = new frmWWCModuleHost();
        _frmWWCModuleHost.tbxHUD_LastName.Text = dgvSearchResults.CurrentRow.Cells[1].FormattedValue.ToString();
        _frmWWCModuleHost.tbxHUD_LastName.Invalidate();
        _frmWWCModuleHost.FormPaint();
    }

Thanks in advance!

~ Patrick


EDIT


dgvSearchResults_CellContentClick is now current. When I step through this code it is getting the correct Value here but it is never updating the actual HUD.


EDIT 2


Is my problem that I am declaring a NEW frmWWCModuleHost instead of passing a ref to the existing? I am still pretty weak in my understanding of this.


EDIT 3


I have "solved" this by doing the following: On the Parent Form where I declare the Child Form I pass this as a param. Then in the constructor of the child form I added _frmWWCModuleHost = m_parent; I have a UpdateHUD() method on my Parent form and I call it from the _CellClick event on the child.

Now to rephrase my question; Is there anything glaringly wrong with doing it this way?

like image 426
Refracted Paladin Avatar asked Dec 30 '22 02:12

Refracted Paladin


1 Answers

When the child form search completes, raise a "SearchCompleted" event. Then anything (including the parent form) can subscribe to that event and retrieve the details.

See the following NotepadCode for an example:

class ParentForm
{
    private readonly ChildForm childForm;

    public ParentForm()
    {
        InitializeComponent();

        childForm = new ChildForm();

        childForm.SearchCompleted += childForm_SearchCompleted;
    }

    private void childForm_SearchCompleted(object sender, SearchCompletedEventArgs e)
    {
        // Update the display
        lblName.Text = e.DataToDisplay;
    }
}

class ChildForm
{
    public event EventHandler<SearchCompletedEventArgs> SearchCompleted;

    private void Search(string query)
    {
        // Do the searching

        OnSearchCompleted(new SearchCompletedEventArgs([arg values]));
    }

    public void OnSearchCompleted(SearchCompletedEventArgs args)
    {
        if (SearchCompleted != null)
        {
            SearchCompleted(this, args);
        }
    }
}
like image 188
Neil Barnwell Avatar answered Jan 01 '23 17:01

Neil Barnwell