6 Replies - 639 Views - Last Post: 24 November 2013 - 08:45 AM Rate Topic: -----

#1 pantomine  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 22-November 13

How to add textboxes fields into array

Posted 23 November 2013 - 10:17 AM

I have found some related threads for this, but didn't find anything what I was particularly looking for.

Here's my form for windows app


Attached Image

Here's the code which I referred to from Stackoverflow

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace ProductRecord
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            string newFileName = "D:\\productrecord.csv";

            string productDetails = txtItemId.Text + "," + txtItemName.Text;

            

            if (!File.Exists(newFileName))
            {
                string productHeader = "Product Id" + "........." + "Product Name";

                File.WriteAllText(newFileName, productHeader);

                MessageBox.Show("Item added!");
            }

            File.AppendAllText(newFileName, productDetails);


        }
    }
}



I would like to save the inputs from textboxes to csv file. For now, it's not writing csv file and not even popping Message "Item added!" on clicking Add button.

Any sort of help would be appreciated. Thanks!

This post has been edited by tlhIn`toq: 23 November 2013 - 10:52 AM
Reason for edit:: CHanged image to attachment due to loading problems


Is This A Good Question/Topic? 0
  • +

Replies To: How to add textboxes fields into array

#2 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3720
  • View blog
  • Posts: 13,006
  • Joined: 12-December 12

Re: How to add textboxes fields into array

Posted 23 November 2013 - 10:32 AM

I copied and pasted that code and was able to run it successfully.

I had to change to a folder on my C: drive, and associate the click event with a button on the form: from the Properties Window, Events list, find the Click event and select btnAdd_Click from the drop-down list.
Was This Post Helpful? 3
  • +
  • -

#3 pantomine  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 22-November 13

Re: How to add textboxes fields into array

Posted 23 November 2013 - 12:57 PM

Had a bit problem with the path which I have managed to fix. Thanks for your valuable inputs!
Was This Post Helpful? 0
  • +
  • -

#4 pantomine  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 22-November 13

Re: How to add textboxes fields into array

Posted 24 November 2013 - 04:43 AM

Sorry to bother you guys again.

How do I put these textboxes values; ItemId and Item Name into array and save into csv file when Add button is clicked.

Is this the right way of declaring array called product with two fields; Item Id and Item Name?
string[] product = new string[] {txtItemId.Text, txtItemName.Text};



Thanks in advance!
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3720
  • View blog
  • Posts: 13,006
  • Joined: 12-December 12

Re: How to add textboxes fields into array

Posted 24 November 2013 - 05:13 AM

You can simplify that declaration with:

string[] product = { "hello", "bob" };

but an array doesn't have fields. This just creates an array with two elements.

If you will be inserting the two text values into the csv each time the button is clicked then why would you need an array?

Alternatively, if you intend to keep these ids and names - that is, to store a number of them - then you could create a Class with Id and Name as fields, and a List to store the instances of this Class that you create. A List would be preferable to an array as you will be dynamically creating and adding-to this List (and you don't know how many instances you may create).

BTW Don't name your Class 'Item'; this is a term that is used throughout C#, and it is not very descriptive anyway. 'Product' would be better.

Hint If you override the ToString() method in your Class then this will assist you when writing the details of a Product to the csv.

This post has been edited by andrewsw: 24 November 2013 - 05:19 AM

Was This Post Helpful? 1
  • +
  • -

#6 pantomine  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 22-November 13

Re: How to add textboxes fields into array

Posted 24 November 2013 - 08:37 AM

Thanks for the help, andrewsw. This is probably what I am looking for.

Quote

Alternatively, if you intend to keep these ids and names - that is, to store a number of them - then you could create a Class with Id and Name as fields, and a List to store the instances of this Class that you create. A List would be preferable to an array as you will be dynamically creating and adding-to this List (and you don't know how many instances you may create).

BTW Don't name your Class 'Item'; this is a term that is used throughout C#, and it is not very descriptive anyway. 'Product' would be better.

Hint If you override the ToString() method in your Class then this will assist you when writing the details of a Product to the csv.


Here's my Product class

class product
{
  public int productId { get; set;}
  public string productName { get; set; }

}



how to assign textboxes' values to the product class's properties?
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3720
  • View blog
  • Posts: 13,006
  • Joined: 12-December 12

Re: How to add textboxes fields into array

Posted 24 November 2013 - 08:45 AM

Class-names should begin with a capital letter (PascalCase).

You first need to create an instance of your Class.

Product prod = new Product();

http://msdn.microsof...o/x9afc042.aspx

This post has been edited by andrewsw: 24 November 2013 - 08:46 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1