0

I have 2 forms, One is is MainProgram.cs for the MainProgram Form. Second, AddNewUser.cs for the NewUser Form. In MainProgram there is a button to open up NewUser which I open by using this code in MainProgram.cs

private void addNew_Button_Click(object sender, EventArgs e)
{
    AddNewUser addUserForm = new AddNewUser();
    addUserForm.Show();
}

The method I'm calling is to reset the textfields in and with the method in AddNewUser from MainProgram. This code is in AddNewUser.cs

public void ResetTextFields_NewUser()
{
    textBox_Username.ResetText();
    textBox_Password.ResetText();
}

This method works if called by a button in the same form. But when I try to call it from MainProgram it does not work. So something is wrong, I'm not initializing the correct way or referencing the existing class because it does work just not when I try to reference the class.

Here is the code in AddNewUser that calls MainProgram to then call AddNewUser again.

AddNewUser.cs inside a Button in the Form

private void addNewUser_Button_Click(object sender, EventArgs e)
{
    MainProgram mainForm = new MainProgram();
    mainForm.Add_DataValues();
}

MainProgram.cs a public method inside the Form

public void Add_DataValues()
{
    AddNewUser addUserForm = new AddNewUser();
    addUserForm.ResetTextFields_NewUser();

    //  reason I'm going back and forth is because in this method im also using variables 
    //  from MainProgram form and instead from grabbing it from AddNewUser.cs I just use it 
    //  from the class itself and then call AddNewUser to reset the textfields 
    //  by a simple public method. but its not working
}

Reason I think the problem is that I'm not referencing the existing class is because I added a MessageBox.Show(""); to all the methods on here and it all showed up so the code works but not to the existing class. Its creating a new one instead of referencing an existing one.

WokerHead
  • 947
  • 2
  • 15
  • 46
  • 6
    `new AddNewUser()` creates a ***new*** form instance each time, and since the 2 snippets shown are local, you dont have a reference to any user form for `ResetTextFields`. You need to hold onto the reference you create or fish it out of `OpenForms` collection – Ňɏssa Pøngjǣrdenlarp Jan 06 '20 at 16:01
  • 1
    It looks like you need to separate your data elements from your form classes. You might be relying on the form classes to convey data, requiring you to create new instances (e.g. `new MainProgram()`) that are improper and confusing. – madreflection Jan 06 '20 at 16:06
  • @ŇɏssaPøngjǣrdenlarp how would I hold onto the reference? Do I set `new AddNewUser` globally ? New to c# – WokerHead Jan 06 '20 at 18:31
  • @madreflection but in my forms I would still have to reference that script and that's my problem I don't know how to reference without `new AddNewUser()` – WokerHead Jan 06 '20 at 18:32
  • Assuming I've inferred enough about your code from what little you've shown, you need to refactor it so that the data doesn't exist as part of any form. Unfortunately, you haven't provided enough context to make a more specific recommendation than that. – madreflection Jan 06 '20 at 18:44
  • @madreflection the data is from textfields that the user inputs but how would I reference an existing instance instead of creating a new one? – WokerHead Jan 06 '20 at 18:53
  • Create a class with properties for the values you need. Expose a property of that (container) type on your Add User form. When the form is closed/submitted, read the text boxes and put those values into the property (of the container type), and read that property after the form closes (in the parent form). If you want anything more specific than that, you'll need to [edit] your question to provide more context. – madreflection Jan 06 '20 at 18:55
  • @madreflection I edited the title so you can help me better. I'm having trouble understanding you sorry. Expose a property? – WokerHead Jan 06 '20 at 19:07
  • It's not the title that was lacking. You haven't provided enough code to see how things fit together, i.e. context. – madreflection Jan 06 '20 at 19:30
  • 3
    `MainProgram mainForm = new MainProgram();` is your cuprit. You create it and never show it. It's most likely already on the screen, so you need to reference that existing one. Add a constructor to your `AddNewUser(MainProgram mainForm)` class, then pass it when you create it: `...new AddNewUser(this);` – LarsTech Jan 06 '20 at 19:50
  • @DourHighArch yes it does! thank you – WokerHead Jan 06 '20 at 20:26
  • @LarsTech that's what I was missing. Referencing the existing class not creating a new one. – WokerHead Jan 06 '20 at 20:26

0 Answers0