Reputation: 3
I've started learning to code C# within the past week or so and I decided to try and make a small text based game. My issue is that I can't get the input from the textbox i've placed on the form. Or rather, from when I run it, it looks like it takes the nothing that's inside the textbox and uses that in the switch, making it always pick the default case.
namespace TextCat {
public partial class form1 : Form
{
public string choiceSwitch;
public double choice = 0;
public form1()
{
InitializeComponent();
rtbDialogBox.Text += "";
rtbChoice1.Text = "1) ";
rtbChoice2.Text = "2) ";
rtbChoice3.Text = "3) ";
rtbChoice4.Text = "4) ";
switch (choiceSwitch = txtboxInput.Text)
{
case "1":
choice = 0.1;
break;
case "2":
choice = 0.2;
break;
case "3":
choice = 0.3;
break;
case "4":
choice = 0.4;
break;
default:
rtbDialogBox.Text += "Invalid selection, please input 1-4.";
break;
}
if (choice == 0.1)
{
rtbDialogBox.Text = "";
}
Upvotes: 0
Views: 69
Reputation: 26
The way you are doing it, that will only get ran once when the form gets created. So it just uses the uninitialized value of txtboxInput.Text. You need to do your work in an eventhandler, not in the constructor of the form, possibly in a TextChanged event, or if you want to detect keys (ex. Enter), KeyUp or KeyDown event.
See the documentation of Textbox events for more information: https://msdn.microsoft.com/en-us/library/system.windows.forms.textbox_events(v=vs.110).aspx
Upvotes: 1