0

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");
            }
        }
    }
}
1
  • Should be "There are only 2 commands" :P Commented May 13, 2018 at 20:04

6 Answers 6

4

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");
        }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Okay, I see what happened. Thank you! I got it working
0

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

Comments

0

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.

6 Comments

Thank you! Now for that last part about the Terminal Interface, how would I approach that?
I can only drop some tips. Make 2 textboxes. 1 non readonly. in the other one the user gives his input and hits a button next to it. On buttonclick you clear the textbox content and release the output in the other textbox with the if conditions you had.
If you dont want the button see this and make sure you set onKeyDown listener to the function from here
@Rishav OnKeyDown listerner will still have the same issue the OP faced actually. It will be raised on every keydown which will create the same problem and the control will go in 'else' part.
@ArpitGupta If you check the link i provided you will find that the answer checks specifically if Enter is hit or not. IF hit then the OP should call the function which checks the condition (here textBox1_TextChanged, not the best function name in this case as it is no longer an textchange event). The OP in my case does not have a listener to textboxChange.
|
0

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");
           }
        }
    }

Comments

-1

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 !

Comments

-1

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");
  }
}

2 Comments

Thank you! I just added it in but I seem to be getting the same issue with the error after every press of a key
Then you should change the event from TextChange to LostFocus

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.