0

For my coding project in c# I must create a program that uses multiple forms. Mine is an RPG character creator that will simply have the user select traits and stats, and then display them on the final form/screen. Given that, I've decided to create a Character class. The traits and stats are on different forms/screen so I create a UserCharacter object from the Character class and when they select "Next" it would add the traits to the properties of the Character object and would do the same on the second part of the creation(adding the stats). My question is how would I set the properties of the UserCharacter object that was created in one form, on the following?

On the next button click I have this

Character UserCharacter = new Character();
        SetCharacterProperties(UserCharacter);

        this.Hide();
        CharacterCreationScreen2 OpenCharacterCreationScreen2 = new CharacterCreationScreen2();
        OpenCharacterCreationScreen2.ShowDialog();
        this.Close();

This sets the first half of the Character Properties and then I open the next form. On the next form I have the stats which I would use a similar method to add those properties to an object, if possible I would like to add them to the UserCharacter created on the previous screen. Which I would then access a last time on the final form/screen to display the properties added.

If I use the method to open a new form instance for example:

CharacterCreationScreen1 AccessCharacterCreationScreen1 = new CharacterCreationScreen1();

I am able to use the SetCharacterProperties method, but not access the UserCharacter object.

1 Answers1

0

If I get what you mean, this will help you, even though it does not exactly do what you want to do:

class SelectTraits : Form {
    public String m_Trait_A = "";

    private void SetTraits() { ... }

    private void NextClicked(object sender, EventArgs e) {            
        this.DialogResult = DialogResult.OK;
        this.Close( );
    }
}

class SelectStats : Form {
    // Basically the same as SelectTraits
}

Now you have your "main form" and do:

using ( var ST = new SelectTraits( ) ) {
    if ( ST.ShowDialog( ) == DialogResult.OK ) {
        String TraitA = ST.m_Trait_A;
        using ( var SS = new SelectStats( ) ) {
            if ( SS.ShowDialog( ) == DialogResult.OK ) {
                String StatA = SS.m_Stat_A;

                // Now do what you want to do with those values from the other 2 forms
            }
        }
    }
}

So you open your main form, enter the select traits form, select your traits, press next. Return the control to the main form which then extracts the values from SelectTraits and immediately opens SelectStats where you do the "same" and on return you gather the inputs from that form as well and then have everything in your main form.

Sheradil
  • 407
  • 3
  • 14