2

I am making an application that loads a separate form, the user puts in information, and then when done, it will show up on the primary form the application loaded with first.

The issue is that I tried multiple solutions to get this to load in, but it will not load in after the information is put in. I have tried this.Controls.Add(Label); which is what I have seen the most, but it has not worked. Another way I tried was doing Label.Show();, but the same result, with nothing showing. The AddContacts(string Name) method below is how I add the contact

The AddContact_Click(object sender, EventArgs e) method is a button that, when pressed, opens another form that allows information to be inserted.

public partial class Phonebook : Form
{

public Phonebook()
    {
        InitializeComponent();
        MaximumSize = new Size(633, 306);
    }

private void AddContact_Click(object sender, EventArgs e)
    {
        MakeContact MC = new MakeContact();
        MC.Show();
    }

public void AddContacts(string Name)
    {
        Label name = new Label();

        //Added Style and Location of Label...
        name.Text = Name;
        name.Location = new Point(98, 13);
        name.Font = new Font("Microsoft Sans Serif", 13, FontStyle.Bold);

        this.Controls.Add(name);
        Refresh();
    }
}

Below is the Method I used when the Finish button is pressed, for when the user is done with the information, and then the AddContacts() method is called

public partial class MakeContact : Form
{

    public MakeContact()
        {
            InitializeComponent();
            MaximumSize = new Size(394, 377);
        }

private void FinishContact_Click(object sender, EventArgs e)
    {
        //FullName is the name of the TextField when asking for a name
        string Name = FullName.Text;

        Phonebook PB = new Phonebook();
        PB.AddContacts(Name);

        //Closes Separate Form and goes back to the
        Close();
    }
}

Expectation: It should load the label into the form after the information is put in.

Actual: It will not show what so ever.

EDIT: Added More to the Code and to the Question since I didn't do too good of asking the question, sorry about that :/

  • Who calls `AddContacts` method? Are there any other controls on the Form? You are not setting the location of the Label before adding to the form. So the label might be hidden behind other controls on the form. You are also not setting the Text property of the label. – Chetan Feb 12 '19 at 02:19
  • It appears that you're creating a new `MakeContact` Form from the `Phonebook` Form. But then, in the `MakeContact` Form, you try to call the `AddContacts` method of a **new** `Phonebook` Form: the original Form will not be affected by any changes you make to this new instance. You need to pass the current instance of `Phonebook` to the `MakeContact` Form's Constructor when you create it, so this new Form can use the current instance of `Phonebook`. In this case, calling a public method of `Phonebook` will produce the result you're expecting. – Jimi Feb 12 '19 at 12:18
  • For example, in `MakeContact`: `private PhoneBook pb = null; public MakeContact(PhoneBook phoneBook) { this.pb = phoneBook }`. In `FinishContact_Click`: `{ this.pb.AddContacts(Name); }` – Jimi Feb 12 '19 at 12:22
  • Hey, sorry for the late response. I understand what you are saying and I gave your example a try, but when I do, I get an error saying that when I call `MakeContact`, that it will require 1 argument, which was `Phoneboook phoneBook`. I am assuming you mean put `Phonebook phoneBook` where I have `InitializeComponent();`, but even if I did another method, it would give me a `NullReferenceException`, maybe I don't understand you correctly, which i'm sorry about because i'm quite new with the language. –  Feb 13 '19 at 02:48

1 Answers1

0

An example of what I described in the comments:

When you do this:

Phonebook PB = new Phonebook();

you create a new instance of the PhoneBook class (your form): this is not the same Form instance (the same object) that created the MakeContact Form and the one you're trying to update. It's a different object.
Whatever change you make to this new object, it will not be reflected in the original, existing, one.

How to solve:
Add a Constructor to the MakeContact Form that a accepts an argument of type PhoneBook and a private object of type Phonebook:

    private PhoneBook pBook = null;

    public MakeContact() : this(null) { }
    public MakeContact(PhoneBook phoneBook)
    {
        InitializeComponent();
        this.pBook = phoneBook;
    }

Assign the argument passed in the constructor to the private field of the same type. This Field will then used to call Public methods of the PhoneBook class (a Form is a class, similar in behaviour to other class).

It's not the only possible method. You can see other examples here.

Full sample code:

public partial class Phonebook : Form
{
    private void AddContact_Click(object sender, EventArgs e)
    {
        MakeContact MC = new MakeContact(this);
        MC.Show();
    }

    public void AddContacts(string Name)
    {
        Label name = new Label();
        // (...)
        this.Controls.Add(name);
    }
}

public partial class MakeContact : Form
{
    private PhoneBook pBook = null;

    public MakeContact() : this(null) { }
    public MakeContact(PhoneBook phoneBook)
    {
        InitializeComponent();
        this.pBook = phoneBook;
    }

    private void FinishContact_Click(object sender, EventArgs e)
    {
        string Name = FullName.Text;
        this.pBook?.AddContacts(Name);
        this.Close();
    }
}
Jimi
  • 29,621
  • 8
  • 43
  • 61