user7644293
user7644293

Reputation:

Store text box value in a string array and display array in listbox

How to take a string value from a textbox and store it in an array string? Upon click of the summary button, it displays the array as a list in the list box.

For example, User enters "Tom" in the textbox. Hits Enter! Tom is stored in the array The user enters "Nick" in the textbox and Hits Enter! Nick is stored in the array and so on.

Finally, when the user hits the summary button, the List box displays something like:

Tom

Nick

Can someone help me with this? Much appreciated thank you!

This is my code so far

//Button helps to display the total number of customers
        private void viewCustomerSBtn_Click(object sender, EventArgs e) 
        {
            //Displays the string of names that are stored in cNames
            //Creates an array to hold Customer Names
            string[] cNames = new string[100];
            for (int i = 0; i < cNames.Length; i++)
            {  
                    cNames[i] = nameTextBox.Text;
                    reportListBox.Items.Add(cNames[i]);
        }

Upvotes: 0

Views: 2638

Answers (2)

Gabor
Gabor

Reputation: 3246

You did not specify what kind of app you are writing: WinForms, WPF, etc.

You also did not show your coding effort.

Without providing you a complete code here is a suggestion what to look for:

Usually a textbox has a Text property and also you can subscribe to the appropriate event of the textbox to catch when the user hits Enter. You can read the entered name through the Text property of the textbox and add it to a list e.g. List<string> then clear the textbox by setting the Text property to an empty string.

When the user clicks on the summary button then you can add the elements in list to the listbox through its Items property using its Add() method.

This is the direction I would go. You can google the rest.

UPDATE #1

Here is a working example:

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;

namespace CollectNames
{
    public partial class MainForm : Form
    {
        private static readonly List<string> names = new List<string>();

        public MainForm()
        {
            InitializeComponent();

            // Usually we set these event handlers using the 'Properties' tab for each specified control.
            // => Click on the control once then press F4 so that 'Properties' tab will appear.
            // Then these event subscriptions will be generated into MainForm.Designer.cs file.
            // They are here just for clarity.
            txtName.KeyUp += new System.Windows.Forms.KeyEventHandler(txtName_KeyUp);
            btnSummary.Click += new System.EventHandler(btnSummary_Click);
        }

        private void txtName_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                names.Add(txtName.Text);
                txtName.Text = String.Empty;

                e.Handled = true;
            }
        }

        private void btnSummary_Click(object sender, EventArgs e)
        {
            lstNames.Items.Clear();
            lstNames.Items.AddRange(names.Cast<object>().ToArray());
        }
    }
}

I have these controls:

  • Label: lblName
  • TextBox: txtName
  • Button: btnSummary
  • ListBox: lstNames

The two methods are the event handlers for the specified controls.

Here is the UI:

The UI

Upvotes: 1

Willy David Jr
Willy David Jr

Reputation: 9131

You may need static variables for this so that this will save all your inputs for display in the future:

Make sure you declare this as static variable:

public static List<string> lstInputs { get; set; } 

Then you can use KeyDown event handler of Textbox so that you can detect if the enter on keyboard has been pressed:

 private void textBox2_KeyDown(object sender, KeyEventArgs e)
    {
        if (lstInputs == null)
            lstInputs = new List<string>();
        if (e.KeyCode == Keys.Enter)
        {
            lstInputs.Add(textBox2.Text);
            textBox2.Text = string.Empty;
            MessageBox.Show("Message has been saved.");
        }
    }

Lastly you can use your for loop, to get all the message. I am using List here since List is dynamic and no need to declare what is the maximum size for this. But you can use normal string array if you wish to.

Upvotes: 0

Related Questions