0

I'm in the process of writing a basic text game using WinForms; this started off as a Console app.

In my Player class, I have this method, which currently reads a name and characterClass from Console.ReadLine() and builds the Player object from these parameters:

public static Player CreateCharacter()
{
    string name;
    string characterClass;

    //InputOutput.Write("Please enter your character's name: ");
    name = Console.ReadLine();

    characterClass = Console.ReadLine(); //SelectClass();

    switch (characterClass.ToLower())
    {
        case "warrior":
            return new Player(name, new Warrior());
        case "archer":
            return new Player(name, new Archer());
        case "mage":
            return new Player(name, new Mage());
        default:
            throw new ArgumentException();
    }
}

This method worked well with the Console project, but now that I'm moving over to WinForms I'm unsure how to get similar functionality from a TextBox.

I am trying to build a method similar to the following:

public void StartNewGame()
{
    AddTextToOutputBox("To begin, please enter a name for your character: ");
    GetInputFromTextbox(input1); //wait for input from textbox
    AddTextToOutputBox("Now, enter a class. [Warrior], [Archer], or [Mage]:");
    GetInputFromTextbox(input2); //wait for input from textbox

    Player.CreateCharacter(input1, input2); //create the player
}

Where I output a message to OutputBox, wait for input from InputBox, and create a Player from these parameters.

I'm unsure how to proceed here, but I would like the functionality to be similar to that of Console.ReadLine() because (as it stands now), the method I'm trying to call depends on input from the user.

  • What options are available to mimic the functionality of Console.ReadLine() with a TextBox in WinForms? Is there a different control better suited to this task?
  • Is this method the "right" way to do what I'm trying to accomplish, or would I be better off with a different method altogether?
2
  • You put your input fields on a form (name, class, etc) and then you have a submit or next or done, or what have you button. The user fills in the text boxes and presses the button. You process the event that the button generates on click in your code. Commented Dec 30, 2015 at 21:21
  • Why don´t you monitor the TEXTBOX events (like TextChanged) to ensure that have a valid text there before exit to the next question to user? You may also utilize a Friend/Public variable to check this between different textboxes events. Commented Dec 30, 2015 at 21:37

1 Answer 1

1

In Win Forms user interaction paradigm is slightly different from Console apps. You can fill many fields and then submit a form with all the values at once.

In your case I would use TextBox control for the character name and a ComboBox or a RaioButton control for the character class.

Then, when user clicks the submit button, Player.CreateCharacter(input1, input2); //create the player is called.

In addition, you can use validation controls to verify user input.

Sign up to request clarification or add additional context in comments.

1 Comment

This is what I've ended up doing. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.