Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

C# Need help with windows forms

Krishot7

New Coder
I made a simple talking program when I just started learning c#. Now I am an average and know a bit about this language, and I decided to take my old program (Which was a console application) and made a new windows forms app with an Input field to Simulate Console.Readline and Output for WriteLine and a next button to move forward and submit what's there in the input field. I completed the code and made 2 lines 'Hi and What's your name' just for testing and while it should have worked, it didn't. Here's the code-
C#:
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 WinFormsApp1

{

    public partial class Form1 : Form

    {



        public bool SubmitClick;

        public int ConvNum;



        public Form1()

        {

            InitializeComponent();



            ConvNum = 1;



            if (ConvNum == 1)

            {

                OutputText.Text = "Hi";

            }



            if (ConvNum == 2)

            {

                OutputText.Text = "What is your name?";

            }

        }



        private void button1_MouseClick(object sender, MouseEventArgs e)

        {

            InputText.Text = " ";

            SubmitClick = true;

            ConvNum = ConvNum + 1;

        }

    }

}
 
By the way, here is the old program I made watching Brackeys' C# course
C#:
using System;

namespace Talketive_Machine
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WindowHeight = 30;
            Console.WindowWidth = 100;
            Console.Title = "Talking Machine";

            Console.WriteLine("Hi what is your name?");

            string userName = (Console.ReadLine() );

            Console.WriteLine("Hi " + userName + "\n I am an AI");
            Console.WriteLine("So what is your favorite activity? Mine is making people happy :)");

            string favoriteHobby = (Console.ReadLine() );

            Console.WriteLine("Cool! I also like " + favoriteHobby + "!");
            Console.WriteLine("So how can I help you?");

            Console.ReadLine();

            Console.WriteLine("Are you sad, bored or happy?");

            string emotion = Console.ReadLine();

            switch (emotion)
            {
                case "sad" or "Sad":
                    Console.WriteLine("I can tell you a joke. This will cheer you up. \nWhat do you call it when a dinosaur crashes his car?");
                    Console.ReadLine();
                    Console.WriteLine("Tyrannosaurus wrecks!");
                    break;

                case "bored" or "Bored":
                    Console.WriteLine("Lets Play a Quiz!");
                    Console.WriteLine("What is the capital of Russia?");

                    string boredQuizAnswer = Console.ReadLine();
                    {
                       switch (boredQuizAnswer)
                        {
                            case "moscow" or "Moscow":
                                Console.WriteLine("Correct!");
                                break;

                            default:
                                Console.WriteLine("Wrong Answer");
                                break;
                        }
                    }
                    break;

                case "happy" or "Happy":
                    Console.WriteLine("Then why are you even here?");
                    break;
            }

            Console.Write("Nice! So what is your favourite food?");

            string favoriteFood = Console.ReadLine();

            Console.WriteLine("Oh! " + favoriteFood + " tasty! my favourite food is Electricity!");

            Console.WriteLine("So do you want to learn something new?");

            string learnNew1 = Console.ReadLine();

            switch (learnNew1)
            {
                case "Yes" or "yes" or "YES" or "yES" or "yeS" or "YeS" or "yEs":
                    Console.WriteLine("The strongest muscle in the body is the tongue.");
                    break;

                case "no" or "No" or "NO":
                    Console.WriteLine("Ok");
                    break;
            }
           
            Console.ReadKey();
        }
    }
}
 
The reason you are not getting any reply is because you do not explain the problem ("It doesn't work" is all but useless) and you do not give sufficient information(like: what kind of field is "OutputText"). Also, the source of your old Console program is totally irrelevant as well as too long, causing a TLDR reaction.
 
I'm also a bit confused in terms of what you want. But here's what I came up with (both textboxes have the multiline property set to true).....
Code:
using System;
using System.Windows.Forms;

namespace talktest
{

    public partial class Form1 : Form
    {
    public int ConvNum;

        public Form1()
        {
        InitializeComponent();
        }

        public void Form1_Load(object sender, EventArgs e)
        {
        ConvNum = 1;
        textBox1.Text = "Hi what is your name?";
        this.ActiveControl = textBox2;
        }

        private void button1_Click(object sender, EventArgs e)
        {
        string newline = "\r\n";

            if (ConvNum == 1)
            {
                if (textBox2.Text == "")
                {
                MessageBox.Show ("Whoops, you didn't enter your name!");
                ConvNum = ConvNum - 1;
                }
                else
                {
                textBox1.Text = "Hi " + textBox2.Text + "." + newline + "So what is your favorite activity?";
                }
            }
            if (ConvNum == 2)
            {
                if (textBox2.Text == "")
                {
                MessageBox.Show ("Please enter your favorite activity.");
                ConvNum = ConvNum - 1;
                }
                else
                {
                textBox1.Text = "That's a great activity." + newline + "Goodbye!";
                }
            }
        textBox2.Clear();
        this.ActiveControl = textBox2;
        ConvNum = ConvNum + 1;
        }
    }
}
 
I made a simple talking program when I just started learning c#. Now I am an average and know a bit about this language, and I decided to take my old program (Which was a console application) and made a new windows forms app with an Input field to Simulate Console.Readline and Output for WriteLine and a next button to move forward and submit what's there in the input field. I completed the code and made 2 lines 'Hi and What's your name' just for testing and while it should have worked, it didn't. Here's the code-
C#:
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 WinFormsApp1

{

    public partial class Form1 : Form

    {



        public bool SubmitClick;

        public int ConvNum;



        public Form1()

        {

            InitializeComponent();



            ConvNum = 1;



            if (ConvNum == 1)

            {

                OutputText.Text = "Hi";

            }



            if (ConvNum == 2)

            {

                OutputText.Text = "What is your name?";

            }

        }



        private void button1_MouseClick(object sender, MouseEventArgs e)

        {

            InputText.Text = " ";

            SubmitClick = true;

            ConvNum = ConvNum + 1;

        }

    }

}
If you expected to see the "What is your name?" question you are wrong. The constructor Form1() is called only once, no matter how many times you push that button. Either introduce a loop, or put your non-initial code inside the MouseClick trigger.
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom