Reputation: 51
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.
Upvotes: 5
Views: 6374
Reputation: 82
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 ShowAllSaveNameAndCountApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List<string> ourList=new List<string>();
string txt =" ";
private void buttonSave_Click(object sender, EventArgs e)
{
ourList.Add(textBoxEntryName.Text);
foreach (string s in ourList)
{
txt+= s+ "\n ";
}
textBoxEntryName.Clear();
ourList.Clear();
}
private void buttonShowAllName_Click(object sender, EventArgs e)
{
textBoxShowName.Text = txt;
}
}
}
Upvotes: 0
Reputation: 48114
Within the button1 click listener (if you don't have this hook go into the GUI builder view and double click on the button, it will automatically create and register the listener for you), add the following code;
textbox2.Text = textbox1.Text; // set tb2 = tb1
textbox1.Text = System.String.Empty; // clear tb1
Now, in your post you say store the data in a list, but you don't specify how the user is to input that data so it's hard to give you a specific answer. If the names are say separated by commas to get an array with all the names you could simply do;
string[] names = textbox1.Text.Split(',');
However, from your post it doesn't seem you want to store the data in a list at all. If you just want the input in textbox1
to be displayed in textbox2
upon clicking the input button then use use the first code snippet. If you go the second route, you'll have to convert the array back into a single string. This can be done easily with a for loop.
string result = System.String.Empty;
for (int i = 0; i < names.Length; i++)
result = result + names[i] + " ";
To make textbox2
display what's in textbox1
and display the number of names;
textbox2.Text = textbox1.Text; // set tb2 = tb1
string[] names = textbox1.Text.Split(','); // i use a comma but use whatever
// separates the names might just want ' ' for whitespace
textbox1.Text = System.String.Empty; // clear tb1
MessageBox.Show("You entered " + names.Count.ToString() + " names."); // show the names count
Upvotes: 0
Reputation: 188
This is very simple.
List<string> mylist=new List<string>();
mylist.Add(textbox1.Text);
textbox2.Text=mylist[mylist.Count - 1]
First you create a list of string objects. Then add the text from textbox1 to the end of the list. Then get the last element you added from the list by getting the length of the list and subtracting 1 since in C# collections are 0 based and the first element is [0] and not [1].
Upvotes: 0
Reputation: 11480
If your utilizing a traditional Windows Form Application; I'm not sure you meant to store the data in another Text Box. But a List Box may be more along your goal.
At this point Visual Studio will leave Designer View and go into Code View. So you'll be able to see the code. It will automatically place you in the Button code block.
These blocks are quite important, as you progress you'll notice how C# is structured.
private void button1_click(object sender, EventArgs e)
{
// Logic to add will go in here.
}
What does this mean?
What that entire block is, is an Event. So when it is clicked it will perform an action. That is what it means; so this is where your goal is implemented:
private void btnAddToList_Click(object sender, EventArgs e)
{
// Test to ensure it isn't null.
if(txtAddText.Text != String.EmptyOrNull)
{
// Declare a variable with the initial textbox value.
string txtVar = txtAddText.Text;
// Has the second textbox inherit value from other Textbox.
txtNewText = txtVar
// Now Add it to a Listbox.
lstContainer.Items.Add(txtAddText.Text + DateTime.Now());
}
else
{
// Null or Empty Character, Error Message.
MessageBox.Show("Invalid Entry, please enter a valid entry");
}
}
That will provide the fundamental knowledge, but as you can see from your other examples they do it differently. You'll notice that bugs can exist in such logic if you aren't careful. Which you'll learn to identify based on the structure you configure.
Hopefully this is helpful, and it looks like a lot of others did some terrific post for you as well.
Happy coding.
Upvotes: 1
Reputation: 8656
You could use something simple as this:
private void button1_Click_1(object sender, EventArgs e)
{
string[] names = textBox1.Text.Split(new string[] { " ", Environment.NewLine, "," }, StringSplitOptions.RemoveEmptyEntries);
//you can add more parameters for splitting the string
textBox2.Text = string.Join(",", names);
//you can replace the comma with something more suitable for you
}
The first line splits the string that you entered in the textBox1 (hence names separated by newlines, blank characters or commas) into array of string (instead of list that you requested) and the second line joins the strings into one big string of names separated by commas and puts it into the textBox2
Upvotes: 0
Reputation: 3385
Something like that ?
string name = Textbox1.Text;
ListBox1.Add(name);
Upvotes: 1
Reputation: 31204
assuming winforms...
button1_Click
event add the text of textbox1
to the listhere 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;
}
}
}
Upvotes: 2