levelonehuman
levelonehuman

Reputation: 1505

Getting Console.ReadLine() functionality in WinForms

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.

Upvotes: 0

Views: 4892

Answers (1)

DanielS
DanielS

Reputation: 774

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.

Upvotes: 1

Related Questions