Hello, I have made two textboxes and a button, you type into the second textbox and it's content gets displayed on the first texbox when you click the button, and also creates a new line so I can type as much as I want without it clearing. I want to create an auto responder, so if I type something like "sup" it will respond saying "Hi" or something, without clearing the entire textbox.

Ive tried many different methods, they all seem to end up spamming.

Thanks for your time!

Edited 7 Years Ago by Jaydenn: n/a

Try yourtextbox.Text +="Your new line of text here \r\n"; That gives you a carriage return and newline, and do this for every line that you add.

Still doesn't work, it just spams my message.

Someone tell me if this is what I'm supposed to be doing;

private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (textBox1.Text.Contains("lolsup"))
            {
                textBox1.Text += "Client: Hey \r\n";
            }
        }

I should have tested it further, apologies. I thought I had it but it kept crashing after the word was entered

It's not exactly what you want but it requires an enter press after "lolsup" is entered

private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
                if (textBox1.Text.Contains("lolsup"))
                {

                    textBox1.Text += "\r\nClient: Hey";
                    textBox1.SelectionStart = textBox1.Text.Length;
                    textBox1.ScrollToCaret();
                }

        }

if you are adding the reply to the same textbox then it will continue to spam the message..
First time you hit enter, textbox text = "lolsup" so code adds "\r\nClient: Hey".
Second time you hit enter the texbox text = "lolsup\r\nClient: Hey" so it still passes the If condition and adds "\r\nClient: Hey" again.

You need to do one of two things.
a) Instead of if (textBox1.Text.Contains("lolsup")) have it check only the last line for the matching text

b) Instead of checking for "lolsup" in textbox1 after you have added the user input, check the user input in textbox2, append if necesary, then transfer it to textbox1. Then clear textbox2 ready for the users next input. That way you are only checking the each input string once.

Am i right in thinking this is for some kind of instant messaging program? It sounds as though you are trying to produce an MSN-like interface, ie each line of text you type gets added to the bottom of the chat window and the automatic responses are added underneath like replies. If thats the case, i'd recommend that you rethink your program structure. Seperate the autoresponse code from the input code.
SO user enters text in textbox. When they click on a button the text is added to the chat window, but also passed to a method that checks for responses:

private void btnSubmit_Click(object sender, EventArgs e)
        {
            string input = txtInput.Text;

            //validate input if necesary

            txtChatLog.AppendText(input + Environment.NewLine);
            AutoRespond(input);
            txtInput.Clear();
            txtInput.Focus();

        }

        private void AutoRespond(string input)
        {
            if (input.Contains("lolsup"))
            {
                txtChatLog.AppendText("Client: Hey" + Environment.NewLine);
            }
        }

By seperating related code segments out into methods you make your code more managable and easier to read.

This question has already been answered. Start a new discussion instead.