Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

User Input from textbox1 > store in a list > output in textbox2

Tags:

c#

I just have a question regarding C# list. I am a totally noob when it comes to programming and I'm really sorry for being a bird brainer. I am doing some practice coding and I am creating a simple program that will allow users to input names through textbox1 and then once they press the button1, the names will be stored in a List and will be output on textbox2.

I am having hard time storing the data from textbox1. Checked it online but I haven't found the right article for my concern so I'm trying my luck here.

Sorry guys, I forgot to mention I am using Winforms.

Thank you so much for the fast replies.

like image 513
user2117433 Avatar asked Oct 05 '22 21:10

user2117433


1 Answers

assuming winforms...

  • Drag and drop 2 lists and a button onto your designer.
  • drag a button onto your designer
  • double-click your button to automatically create an event
  • make a list structure somewhere inside your form to store the list
  • instantiate your list in the form constructor
  • in the button1_Click event add the text of textbox1 to the list
  • generate the text of 1textbox2`

here is an example

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 WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            list = new List<string>();
        }

        List<string> list;

        private void button1_Click(object sender, EventArgs e)
        {
            list.Add(textBox1.Text);


            string txt = "";
            foreach(string s in list)
            {
                txt += s + " ";
            }
            textBox2.Text = txt;
        }
    }
}
like image 142
Sam I am says Reinstate Monica Avatar answered Oct 16 '22 16:10

Sam I am says Reinstate Monica