Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, November 15, 2016 2:34 PM
Hi
I'm new to c# and all I can find to slowed this problem is in console but I need the user to input a name in a textbox and for that name to end up in a string array. So my question is how do I add the user input into a string array in Windows form application?
All replies (2)
Tuesday, November 15, 2016 2:45 PM | 1 vote
Hi,
See the below code. Add a button and textbox in your form. Hope this helps you.
using System;
using System.Collections;
using System.Windows.Forms;
namespace ArrayListt
{
public partial class Form1 : Form
{
ArrayList list = new ArrayList();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
list.Add(textBox1.Text);
}
}
}
Thanks,
Sabah Shariq
[If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]
Tuesday, November 15, 2016 8:19 PM | 2 votes
You generally want to use a List<string> over a string[] whenever you intend to add or remove data from the array dynamically and you don't know the number of items upfront. Please refer to the following thread at Stackoverflow for more information about this: http://stackoverflow.com/questions/434761/array-versus-listt-when-to-use-which
Adding a string to a List<string> is as simple as calling the Add method:
private readonly List<string> theStrings = new List<string>();
private void button1_Click(object sender, EventArgs e)
{
theStrings.Add(textBox1.Text);
}
The difference between a List<string> and an ArrayList is that the former is generic and stores values of a specific type (like string) without having to cast the items to or from object. This is good for performance. Please refer to the following thread for more information about this: http://stackoverflow.com/questions/2309694/arraylist-vs-list-in-c-sharp
Hope that helps.
Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread