• 0

[C#] Writing data to a .dat file


Question

Hi!

I have a window application.

User makes some selection from different combo boxes, then when hitted the "ADD TO CART" button, the data selected will be copied to some labels next to it.

But now, i need to transfer/copy all that data from the labels to a file named: computer.dat

How do i do that? I'm really having problems with that.

I have this there:

void storeInFile(Part *d)
  	{
    FileStream file = new FileStream("computer.dat", FileMode.OpenOrCreate, FileAccess.Read);

    for (int i=0; i<d->dataCount; i++)
    {
    	file << d->pb[i].processor <<"\n\n";
    	file << d->pb[i].memory <<"\n"<< endl; 
    	file << d->pb[i].harddrive <<"\n"<< endl; 
    	file << d->pb[i].cdrom <<"\n"<< endl;
    	file << d->pb[i].floppydrive <<"\n"<< endl; 
    	file << d->pb[i].keyboard <<"\n"<< endl; 
    	file << d->pb[i].monitor <<"\n"<< endl; 
    	file << d->pb[i].mouse <<"\n"<< endl; 
    }
    file.Close();	// close computer.dat
  	}

but im not sure if that's correct.

Please help me..:)

Thanks,

Link to comment
https://www.neowin.net/forum/topic/376679-c-writing-data-to-a-dat-file/
Share on other sites

Recommended Posts

  • 0
  marie said:
Thanks...

but... where's the .dat file? it isn't appearing... :no:

586579242[/snapback]

Look at your code, the "storeInFile()" is not being called.

You want it to work when you hit the "add to cart" button right?

so in your

private void button1_Click(object sender, System.EventArgs e)
        {
            label12.Text = textBox1.Text += textBox2.Text += ", thanks for selecting us as your preferred brand!";
            label4.Text = comboBox1.Text;
            label5.Text = comboBox2.Text;
            label6.Text = comboBox3.Text;
            label7.Text = comboBox4.Text;
            label8.Text = comboBox5.Text;
            label9.Text = comboBox6.Text;
            label10.Text = comboBox7.Text;
            label11.Text = comboBox8.Text;


            !!!!!!CALL "storeInFile()" HERE!!!!! ( this is not real code :P  )

but there is a cleaner way to do this. Copy the code for storeInFile() and put it into the "private void button1_Click(object sender, System.EventArgs e)" code above ( in the spot where i have !!!!!!CALL "storeInFile()" HERE!!!!!) and you dont have to worry about that owner form1 mess but its all diffrent ways of doing the same thing.

so....

private void button1_Click(object sender, System.EventArgs e)
        {
            label12.Text = textBox1.Text += textBox2.Text += ", thanks for selecting us as your preferred brand!";
            label4.Text = comboBox1.Text;
            label5.Text = comboBox2.Text;
            label6.Text = comboBox3.Text;
            label7.Text = comboBox4.Text;
            label8.Text = comboBox5.Text;
            label9.Text = comboBox6.Text;
            label10.Text = comboBox7.Text;
            label11.Text = comboBox8.Text;

            // create a file stream, for "computer.dat"
            FileStream fs = new FileStream("computer.dat", FileMode.CreateNew, FileAccess.ReadWrite);
            // create a stream writer
            StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.ASCII);

            //write to file (buffer), where textBox1 is your text box

            sw.Write(label4.Text.ToString() + Environment.NewLine);
            sw.Write(label5.Text.ToString() + Environment.NewLine);
            sw.Write(label6.Text.ToString() + Environment.NewLine);
            sw.Write(label7.Text.ToString() + Environment.NewLine);
            sw.Write(label8.Text.ToString() + Environment.NewLine);
            sw.Write(label9.Text.ToString() + Environment.NewLine);
            sw.Write(label10.Text.ToString() + Environment.NewLine);
            sw.Write(label11.Text.ToString() + Environment.NewLine);

            // flush buffer (so the text really goes into the file)
            sw.Flush();
            // close stream writer and file
            sw.Close();
            fs.Close();


        } // end button1_Click

Its done and works .dat file and all. :)

I need to get back into programing, but all i play with is the windows media SDK in C#

  • 0
  marie said:
Call me stupid but i CANT see the .dat file.

Oh my godness im feeling so dumb now!!! :wacko:

586579389[/snapback]

? should be in the same directory with your .exe that is created

but here is your code with the change

Edited by dolimite35
  • 0
  marie said:
Call me stupid but i CANT see the .dat file.

Oh my godness im feeling so dumb now!!! :wacko:

586579389[/snapback]

I don't understand what you aren't seeing.

FileStream fs = new FileStream("computer.dat", FileMode.CreateNew, FileAccess.ReadWrite);

^^ That makes the object that will write the file.

fs.Close() will write it.

Also, lose the Flush!

  • 0
  marie said:
is there a way of calculating the total price?

586579451[/snapback]

hmm so many ways.... since your combox selections are strings as are your labels, you can try a search of the string. Like for each label:

label1.TEXT = "Intel? Pentium? 4 Processor (3GHz, 800 FSB) [add $50]"

do a search in the string (label.TEXT) for the "$". Then get the number with a search from the "$" to the "]"

example "$"50"]" would be like

aString[n] = "$" (start)

aString[n+1] = "5"

aString[n+2] = "0"

aString[n+3] = "]" (end)

just get the 50 and convert to an int and add them up. THere is a method to do that in the String class.

Im sure there is a better way maybe someone can comeup with it

If you still need help i can give it a try. Im liking this and im scared that i am :blink:

  • 0
  dolimite35 said:
hmm so many ways.... since your combox selections are strings as are your labels, you can try a search of the string. Like for each label:

label1.TEXT =  "Intel? Pentium? 4 Processor (3GHz, 800 FSB) [add $50]"

do a search in the string (label.TEXT) for the "$". Then get the number with a search from the "$" to the "]"

example "$"50"]" would be like

aString[n] = "$"  (start)

aString[n+1] = "5"

aString[n+2] = "0"

aString[n+3] = "]" (end)

just get the 50 and convert to an int and add them up. THere is a method to do that in the String class.

Im sure there is a better way maybe someone can comeup with it

If you still need help i can give it a try. Im liking this and im scared that i am  :blink:

586579520[/snapback]

Okay, so got a little confunsed in here.

*hahaha* :rofl: :D - why are you scared that you are liking this?

In which part i should write what you explained up there?

PLEASE, sorry for all the inconvinience.

  amrinders87 said:
Close() calls Flush(), but it does not call Dispose(). Dispose() calls Clos().

586579529[/snapback]

  • 0
  dolimite35 said:
hmm so many ways.... since your combox selections are strings as are your labels, you can try a search of the string. Like for each label:

label1:  Intel? Pentium? 4 Processor (3GHz, 800 FSB) [add $50]

do a search in the string (label.TEXT) for the "$". Then get the number with a search from the "$" to the "]"

example "$"50"]" would be like

aString[n] = "$"  (start)

aString[n+1] = "5"

aString[n+2] = "0"

aString[n+3] = "]" (end)

just get the 50 and convert to an int and add them up. THere is a method to do that in the String class.

Im sure there is a better way maybe someone can comeup with it

If you still need help i can give it a try. Im liking this and im scared that i am  :blink:

586579520[/snapback]

<gag>

Fortunetly, you don't need to. When you populate the comboboxes, just use your items that derive from part. Override ToString() to get their text, like so:

        private void PopulateBox()
        {
            //Populate the box.
            comboBox1.Items.Add(new ItemForTheComboBox("Item1", 1));
            comboBox1.Items.Add(new ItemForTheComboBox("Item2", 2));
        }
        class ItemForTheComboBox
        {
            private decimal price;
            public decimal Price
            {
                get
                {
                    return price;
                }
            }
            private string name;
            public override string ToString()
            {
                return name;
            }
            public ItemForTheComboBox(string Item, decimal Price)
            {
                name = Item;
                price = Price;
            }
        }
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            MessageBox.Show((comboBox1.SelectedItem as ItemForTheComboBox).Price.ToString());
        }

  • 0
  Dayon said:
<gag>

Fortunetly, you don't need to. When you populate the comboboxes, just use your items that derive from part. Override ToString() to get their text, like so:

 ? ? ? ?private void PopulateBox()
 ? ? ? ?{
 ? ? ? ? ? ?//Populate the box.
 ? ? ? ? ? ?comboBox1.Items.Add(new ItemForTheComboBox("Item1", 1));
 ? ? ? ? ? ?comboBox1.Items.Add(new ItemForTheComboBox("Item2", 2));
 ? ? ? ?}
 ? ? ? ?class ItemForTheComboBox
 ? ? ? ?{
 ? ? ? ? ? ?private decimal price;
 ? ? ? ? ? ?public decimal Price
 ? ? ? ? ? ?{
 ? ? ? ? ? ? ? ?get
 ? ? ? ? ? ? ? ?{
 ? ? ? ? ? ? ? ? ? ?return price;
 ? ? ? ? ? ? ? ?}
 ? ? ? ? ? ?}
 ? ? ? ? ? ?private string name;
 ? ? ? ? ? ?public override string ToString()
 ? ? ? ? ? ?{
 ? ? ? ? ? ? ? ?return name;
 ? ? ? ? ? ?}
 ? ? ? ? ? ?public ItemForTheComboBox(string Item, decimal Price)
 ? ? ? ? ? ?{
 ? ? ? ? ? ? ? ?name = Item;
 ? ? ? ? ? ? ? ?price = Price;
 ? ? ? ? ? ?}
 ? ? ? ?}
 ? ? ? ?private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
 ? ? ? ?{
 ? ? ? ? ? ?MessageBox.Show((comboBox1.SelectedItem as ItemForTheComboBox).Price.ToString());
 ? ? ? ?}

586579566[/snapback]

I didnt see the "Part" class but it doesnt look like he is using it at all in the code

  • 0

Here's something more suited to your program:

        abstract public class Part
        {
            internal decimal price;
            internal string name;
            internal Form1 owner;
            public decimal Price
            {
                get
                {
                    return price;
                }
            }
            public Part(Form1 Owner, string Name, decimal Price)
            {
                owner = Owner;
                name = Name;
                price = Price;
            }
            public override string ToString()
            {
                return name + " [$" + Math.Round(price, 2).ToString() + "]";
            }
        }

        public class Memory : Part
        {
            public Memory(Form1 Owner, string Name, decimal Price) : base(Owner, Name, Price)
            {
            }
        }
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            MessageBox.Show((comboBox1.SelectedItem as Part).Price.ToString());
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.Items.Add(new Memory(this, "512mb DDR", 50));
            comboBox1.Items.Add(new Memory(this, "1024mb DDR", 100));
        }

  • 0

I really got lost there.

Im posting my code again:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;



namespace WindowsApplication1
{
	/// &lt;summary&gt;
	/// Summary description for Form1.
	/// &lt;/summary&gt;
	public class Form1 : System.Windows.Forms.Form
	{
  private System.Windows.Forms.ComboBox comboBox1;
  private System.Windows.Forms.ComboBox comboBox2;
  private System.Windows.Forms.ComboBox comboBox3;
  private System.Windows.Forms.ComboBox comboBox4;
  private System.Windows.Forms.ComboBox comboBox5;
  private System.Windows.Forms.ComboBox comboBox6;
  private System.Windows.Forms.ComboBox comboBox7;
  private System.Windows.Forms.ComboBox comboBox8;
  private System.Windows.Forms.Label label1;
  private System.Windows.Forms.Label label2;
  private System.Windows.Forms.TextBox textBox1;
  private System.Windows.Forms.Label label3;
  private System.Windows.Forms.TextBox textBox2;
  private System.Windows.Forms.PictureBox pictureBox1;
  private System.Windows.Forms.Button button1;
  private System.Windows.Forms.GroupBox groupBox1;
  private System.Windows.Forms.Label label4;
  private System.Windows.Forms.Label label5;
  private System.Windows.Forms.Label label6;
  private System.Windows.Forms.Label label7;
  private System.Windows.Forms.Label label8;
  private System.Windows.Forms.Label label9;
  private System.Windows.Forms.Label label10;
  private System.Windows.Forms.Label label11;
  private System.Windows.Forms.Label label12;
  private System.Windows.Forms.Label label13;
  private System.Windows.Forms.TextBox textBox3;
  /// &lt;summary&gt;
  /// Required designer variable.
  /// &lt;/summary&gt;
  private System.ComponentModel.Container components = null;


  public Form1()
  {
  	//
  	// Required for Windows Form Designer support
  	//
  	InitializeComponent();

  	//
  	// TODO: Add any constructor code after InitializeComponent call
  	//
  }

  /// &lt;summary&gt;
  /// Clean up any resources being used.
  /// &lt;/summary&gt;
  protected override void Dispose( bool disposing )
  {
  	if( disposing )
  	{
    if (components != null) 
    {
    	components.Dispose();
    }
  	}
  	base.Dispose( disposing );
  }

  #region Windows Form Designer generated code
  /// &lt;summary&gt;
  /// Required method for Designer support - do not modify
  /// the contents of this method with the code editor.
  /// &lt;/summary&gt;
  private void InitializeComponent()
  {
  	System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
  	this.comboBox1 = new System.Windows.Forms.ComboBox();
  	this.comboBox2 = new System.Windows.Forms.ComboBox();
  	this.comboBox3 = new System.Windows.Forms.ComboBox();
  	this.comboBox4 = new System.Windows.Forms.ComboBox();
  	this.comboBox5 = new System.Windows.Forms.ComboBox();
  	this.comboBox6 = new System.Windows.Forms.ComboBox();
  	this.comboBox7 = new System.Windows.Forms.ComboBox();
  	this.comboBox8 = new System.Windows.Forms.ComboBox();
  	this.label1 = new System.Windows.Forms.Label();
  	this.label2 = new System.Windows.Forms.Label();
  	this.textBox1 = new System.Windows.Forms.TextBox();
  	this.label3 = new System.Windows.Forms.Label();
  	this.textBox2 = new System.Windows.Forms.TextBox();
  	this.pictureBox1 = new System.Windows.Forms.PictureBox();
  	this.button1 = new System.Windows.Forms.Button();
  	this.groupBox1 = new System.Windows.Forms.GroupBox();
  	this.textBox3 = new System.Windows.Forms.TextBox();
  	this.label13 = new System.Windows.Forms.Label();
  	this.label12 = new System.Windows.Forms.Label();
  	this.label11 = new System.Windows.Forms.Label();
  	this.label10 = new System.Windows.Forms.Label();
  	this.label9 = new System.Windows.Forms.Label();
  	this.label8 = new System.Windows.Forms.Label();
  	this.label7 = new System.Windows.Forms.Label();
  	this.label6 = new System.Windows.Forms.Label();
  	this.label5 = new System.Windows.Forms.Label();
  	this.label4 = new System.Windows.Forms.Label();
  	this.groupBox1.SuspendLayout();
  	this.SuspendLayout();
  	// 
  	// comboBox1
  	// 
  	this.comboBox1.DropDownWidth = 320;
  	this.comboBox1.Items.AddRange(new object[] {
                 "Intel? Pentium? 4 Processor (2.80GHz, 533 FSB) [add $30]",
                 "Intel? Pentium? 4 Processor (3GHz, 800 FSB) [add $50]",
                 "Intel? Pentium? D 820 (2.8GHz, 800MHz FSB) [add $150]",
                 "Intel? Pentium? D 840 (3.2GHz, 800MHz FSB) [add $200]"});
  	this.comboBox1.Location = new System.Drawing.Point(24, 144);
  	this.comboBox1.Name = "comboBox1";
  	this.comboBox1.Size = new System.Drawing.Size(320, 21);
  	this.comboBox1.TabIndex = 0;
  	this.comboBox1.Text = "Select Processor Desired";
  	// 
  	// comboBox2
  	// 
  	this.comboBox2.DropDownWidth = 300;
  	this.comboBox2.Items.AddRange(new object[] {
                 "512MB Dual Channel DDR SDRAM at 400MHz [add $39]",
                 "1GB Dual Channel DDR SDRAM at 533MHz [add $60]",
                 "2GB Dual Channel DDR2 SDRAM at 533MHz [add $220]",
                 "4GB Dual Channel DDR2 SDRAM at 533MHz [add $500]"});
  	this.comboBox2.Location = new System.Drawing.Point(24, 192);
  	this.comboBox2.Name = "comboBox2";
  	this.comboBox2.Size = new System.Drawing.Size(320, 21);
  	this.comboBox2.TabIndex = 1;
  	this.comboBox2.Text = "Select Memory Desired";
  	// 
  	// comboBox3
  	// 
  	this.comboBox3.DropDownWidth = 250;
  	this.comboBox3.Items.AddRange(new object[] {
                 "80GB Serial ATA (7200rpm) (80S) [add $39]",
                 "160GB Serial ATA (7200rpm) (160S) [add $49]",
                 "250GB Serial ATA (7200rpm) (250S) [add $100]",
                 "400GB Serial ATA (7200rpm) (400S) [add $310]"});
  	this.comboBox3.Location = new System.Drawing.Point(24, 248);
  	this.comboBox3.Name = "comboBox3";
  	this.comboBox3.Size = new System.Drawing.Size(320, 21);
  	this.comboBox3.TabIndex = 2;
  	this.comboBox3.Text = "Select Hard Drive Desired";
  	// 
  	// comboBox4
  	// 
  	this.comboBox4.DropDownWidth = 325;
  	this.comboBox4.Items.AddRange(new object[] {
                 "16X CD/DVD burner (DVD+/-RW) [add $20]",
                 "16x DVD-ROM Drive + 16x DVD+/-RW  [add $30]",
                 "24X CD-RW / DVD Combo Drive [add $40]",
                 "8X CD/DVD burner (DVD+/-RW) [add $50]"});
  	this.comboBox4.Location = new System.Drawing.Point(24, 304);
  	this.comboBox4.Name = "comboBox4";
  	this.comboBox4.Size = new System.Drawing.Size(320, 21);
  	this.comboBox4.TabIndex = 3;
  	this.comboBox4.Text = "Select CD-Rom Desired";
  	// 
  	// comboBox5
  	// 
  	this.comboBox5.DropDownWidth = 200;
  	this.comboBox5.Items.AddRange(new object[] {
                 "3.5 inches Floppy Drive 1.44MB [add $30]",
                 "None [add $0]"});
  	this.comboBox5.Location = new System.Drawing.Point(24, 360);
  	this.comboBox5.MaxDropDownItems = 5;
  	this.comboBox5.Name = "comboBox5";
  	this.comboBox5.Size = new System.Drawing.Size(320, 21);
  	this.comboBox5.TabIndex = 4;
  	this.comboBox5.Text = "Select Floppy Drive Desired";
  	// 
  	// comboBox6
  	// 
  	this.comboBox6.Items.AddRange(new object[] {
                 "USB Keyboard  [add $20] ",
                 "Wireless Keyboard [add $50]"});
  	this.comboBox6.Location = new System.Drawing.Point(24, 416);
  	this.comboBox6.Name = "comboBox6";
  	this.comboBox6.Size = new System.Drawing.Size(320, 21);
  	this.comboBox6.TabIndex = 5;
  	this.comboBox6.Text = "Select Keyboard Desired";
  	// 
  	// comboBox7
  	// 
  	this.comboBox7.DropDownWidth = 245;
  	this.comboBox7.Items.AddRange(new object[] {
                 "15 inch E153FP Analog Flat Panel [add $100] ",
                 "17 inch E173FP Analog Flat Panel [add $160]",
                 "20 inch UltraSharp? Widescreen Digital Flat Panel [add $270]",
                 "No Monitor [add $0]"});
  	this.comboBox7.Location = new System.Drawing.Point(24, 472);
  	this.comboBox7.Name = "comboBox7";
  	this.comboBox7.Size = new System.Drawing.Size(320, 21);
  	this.comboBox7.TabIndex = 6;
  	this.comboBox7.Text = "Select Monitor Desired";
  	// 
  	// comboBox8
  	// 
  	this.comboBox8.DropDownWidth = 250;
  	this.comboBox8.Items.AddRange(new object[] {
                 "Dell? 2-button USB mouse [add $19] ",
                 "Dell Optical USB Mouse [add $29]",
                 "Logitech? MX? 518 Optical Mouse [add $50]",
                 "No Mouse [add $0]"});
  	this.comboBox8.Location = new System.Drawing.Point(24, 528);
  	this.comboBox8.Name = "comboBox8";
  	this.comboBox8.Size = new System.Drawing.Size(320, 21);
  	this.comboBox8.TabIndex = 7;
  	this.comboBox8.Text = "Select Mouse Desired";
  	// 
  	// label1
  	// 
  	this.label1.Font = new System.Drawing.Font("Edwardianpt ITC", 25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
  	this.label1.Location = new System.Drawing.Point(184, 8);
  	this.label1.Name = "label1";
  	this.label1.Size = new System.Drawing.Size(288, 48);
  	this.label1.TabIndex = 8;
  	this.label1.Text = "Customize Your Computer";
  	// 
  	// label2
  	// 
  	this.label2.Font = new System.Drawing.Font("Microsoft Serif", 8.6F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
  	this.label2.Location = new System.Drawing.Point(88, 88);
  	this.label2.Name = "label2";
  	this.label2.Size = new System.Drawing.Size(72, 16);
  	this.label2.TabIndex = 9;
  	this.label2.Text = "First Name";
  	// 
  	// textBox1
  	// 
  	this.textBox1.AcceptsTab = true;
  	this.textBox1.Location = new System.Drawing.Point(168, 88);
  	this.textBox1.Name = "textBox1";
  	this.textBox1.Size = new System.Drawing.Size(120, 20);
  	this.textBox1.TabIndex = 10;
  	this.textBox1.Text = "";
  	// 
  	// label3
  	// 
  	this.label3.Font = new System.Drawing.Font("Microsoft Serif", 8.6F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
  	this.label3.Location = new System.Drawing.Point(376, 88);
  	this.label3.Name = "label3";
  	this.label3.Size = new System.Drawing.Size(72, 16);
  	this.label3.TabIndex = 11;
  	this.label3.Text = "Last Name";
  	// 
  	// textBox2
  	// 
  	this.textBox2.AcceptsTab = true;
  	this.textBox2.Location = new System.Drawing.Point(448, 88);
  	this.textBox2.Name = "textBox2";
  	this.textBox2.Size = new System.Drawing.Size(128, 20);
  	this.textBox2.TabIndex = 12;
  	this.textBox2.Text = "";
  	// 
  	// pictureBox1
  	// 
  	this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image
  	this.pictureBox1.Location = new System.Drawing.Point(512, 8);
  	this.pictureBox1.Name = "pictureBox1";
  	this.pictureBox1.Size = new System.Drawing.Size(64, 64);
  	this.pictureBox1.TabIndex = 14;
  	this.pictureBox1.TabStop = false;
  	// 
  	// button1
  	// 
  	this.button1.FlatStyle = System.Windows.Forms.FlatStyle.System;
  	this.button1.ImageAlign = System.Drawing.ContentAlignment.TopCenter;
  	this.button1.Location = new System.Drawing.Point(144, 568);
  	this.button1.Name = "button1";
  	this.button1.Size = new System.Drawing.Size(80, 24);
  	this.button1.TabIndex = 16;
  	this.button1.Text = "Add To Cart";
  	this.button1.Click += new System.EventHandler(this.button1_Click);
  	// 
  	// groupBox1
  	// 
  	this.groupBox1.Controls.Add(this.textBox3);
  	this.groupBox1.Controls.Add(this.label13);
  	this.groupBox1.Controls.Add(this.label12);
  	this.groupBox1.Controls.Add(this.label11);
  	this.groupBox1.Controls.Add(this.label10);
  	this.groupBox1.Controls.Add(this.label9);
  	this.groupBox1.Controls.Add(this.label8);
  	this.groupBox1.Controls.Add(this.label7);
  	this.groupBox1.Controls.Add(this.label6);
  	this.groupBox1.Controls.Add(this.label5);
  	this.groupBox1.Controls.Add(this.label4);
  	this.groupBox1.Location = new System.Drawing.Point(368, 136);
  	this.groupBox1.Name = "groupBox1";
  	this.groupBox1.Size = new System.Drawing.Size(320, 456);
  	this.groupBox1.TabIndex = 17;
  	this.groupBox1.TabStop = false;
  	this.groupBox1.Text = "Items on your Cart";
  	// 
  	// textBox3
  	// 
  	this.textBox3.Location = new System.Drawing.Point(208, 424);
  	this.textBox3.Name = "textBox3";
  	this.textBox3.TabIndex = 10;
  	this.textBox3.Text = "$";
  	// 
  	// label13
  	// 
  	this.label13.Location = new System.Drawing.Point(168, 432);
  	this.label13.Name = "label13";
  	this.label13.Size = new System.Drawing.Size(40, 16);
  	this.label13.TabIndex = 9;
  	this.label13.Text = "Total";
  	// 
  	// label12
  	// 
  	this.label12.Font = new System.Drawing.Font("Microsoft Serif", 8.9F, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
  	this.label12.Location = new System.Drawing.Point(16, 16);
  	this.label12.Name = "label12";
  	this.label12.Size = new System.Drawing.Size(296, 32);
  	this.label12.TabIndex = 8;
  	// 
  	// label11
  	// 
  	this.label11.Location = new System.Drawing.Point(16, 392);
  	this.label11.Name = "label11";
  	this.label11.Size = new System.Drawing.Size(288, 24);
  	this.label11.TabIndex = 7;
  	// 
  	// label10
  	// 
  	this.label10.Location = new System.Drawing.Point(16, 344);
  	this.label10.Name = "label10";
  	this.label10.Size = new System.Drawing.Size(288, 32);
  	this.label10.TabIndex = 6;
  	// 
  	// label9
  	// 
  	this.label9.Location = new System.Drawing.Point(16, 304);
  	this.label9.Name = "label9";
  	this.label9.Size = new System.Drawing.Size(288, 24);
  	this.label9.TabIndex = 5;
  	// 
  	// label8
  	// 
  	this.label8.Location = new System.Drawing.Point(16, 264);
  	this.label8.Name = "label8";
  	this.label8.Size = new System.Drawing.Size(288, 24);
  	this.label8.TabIndex = 4;
  	// 
  	// label7
  	// 
  	this.label7.Location = new System.Drawing.Point(16, 216);
  	this.label7.Name = "label7";
  	this.label7.Size = new System.Drawing.Size(288, 24);
  	this.label7.TabIndex = 3;
  	// 
  	// label6
  	// 
  	this.label6.Location = new System.Drawing.Point(16, 168);
  	this.label6.Name = "label6";
  	this.label6.Size = new System.Drawing.Size(288, 32);
  	this.label6.TabIndex = 2;
  	// 
  	// label5
  	// 
  	this.label5.Location = new System.Drawing.Point(16, 112);
  	this.label5.Name = "label5";
  	this.label5.Size = new System.Drawing.Size(288, 40);
  	this.label5.TabIndex = 1;
  	// 
  	// label4
  	// 
  	this.label4.Location = new System.Drawing.Point(16, 64);
  	this.label4.Name = "label4";
  	this.label4.Size = new System.Drawing.Size(288, 32);
  	this.label4.TabIndex = 0;
  	// 
  	// Form1
  	// 
  	this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  	this.ClientSize = new System.Drawing.Size(712, 606);
  	this.Controls.Add(this.groupBox1);
  	this.Controls.Add(this.button1);
  	this.Controls.Add(this.pictureBox1);
  	this.Controls.Add(this.textBox2);
  	this.Controls.Add(this.label3);
  	this.Controls.Add(this.textBox1);
  	this.Controls.Add(this.label2);
  	this.Controls.Add(this.label1);
  	this.Controls.Add(this.comboBox8);
  	this.Controls.Add(this.comboBox7);
  	this.Controls.Add(this.comboBox6);
  	this.Controls.Add(this.comboBox5);
  	this.Controls.Add(this.comboBox4);
  	this.Controls.Add(this.comboBox3);
  	this.Controls.Add(this.comboBox2);
  	this.Controls.Add(this.comboBox1);
  	this.Name = "Form1";
  	this.Text = "Form1";
  	this.groupBox1.ResumeLayout(false);
  	this.ResumeLayout(false);

  }
  #endregion

  public class Part
  { // begins class Part


  	private 
  	Form1 owner;
  	int hardware;
  	double qty;
  
  	
  	public
  	Part(Form1 Owner)
  	{
    owner = Owner;
  	}
  	void parts_Name(int parts)
  	{
    this.hardware = parts;
  	}

  	void parts_Price(double price)
  	{
    this.qty = price;
  	}

  	
  	


  

  /*	void storeInFile()
  	{
    // create a file stream, for "computer.dat"
    FileStream fs = new FileStream("computer.dat", FileMode.CreateNew, FileAccess.ReadWrite);
    // create a stream writer
    StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.ASCII);

    //write to file (buffer), where textBox1 is your text box
    sw.WriteLine(owner.label4.Text);
    sw.WriteLine(owner.label5.Text);
    sw.WriteLine(owner.label6.Text);
    sw.WriteLine(owner.label7.Text);
    sw.WriteLine(owner.label8.Text);
    sw.WriteLine(owner.label9.Text);
    sw.WriteLine(owner.label10.Text);
    sw.Write(owner.label11.Text);

    // close stream writer and file
    sw.Close();
    fs.Close();
  	}

*/
  	
  } // end class Part

	/*	public class Processor :  Part
  {
            
  }*/

  public class Memory : Part
  {
  	public Memory(Form1 Owner) : base(Owner)
  	{
  	}
  }

  public class HardDrive : Part
  {
  	public HardDrive(Form1 Owner) : base(Owner)
  	{
  	}

  }

  public class CDRom : Part
  {
  	public CDRom(Form1 Owner) : base(Owner)
  	{
  	}
  }

  public class Floppy : Part
  {
  	public Floppy(Form1 Owner) : base(Owner)
  	{
  	}
  }

  public class Keyboard : Part
  {
  	public Keyboard(Form1 Owner) : base(Owner)
  	{
  	}
  }

  public class Monitor : Part
  {
  	public Monitor(Form1 Owner) : base(Owner)
  	{
  	}
  }

  public class Mouse : Part
  {
  	public Mouse(Form1 Owner) : base(Owner)
  	{
  	}
  }


  /// &lt;summary&gt;
  /// The main entry point for the application.
  /// &lt;/summary&gt;
  [STAThread]
  static void Main() 
  {
  	Application.Run(new Form1());
  }

  private void button1_Click(object sender, System.EventArgs e)
  {
  	label12.Text = textBox1.Text += textBox2.Text += ", thanks for selecting us as your preferred brand!";
  	label4.Text = comboBox1.Text;
  	label5.Text = comboBox2.Text;
  	label6.Text = comboBox3.Text;
  	label7.Text = comboBox4.Text;
  	label8.Text = comboBox5.Text;
  	label9.Text = comboBox6.Text;
  	label10.Text = comboBox7.Text;
  	label11.Text = comboBox8.Text;

  	// create a file stream, for "computer.dat"
  	FileStream fs = new FileStream("computer.dat", FileMode.CreateNew, FileAccess.ReadWrite);
  	// create a stream writer
  	StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.ASCII);

  	//write to file (buffer), where textBox1 is your text box

  	sw.WriteLine(label4.Text.ToString()); 
  	sw.WriteLine(label5.Text.ToString());
  	sw.WriteLine(label6.Text.ToString());
  	sw.WriteLine(label7.Text.ToString());
  	sw.WriteLine(label8.Text.ToString());
  	sw.WriteLine(label9.Text.ToString());
  	sw.WriteLine(label10.Text.ToString());
  	sw.WriteLine(label11.Text.ToString());

  	
  	// close stream writer and file
  	sw.Close();
  	fs.Close();


  } // end button1_Click

	} // end class Form1
} // end namespace



    





Thanks, for all the help!

Edited by marie
  • 0

this will find the price by searching the label.

ex.

label4.Text = "Intel? Pentium? 4 Processor (2.80GHz, 533 FSB) [add $30]"

use: abc(label4); (make a better name than "abc")

it will return an interger thats 30.

so

int price = 0;

price += abc(label4);

price += abc(label5);

price += abc(label6);

price += abc(label7);

price += abc(label8);

price += abc(label9);

price += abc(label10);

price += abc(label11);

textBox3.Text = price.ToString();

public int abc( Label aLabel )
 ? ? ? ?{
 ? ? ? ? ? ?int price = 0;
 ? ? ? ? ? ?int startIndex = 0;
 ? ? ? ? ? ?int endIndex = 0;
 ? ? ? ? ? ?string subString;
 ? ? ? ? ? 
 ? ? ? ? ? ?startIndex = aLabel.Text.LastIndexOf("$"); ? // give me the index at char "$"
 ? ? ? ? ? ?endIndex = aLabel.Text.LastIndexOf("]"); ? ? // give me the index at char "]"

 ? ? ? ? ? ?subString = label4.Text.Substring(startIndex + 1, (endIndex - 1) - startIndex);

 ? ? ? ? ? ?return Convert.ToInt16(subString);
 ? ? ? ?}

:)l check again if there are any problems in a while, TV TIME :)

  • 0

Whoa!

That really made sense.

But uhmm..

i added the:

price += abc(label4);

price += abc(label5);

price += abc(label6);

price += abc(label7);

price += abc(label8);

price += abc(label9);

price += abc(label10);

price += abc(label11);

textBox3.Text = price.ToString();

in the code you pasted and i think icant' get it to work.

Anwyays, i tihnk you might be watching TV (well-deserved)

I'll be here... so if you or anyone else could give me some help, i would appreciate it.

THANKS A LOT for the PATIENCE!

  • 0
  marie said:
Whoa!

That really made sense.

But uhmm..

i added the:

price += abc(label4);

price += abc(label5);

price += abc(label6);

price += abc(label7);

price += abc(label8);

price += abc(label9);

price += abc(label10);

price += abc(label11);

textBox3.Text = price.ToString();

in the code you pasted and i think icant' get it to work.

Anwyays, i tihnk you might be watching TV (well-deserved)

I'll be here... so if you or anyone else could give me some help, i would appreciate it.

THANKS A LOT for the PATIENCE!

586579777[/snapback]

opps, there was a problem in the abc()

the correct way

public int abc( Label aLabel )
        {
            int price = 0;
            int startIndex = 0;
            int endIndex = 0;
            string subString;
           
            startIndex = aLabel.Text.LastIndexOf("$");   // give me the index at char "$"
            endIndex = aLabel.Text.LastIndexOf("]");     // give me the index at char "]"

            subString = aLabel.Text.Substring(startIndex + 1, (endIndex - 1) - startIndex);

            return Convert.ToInt16(subString);
        }

there was a boo boo in " subString = aLabel.Text.Substring(startIndex + 1, (endIndex - 1) - startIndex);" in the last post.

use it in

public void calculatePrice()
        {
            int totalPrice = 0;
            totalPrice += abc(label4);
            totalPrice += abc(label5);
            totalPrice += abc(label6);
            totalPrice += abc(label7);
            totalPrice += abc(label8);
            totalPrice += abc(label9);
            totalPrice += abc(label10);
            totalPrice += abc(label11);

            textBox3.Text = totalPrice.ToString();          

        }

to clean stuff up and lastly just call "calculatePrice()" in your button event like:

        private void button1_Click(object sender, System.EventArgs e)
        {
            label12.Text = textBox1.Text += textBox2.Text += ", thanks for selecting us as your preferred brand!";
            label4.Text = comboBox1.Text;
            label5.Text = comboBox2.Text;
            label6.Text = comboBox3.Text;
            label7.Text = comboBox4.Text;
            label8.Text = comboBox5.Text;
            label9.Text = comboBox6.Text;
            label10.Text = comboBox7.Text;
            label11.Text = comboBox8.Text;

            // create a file stream, for "computer.dat"
            FileStream fs = new FileStream("computer.dat", FileMode.CreateNew, FileAccess.ReadWrite);
            // create a stream writer
            StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.ASCII);

            //write to file (buffer), where textBox1 is your text box

            sw.Write(label4.Text.ToString() + Environment.NewLine);
            sw.Write(label5.Text.ToString() + Environment.NewLine);
            sw.Write(label6.Text.ToString() + Environment.NewLine);
            sw.Write(label7.Text.ToString() + Environment.NewLine);
            sw.Write(label8.Text.ToString() + Environment.NewLine);
            sw.Write(label9.Text.ToString() + Environment.NewLine);
            sw.Write(label10.Text.ToString() + Environment.NewLine);
            sw.Write(label11.Text.ToString() + Environment.NewLine);

            // flush buffer (so the text really goes into the file)
            sw.Flush();
            // close stream writer and file
            sw.Close();
            fs.Close();

            calculatePrice();
             
        } // end button1_Click

and thats it

  • 0

According to Reflector:

public override void Close()
{
      this.Dispose(true);
      GC.nativeSuppressFinalize(this);
}



protected virtual void Dispose(bool disposing)
{
      if (this._handleProtector != null)
      {
            if (!this._handleProtector.IsClosed)
            {
                  this.Flush();
            }
            this._handleProtector.Close();
      }
      this._canRead = false;
      this._canWrite = false;
      this._canSeek = false;
      this._buffer = null;
}

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • RIP Hotlips..... IMHO, her best scenes were the few where she dared to let her REAL feminine side show through from underneath all that crappy, worn-torn soldier facade that she had to keep up with...she instantly lit up every room or situation where she was featured !
    • Helium Converter 3.3.69.0 by Razvan Serea Helium Converter is a free Windows utility for converting audio files between formats such as MP3, FLAC, AAC, WMA, OGG, and WAV. It supports batch conversion, preserves or updates tag information, and offers features like volume normalization. With a simple interface, it's ideal for users who need to convert large music libraries quickly and efficiently while retaining metadata. Helium Converter key features: Supports file formats: MP3, MP4, FLAC, AAC, M4A, WMA, WAV, OGG, OPUS, APE.... Batch conversion for large music libraries Preserves and edits metadata (ID3, Vorbis Comments, etc.) Volume normalization to equalize loudness Album art extraction and embedding Drag-and-drop interface for quick file selection Adjustable encoding parameters (bitrate, sample rate, channels) Uses internal codecs for consistent performance Supports CUE sheets for split track conversion File renaming based on tags during export Unicode support for international file and tag names Logging of conversion processes for troubleshooting Multi-core CPU support for faster conversions Download: Helium Converter 3.3.69.0 | 25.1 MB (Freeware) Links: Helium Converter Home Page | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Since it's been quite a while since the last episode aired, would it be fair (or cruel) to refer to Peggy as a MILF, or even a SMILF ?  HAHAHAHAHAHA
    • Their computers are not gonna stop working in October
    • Crowdstrike the same service provider that caused millions of in damages? I hate disliking a company for a singular failure but they really screwed up.
  • Recent Achievements

    • One Year In
      WaynesWorld earned a badge
      One Year In
    • First Post
      chriskinney317 earned a badge
      First Post
    • Week One Done
      Nullun earned a badge
      Week One Done
    • First Post
      sultangris earned a badge
      First Post
    • Reacting Well
      sultangris earned a badge
      Reacting Well
  • Popular Contributors

    1. 1
      +primortal
      172
    2. 2
      snowy owl
      122
    3. 3
      ATLien_0
      122
    4. 4
      Xenon
      116
    5. 5
      +Edouard
      93
  • Tell a friend

    Love Neowin? Tell a friend!