简体   繁体   中英

How do I use buttons and text boxes in a winform application in C#?

I'm trying to make my console application into a winforms application.

The console version would ask for multiple inputs, then return output.

For the winforms version, I want to have multiple text boxes for the user to enter the data, with a button to reset them all, and another to actually perform the calculation. Then there'd be a display to show the result (I'm guessing it would be another text box).

Basically I've figured out how to add the buttons and text boxes for input/calculation, but I'm not sure how to display the output, or add functionality to all of this.

Edit: Should I be using Masked Text Boxes if I want to keep the user from using invalid input (anything that isn't a positive integer)?

Start by looking here: http://www.codeproject.com/KB/books/1861004982.aspx , it's a little old but still relevant.

Also have a look on the official Microsoft site: http://windowsclient.net/learn/videos.aspx

GUI development is tricky, fun and exciting, all at the same time!

EDIT: I should point out I'm linking to things because at this point, being able to learn a new technology (such as WinForms) by yourself, from documentation and the internet, is invaluable to your future skills development.

如何添加另一个只读文本框以显示输出?

 public partial class Form2 : Form
    {
        Button reset = new Button();
        Button compute = new Button();
        Panel pnl = new Panel();

        public Form2()
        {

            reset.Text = "reset";
            compute.Text = "compute";
            pnl.Name = "pnl";

            reset.Click += new EventHandler(reset_Click);
            compute.Click += new EventHandler(compute_Click); 

            this.Controls.Add(compute);
            this.Controls.Add(reset);
            this.Controls.Add(pnl);

            init();

            foreach (Control ctl in this.Controls)
            {
                ctl.Dock = DockStyle.Top;
            }


        }

        void compute_Click(object sender, EventArgs e)
        {
            int tot=0;
            foreach (TextBox txt in pnl.Controls)
            {
                tot += int.Parse(txt.Text);
            }

            MessageBox.Show("total is:" + tot.ToString());
        }

        void reset_Click(object sender, EventArgs e)
        {
            foreach (TextBox txt in pnl.Controls)
            {
                txt.Text = "0";
            }
        }

        private void init()
        {   
            pnl.Controls.Clear();

            //5 textbox
            for (int i = 0; i <= 5; i++)
            {
                TextBox t = new TextBox();
                t.Dock = DockStyle.Top;
                t.Text = "0";
                this.Controls["pnl"].Controls.Add(t);                
            }
        }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM