• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 518
  • Last Modified:

C# Calling a method in another form

I have two windows forms, FormPurchaseHistory and FormCustomer. FormPurchaseHistory opens up FormCustomer which displays a list of Customers in a DataGridView control. User clicks on a cell to select a customer and FormCustomer passes the CustomerID to FormPurchaseHistory and FormCustomer closes. There is a method in FormPurchaseHistoy I need to call after FormCustomer passes CustomerID to FormPurchaseHistory to retrieve purchase history by the customer. How do I fire that method?

I am using a static variable in a Module to pass value from FormCustomer to FormPurchaseHistory.
0
awu25
Asked:
awu25
  • 2
2 Solutions
 
Arthur_WoodCommented:
can you show us the code, in form FormPurchaseHistory, which opens FormCustomer ?

I would make the selected CustomerID a Public Property of FormCustomer .  I would also show FormCustomer  as a MODAL form, and when the form is closed, read the CustomerID property, and then use that value to retrieve the Customer History for the Selected customer.

AW
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Show your second form using ShowDialog().  textBox1 on Form2 has its Modifiers() property set to Internal.

    public partial class Form1 : Form
    {

        private void button1_Click_1(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            f2.ShowDialog();
            String value = f2.textBox1.Text;

            // do something with value now...like passs it to another method in this form...
            MessageBox.Show(value);
        }

    }
0
 
Arthur_WoodCommented:
like this:

    public partial class FormPurchaseHistory: Form
    {

        private void button1_Click_1(object sender, EventArgs e)
        {
            FormCustomer  fc = new FormCustomer ();
            fc.ShowDialog();
            String value = fc.CustomerID

            // do something with value now...like passs it to another method in this form...
            MessageBox.Show(value);
        }

    }


and in FormCustomer :


   private int m_CustomerID;
   public property CustomerID
   {
        get { return m_CustomerID;} // makes this a READ-ONLY property
   }


you would also have code in FormCustomer  that would set the internal variable m_Customer to be the value selected by the user.

AW
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now