Reputation: 11
I'm currently trying to make an application (just for learning purposes to try to get used to C# because I'm kinda new) and I've wanted to create a sort of to say Terminal within the Form. I've decided to try to use a text-box with multiple lines and tried using if and else statements but when I go into the text box and start typing it immediately goes to the error message that I set up for 'else' after every keystroke. I don't know what it is but I feel like I'm missing something. Any suggestions? Also, would it be possible to create my own "commands" for that application alone in itself? I'm talking about like when you type in lets say "program_speech" it will come up with a dialog asking for user input and it will basically convert text to speech with the built in Speech Synthesizer for Windows. Thanks! All help is appreciated!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Terminator //Lol Terminator Reference
{
public partial class Form1 : Form
{
private string answer;
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (answer == "help")
{
MessageBox.Show("There is only 2 commands as of now and that is 'help' and 'program_speech' ");
}
else if (answer == "program_speech")
{
MessageBox.Show("Still currently under creation");
}
else
{
MessageBox.Show("Invalid Command. Please try again or type help for current available commands");
}
}
}
}
Upvotes: 0
Views: 17459
Reputation: 1857
I've read answer here.
Cause problem is already found. As text change event is being fired on every change of Textbox's text change.
Two good working suggestion here mentioned,
To use exclusive Button and perform logic on Button's click
To use Textbox's Lost Focus event.
Both approach require user to leave Textbox ultimately. (so he would need to re-enter in terminal (textbox) if he wants to enter another command.
But here I wonder why nobody have suggested to track Enter
press and do the logic only if Enter key being hit. Here user will not have to leave terminal (textbox) and fire another command without much effort.
you can do it like below.
First, use Textbox's key Up event, it will be fired later then Key Down (so to be sure that input is properly entered in textbox)
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
answer = textBox1.Text;
if (answer == "help")
{
MessageBox.Show("There is only 2 commands as of now and that is 'help' and 'program_speech' ");
}
else if (answer == "program_speech")
{
MessageBox.Show("Still currently under creation");
}
else
{
MessageBox.Show("Invalid Command. Please try again or type help for current available commands");
}
}
}
Upvotes: 0
Reputation: 12884
You have to make sure, you are initializing the value of answer
variable with the value of TextBox
and change the event from TextChanged to LostFocus
private void textBox1_LostFocus(object sender, EventArgs e)
{
answer = textBox1.Text;
if (answer == "help")
{
MessageBox.Show("There is only 2 commands as of now and that is 'help' and 'program_speech' ");
}
else if (answer == "program_speech")
{
MessageBox.Show("Still currently under creation");
}
else
{
MessageBox.Show("Invalid Command. Please try again or type help for current available commands");
}
}
Upvotes: -1
Reputation: 41
Alright,
I think I've found the problem.
The string
variable named "answer", is it where the command entered by the user should be stored ?
Because in your code, nothing mentions it,
so try to add this line at the beginning of text_changed
void :
answer = textBox1.Text;
If you're new to C#, this means you take the property Text of textBox1 and you store it in answer.
Hope it works !
Upvotes: -1
Reputation: 4088
Initialize answer
to textbox1.text
. I am assuming you have achieved it somehow. If not @Kishor's answer is the way you should do it.
This is happening because your textBox1_TextChanged
will get fired every time there is even a single change in your textbox. So when you type in any letter the textbox text changes and the function is fired thus triggering your else statement. For example you type h
, ==> Textbox registers the change and calls textBox1_TextChanged
and since the text is not help
if goes in the else part. This is repeated till hel
till u completely type in help
.
If you try when your textbox finally reads help
it will follow the MessageBox.Show("There is only 2 commands as of now and that is 'help' and 'program_speech' ");
command you specified.
As per the dialogue thing you will need to create a new form and call it when you want. Also as u mentioned you are i will recommend watching this tutorial. I know it is outdated but it covers most of your doubts. I started with it and I think so should you.
Also I don't think a multiple line textbox is the best choice when you want to make a terminal like structure. Hope I cleared most of your doubts.
Upvotes: 0
Reputation: 43
Better make a Enter Button and read in the text from the textbox, after pressing the enter button
private void Button1_Enter(object sender, EventArgs e)
{
input = textbox.Text;
//then do a switch case
Upvotes: 0
Reputation: 1277
At every keystroke an event called TextChanged is raised, it goes to else condition of 'Invalid Command' because the text in that textbox at that time is neither "help" nor "program_speech". Using TextChanged is definitely not recommended.
You should use a button and its click event to check the value of textbox. Because that's the only way you can be sure that all the required text is written. It would be something like -
private void btnCheckText_Click(object sender, EventArgs e)
{
answer = textBox1.Text;
if (answer == "help")
{
MessageBox.Show("There is only 2 commands as of now and that is 'help' and 'program_speech' ");
}
else if (answer == "program_speech")
{
MessageBox.Show("Still currently under creation");
}
else
{
MessageBox.Show("Invalid Command. Please try again or type help for current available commands");
}
}
Upvotes: 4