[RESOLVED] MainForm's object calling a method from MainForm?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: [RESOLVED] MainForm's object calling a method from MainForm?

  1. #1
    Join Date
    Apr 2013
    Posts
    11

    [RESOLVED] MainForm's object calling a method from MainForm?

    Hi there. I've got a bunch of classes, they look like this:
    (Arrow means the left one got the right one as an object)
    MainForm -> ContactForm -> Contact -> Adress + Phone + Email MainForm -> CustomerManager (has a lsit of..) Customer -> Contact -> Adress + Phone + Email
    MainForm is the Form that has a list of all Customers. ContactForm is the Form where you enter information about a customer. CustomerManager has a list of Customers.
    Now, I need to have MainForm add things to the list; simple List<T>.Add. But the add method needs to be called by something in ContactForm in order to have it added to the list. It's a school assignment, so I can't create or remove classes or their objects. ContactForm cannot call MainForm since it's MainForm's object. I tried making a bool in MainForm that changed when ContactForm exited, but that didn't work.
    How would I go about? I don't think I can post all of the classes here, but tell me which one's missing and I'll post it up.

    Here's my MainForm:
    using System;
    using System.Drawing;
    using System.Windows.Forms;

    namespace assignment_5
    {
    /// <summary>
    /// Description of MainForm.
    /// </summary>
    public partial class MainForm : Form
    {
    private CustomerManager m_manager;
    private CustomerManager m_copyManager;
    private ContactForm m_contact;
    private int id = 0;

    public MainForm()
    {
    InitializeComponent();
    m_manager = new CustomerManager();
    m_copyManager = new CustomerManager(m_manager);
    }

    private bool CheckInput(int index)
    {
    if (index > 0 && index < m_manager.CustomerListCount)
    return true;
    else
    return false;
    }

    public void NewListItem()
    {
    id++;
    string customerString = id + m_contact.FinalString;
    customerList.Items.Add(customerString);
    }

    void AddBtnClick(object sender, EventArgs e)
    {
    m_contact = new ContactForm();
    m_contact.ShowDialog();
    }
    }
    }

    And here's my ContactForm, which is an object of MainForm.
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Windows.Forms;
    using assignment_5.ContactFiles;

    namespace assignment_5
    {
    /// <summary>
    /// Description of MainForm.
    /// </summary>
    public partial class ContactForm : Form
    {
    private ContactFiles.Contact m_contact;
    private bool instaClose = false;
    string stringFinal;

    /// <summary>
    /// Default constructor.
    /// </summary>
    public ContactForm()
    {
    InitializeComponent();
    m_contact = new assignment_5.ContactFiles.Contact();
    InitializeGUI();
    }

    /// <summary>
    /// Sets-up certain things to make the interface work.
    /// </summary>
    private void InitializeGUI()
    {
    FillCountryBox();
    countryList.SelectedIndex = 0;
    }

    #region Properties

    /// <summary>
    /// Allows access to the m_contact object.
    /// </summary>
    public Contact ContactData
    {
    get {return m_contact;}

    set
    {
    if (value != null)
    m_contact = value;
    }
    }

    public bool InstantClose
    {
    get {return instaClose;}
    }

    #endregion

    public string FinalString
    {
    get {return stringFinal;}
    }

    private void FillCountryBox()
    {
    string[] listing = m_contact.AdressData.GetCountryArray();

    for (int count = 0; count < listing.Length; count++)
    {
    countryList.Items.Add(listing[count].Replace("_", " "));
    }
    }

    void OkBtnClick(object sender, EventArgs e)
    {
    //Filling in from the listboxes
    m_contact.FirstName = box_firstName.Text;
    m_contact.LastName = box_lastName.Text;
    m_contact.AdressData.Street = box_street.Text;
    m_contact.AdressData.Zipcode = box_zipCode.Text;
    m_contact.AdressData.City = box_city.Text;
    m_contact.AdressData.Country = countryList.SelectedItem.ToString();
    m_contact.EmailData.Personal = box_privateEmail.Text;
    m_contact.EmailData.Work = box_workEmail.Text;
    m_contact.PhoneData.Home = box_homePhone.Text;
    m_contact.PhoneData.Work = box_mobilePhone.Text;
    instaClose = true;

    FinalStage();
    this.Close();
    }

    private void FinalStage()
    {
    if (CheckData())
    MessageBox.Show(GetFinalString());
    else
    MessageBox.Show("Please enter information in all the fields.", "Missing information");
    }

    private bool CheckData()
    {
    if ((m_contact.CheckData())
    && (m_contact.AdressData.CheckData())
    && (m_contact.EmailData.CheckData())
    && (m_contact.PhoneData.CheckData()))
    return true;
    else
    return false;
    }

    void CancelBtnClick(object sender, EventArgs e)
    {
    this.Close();
    }

    void ContactFormFormClosing(object sender, FormClosingEventArgs e)
    {
    if (instaClose == false)
    {
    DialogResult closeResult = MessageBox.Show("If you close, you will lose all data." + Environment.NewLine + "Continue?", "Data Loss Imminent",
    MessageBoxButtons.OKCancel);
    if (closeResult == DialogResult.OK)
    e.Cancel = false;
    else
    {
    e.Cancel = true;
    DefaultAllValues();
    }
    }
    }

    private void DefaultAllValues()
    {
    m_contact.DefaultValues();
    m_contact.AdressData.DefaultValues();
    m_contact.EmailData.DefaultValues();
    m_contact.PhoneData.DefaultValues();
    }

    private string GetFinalString()
    {
    stringFinal = m_contact.FirstName + ", " + m_contact.LastName + ", " + m_contact.PhoneData.ToString() + m_contact.EmailData.ToString() + m_contact.AdressData.ToString();
    return stringFinal;
    }

    public override string ToString()
    {
    return string.Format("{0}, {1}", m_contact.FirstName, m_contact.LastName);
    }
    }
    }


    To summarize; how do I call a method in MainFrame - public void NewListItem() - from its objects?
    I've been given instructions that say I cannot have any other constructor but the default ones (Adress is an exception though)
    and that I cannot create or remove classes, plus I have to retain the object order shown above.


    Help greatly appreciated! Thanks!

  2. #2
    Arjay's Avatar
    Arjay is online now Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    10,957

    Re: MainForm's object calling a method from MainForm?

    What you want to do is separate the data from the various views. So you have a single CustomerManager class with a list of Customer objects.

    I am assuming that you select a customer in a list in the mainForm form and then open the ContactForm form?

    If this is correct, then you just pass a reference of the selected customer to the ContactForm. The contact form only ever acts on the data in the [selected] customer's instance. Using this approach, the ContactForm doesn't know about the MainForm or any other form as all forms just act on data.

  3. #3
    Join Date
    Apr 2013
    Posts
    11

    Re: MainForm's object calling a method from MainForm?

    You're almost correct, but I want to add to the list of customers that's located in MainForm. Basically, you start the program with MainForm which will show you an empty listbox. You then click on 'Add' and ContactForm opens. That's where you fill out all the information about a customer. When you're done, you click 'Ok' and the ContactForm closes and the customer is added to the listbox. But how does MainForm know when you're done with ContactForm? ContactForm needs to send a signal to MainForm that it's list-adding-to-time.

  4. #4
    Arjay's Avatar
    Arjay is online now Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    10,957

    Re: MainForm's object calling a method from MainForm?

    Quote Originally Posted by sadslapper View Post
    You're almost correct, but I want to add to the list of customers that's located in MainForm. Basically, you start the program with MainForm which will show you an empty listbox. You then click on 'Add' and ContactForm opens. That's where you fill out all the information about a customer. When you're done, you click 'Ok' and the ContactForm closes and the customer is added to the listbox. But how does MainForm know when you're done with ContactForm? ContactForm needs to send a signal to MainForm that it's list-adding-to-time.
    Okay, do you need to edit existing customers, if so, make the ContactForm handle both conditions. While your at it, research such terms as document/view (older term), or MVC.

  5. #5
    Join Date
    Apr 2013
    Posts
    11

    Re: MainForm's object calling a method from MainForm?

    Quote Originally Posted by Arjay View Post
    Okay, do you need to edit existing customers, if so, make the ContactForm handle both conditions. While your at it, research such terms as document/view (older term), or MVC.
    I got it figured out, I just didn't know that methods 'pause' if they open a dialog, then resume when the dialog closes. Thanks a ton anyway!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center