none
Error with .txt File to Label in WinForm

    Question

  • For some reason, when my  stream.WriteLine(tboxname.Text);  writes the users first name to the .txt file, it works. But then, when I want my label (nameforareyou) to retrieve the users last entered first name, it gives me this out of no where, instead of the name it gives me System.Windows.Forms.Label, Text:. So it literally says when I run it Are YouSystem.Windows.Forms.Label, Text:. I put the code for the form below. If someone could help that would be great.

    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.IO;
    using System.Windows.Forms;
    
    namespace Quiz_Game_2
    {
        public partial class Form4 : Form
        {
            public Form4()
            {
                InitializeComponent();
                proceedButton.Click += proceedButton_click;
    
            }
    
            private void Form4_Load(object sender, EventArgs e)
            {
                proceedButton.Visible = false;
                Check.Visible = false;
                X.Visible = false;
                labelAboveType.Visible = false;
                nameforareyou.Visible = false;
    
    
    
                if (File.Exists(@"C:\NKQG_G2\name.txt"))
                {
    
                    labelAboveType.Text = "Are You"+nameforareyou;
                    labelAboveType.Visible = true;
                    labelAboveType.BringToFront();
                   
                }
    
    
            }
    
            public string MenuItems;
    
    
    
            public FormStartPosition Center { get; private set; }
            public static FormStartPosition Position { get; private set; }
    
    
    
            private void klamenickToolStripMenuItem_Click(object sender, EventArgs e)
            {
                System.Diagnostics.Process.Start("https://twitter.com/klamenick");
            }
    
            private void noahKamenickToolStripMenuItem_Click(object sender, EventArgs e)
            {
                System.Diagnostics.Process.Start("https://facebook.com");
            }
    
            private void klamenickToolStripMenuItem1_Click(object sender, EventArgs e)
            {
                System.Diagnostics.Process.Start("https://instagram.com/klamenick");
            }
    
            private void aboutNKQGG2ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                quizAbout qa = new quizAbout();
    
                qa.Show();
            }
    
            private void exitToolStripMenuItem_Click(object sender, EventArgs e)
            {
                Application.Exit();
            }
    
            private void restartToolStripMenuItem_Click(object sender, EventArgs e)
            {
                Application.Restart();
            }
    
            private void proceedButton_click(object sender, EventArgs e)
            {
    
                string file = @"C:\NKQG_G2\name.txt";
    
                Directory.CreateDirectory(Path.GetDirectoryName(file));
                using (var stream = File.CreateText(file))
    
                {
    
                    stream.WriteLine(tboxname.Text);
    
                }
    
    
                Form5 f5 = new Form5();
                this.Hide();
                f5.ShowDialog();
                this.Close();
    
            }
    
            private void tboxname_TextChanged(object sender, EventArgs e)
    
            {
    
                if (tboxname.TextLength > 2)
                {
    
                    proceedButton.Show(); Check.Show(); X.Hide();
    
                }
    
                if (tboxname.TextLength <= 1)
                {
    
                    proceedButton.Hide(); Check.Hide(); X.Hide();
    
                }
    
                if (tboxname.TextLength == 0)
                {
    
                    proceedButton.Hide(); X.Hide(); Check.Hide();
    
                }
    
                if (tboxname.TextLength <= 2)
                {
    
                    proceedButton.Hide(); X.Show(); Check.Hide();
    
                }
    
                if (tboxname.Text.Length > 0 && tboxname.Text.Trim().Length == 0)
                {
    
                    proceedButton.Hide(); X.Show(); Check.Hide();
    
                }
    
                tboxname.MaxLength = 15;
    
    
    
            }
    
            private void nameforareyou_Click(object sender, EventArgs e)
            {
                StreamReader objstream = new StreamReader("C:\\NKQG_G2\name.txt");
    
                nameforareyou.Text = objstream.ReadLine();
    
            }
    
    
        }
    
    }
    

    Sunday, June 12, 2016 9:41 PM

Answers

  • Hi,

    that your label is now empty is correct.

    This is because "Form4_Load" is called before "nameforareyou_Load".

    The text of the label is loaded in "nameforareyou_Load" and used in "Form4_Load".

    So it is used before the text content is written.

    Why you use that XXX_Load methods?

    What you want to have is exactly the following, or?

    using System;
    using System.IO;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            private string fileName;
    
            public Form1()
            {
                InitializeComponent();
    
                fileName = Path.Combine(Directory.GetCurrentDirectory(), "name.txt");
                LoadLabel();
            }
    
            private void LoadLabel()
            {
                if (File.Exists(fileName))
                {
                    label.Text = "Are You" + File.ReadAllText(fileName);
                    label.Visible = true;
                }
                else
                {
                    label.Visible = false;
                }
            }
    
            private void button_Click(object sender, EventArgs e)
            {
                if (File.Exists(fileName))
                {
                    File.Delete(fileName);
                }
                File.WriteAllText(fileName, textbox.Text);
            }
        }
    }
    
    namespace WindowsFormsApplication1
    {
        partial class Form1
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;
    
            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
    
            #region Windows Form Designer generated code
    
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                this.textbox = new System.Windows.Forms.TextBox();
                this.label = new System.Windows.Forms.Label();
                this.button = new System.Windows.Forms.Button();
                this.SuspendLayout();
                // 
                // textbox
                // 
                this.textbox.Location = new System.Drawing.Point(240, 113);
                this.textbox.Name = "textbox";
                this.textbox.Size = new System.Drawing.Size(670, 31);
                this.textbox.TabIndex = 0;
                // 
                // label
                // 
                this.label.AutoSize = true;
                this.label.Location = new System.Drawing.Point(424, 295);
                this.label.Name = "label";
                this.label.Size = new System.Drawing.Size(58, 25);
                this.label.TabIndex = 1;
                this.label.Text = "label";
                // 
                // button
                // 
                this.button.Location = new System.Drawing.Point(792, 256);
                this.button.Name = "button";
                this.button.Size = new System.Drawing.Size(153, 92);
                this.button.TabIndex = 2;
                this.button.Text = "proceedButton";
                this.button.UseVisualStyleBackColor = true;
                this.button.Click += new System.EventHandler(this.button_Click);
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(12F, 25F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(1130, 821);
                this.Controls.Add(this.button);
                this.Controls.Add(this.label);
                this.Controls.Add(this.textbox);
                this.Name = "Form1";
                this.Text = "Form1";
                this.ResumeLayout(false);
                this.PerformLayout();
    
            }
    
            #endregion
    
            private System.Windows.Forms.TextBox textbox;
            private System.Windows.Forms.Label label;
            private System.Windows.Forms.Button button;
        }
    }
    
    

    Regards,

    David

    Monday, June 13, 2016 6:46 AM
  • Ah yes that makes complete sense. I think I fixed it, but what you said above was correct. I changed up the order of the code and added another label and it worked. Thank you so much! Greatly Appreciated!

    Monday, June 13, 2016 10:27 PM

All replies

  • Hi,

    I think the problem is that you use the label object instead of the labels text.

    Instead of:

    if (File.Exists(@"C:\NKQG_G2\name.txt"))
    {
        labelAboveType.Text = "Are You"+nameforareyou;
        labelAboveType.Visible = true;
        labelAboveType.BringToFront();
       
    }

    Use this:

    if (File.Exists(@"C:\NKQG_G2\name.txt"))
    {
        labelAboveType.Text = "Are You"+nameforareyou.Text;
        labelAboveType.Visible = true;
        labelAboveType.BringToFront();
       
    }

    Regards,

    David

    Sunday, June 12, 2016 11:05 PM
  • Now when I do that, it doesn't show that nonsense. Now it shows nothing.
    Monday, June 13, 2016 2:17 AM
  • Here is the code: I did make a few changes after you said that. They see to be minor.

    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.IO;
    using System.Windows.Forms;
    
    namespace Quiz_Game_2
    {
        public partial class Form4 : Form
        {
            public Form4()
            {
                InitializeComponent();
                proceedButton.Click += proceedButton_click;
        
            }
    
            private void Form4_Load(object sender, EventArgs e)
            {
                proceedButton.Visible = false;
                Check.Visible = false;
                X.Visible = false;
                areYou.Visible = false;
                nameforareyou.Visible = false;
    
    
    
                if (File.Exists(@"C:\NKQG_G2\name.txt"))
                {
                    nameforareyou.Visible = true;
                    nameforareyou.BringToFront();
                    areYou.Text = "Are You" + nameforareyou;
                    areYou.Visible = true;
                    areYou.BringToFront();
                    
                }
    
    
            }
    
            public string MenuItems;
    
    
    
            public FormStartPosition Center { get; private set; }
            public static FormStartPosition Position { get; private set; }
    
    
    
            private void klamenickToolStripMenuItem_Click(object sender, EventArgs e)
            {
                System.Diagnostics.Process.Start("https://twitter.com/klamenick");
            }
    
            private void noahKamenickToolStripMenuItem_Click(object sender, EventArgs e)
            {
                System.Diagnostics.Process.Start("https://facebook.com");
            }
    
            private void klamenickToolStripMenuItem1_Click(object sender, EventArgs e)
            {
                System.Diagnostics.Process.Start("https://instagram.com/klamenick");
            }
    
            private void aboutNKQGG2ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                quizAbout qa = new quizAbout();
    
                qa.Show();
            }
    
            private void exitToolStripMenuItem_Click(object sender, EventArgs e)
            {
                Application.Exit();
            }
    
            private void restartToolStripMenuItem_Click(object sender, EventArgs e)
            {
                Application.Restart();
            }
    
            private void proceedButton_click(object sender, EventArgs e)
            {
    
                string file = @"C:\NKQG_G2\name.txt";
    
                Directory.CreateDirectory(Path.GetDirectoryName(file));
                using (var stream = File.CreateText(file))
    
                {
    
                    stream.WriteLine(tboxname.Text);
    
                }
    
    
                Form5 f5 = new Form5();
                this.Hide();
                f5.ShowDialog();
                this.Close();
    
            }
    
            private void tboxname_TextChanged(object sender, EventArgs e)
    
            {
    
                if (tboxname.TextLength > 2)
                {
    
                    proceedButton.Show(); Check.Show(); X.Hide();
    
                }
    
                if (tboxname.TextLength <= 1)
                {
    
                    proceedButton.Hide(); Check.Hide(); X.Hide();
    
                }
    
                if (tboxname.TextLength == 0)
                {
    
                    proceedButton.Hide(); X.Hide(); Check.Hide();
    
                }
    
                if (tboxname.TextLength <= 2)
                {
    
                    proceedButton.Hide(); X.Show(); Check.Hide();
    
                }
    
                if (tboxname.Text.Length > 0 && tboxname.Text.Trim().Length == 0)
                {
    
                    proceedButton.Hide(); X.Show(); Check.Hide();
    
                }
    
                tboxname.MaxLength = 15;
    
    
    
            }
    
            private void nameforareyou_Load(object sender, EventArgs e)
    
            {
                StreamReader objstream = new StreamReader("C:\\NKQG_G2\name.txt");
    
                nameforareyou.Text = objstream.ReadToEnd();
    
            }
    
            private void nameforareyou_Click(object sender, EventArgs e)
            {
    
            }
    
            private void areYou_Click(object sender, EventArgs e)
            {
    
            }
        }
    
    }

    Monday, June 13, 2016 2:21 AM
  • Hi,

    that your label is now empty is correct.

    This is because "Form4_Load" is called before "nameforareyou_Load".

    The text of the label is loaded in "nameforareyou_Load" and used in "Form4_Load".

    So it is used before the text content is written.

    Why you use that XXX_Load methods?

    What you want to have is exactly the following, or?

    using System;
    using System.IO;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            private string fileName;
    
            public Form1()
            {
                InitializeComponent();
    
                fileName = Path.Combine(Directory.GetCurrentDirectory(), "name.txt");
                LoadLabel();
            }
    
            private void LoadLabel()
            {
                if (File.Exists(fileName))
                {
                    label.Text = "Are You" + File.ReadAllText(fileName);
                    label.Visible = true;
                }
                else
                {
                    label.Visible = false;
                }
            }
    
            private void button_Click(object sender, EventArgs e)
            {
                if (File.Exists(fileName))
                {
                    File.Delete(fileName);
                }
                File.WriteAllText(fileName, textbox.Text);
            }
        }
    }
    
    namespace WindowsFormsApplication1
    {
        partial class Form1
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;
    
            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
    
            #region Windows Form Designer generated code
    
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                this.textbox = new System.Windows.Forms.TextBox();
                this.label = new System.Windows.Forms.Label();
                this.button = new System.Windows.Forms.Button();
                this.SuspendLayout();
                // 
                // textbox
                // 
                this.textbox.Location = new System.Drawing.Point(240, 113);
                this.textbox.Name = "textbox";
                this.textbox.Size = new System.Drawing.Size(670, 31);
                this.textbox.TabIndex = 0;
                // 
                // label
                // 
                this.label.AutoSize = true;
                this.label.Location = new System.Drawing.Point(424, 295);
                this.label.Name = "label";
                this.label.Size = new System.Drawing.Size(58, 25);
                this.label.TabIndex = 1;
                this.label.Text = "label";
                // 
                // button
                // 
                this.button.Location = new System.Drawing.Point(792, 256);
                this.button.Name = "button";
                this.button.Size = new System.Drawing.Size(153, 92);
                this.button.TabIndex = 2;
                this.button.Text = "proceedButton";
                this.button.UseVisualStyleBackColor = true;
                this.button.Click += new System.EventHandler(this.button_Click);
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(12F, 25F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(1130, 821);
                this.Controls.Add(this.button);
                this.Controls.Add(this.label);
                this.Controls.Add(this.textbox);
                this.Name = "Form1";
                this.Text = "Form1";
                this.ResumeLayout(false);
                this.PerformLayout();
    
            }
    
            #endregion
    
            private System.Windows.Forms.TextBox textbox;
            private System.Windows.Forms.Label label;
            private System.Windows.Forms.Button button;
        }
    }
    
    

    Regards,

    David

    Monday, June 13, 2016 6:46 AM
  • Ah yes that makes complete sense. I think I fixed it, but what you said above was correct. I changed up the order of the code and added another label and it worked. Thank you so much! Greatly Appreciated!

    Monday, June 13, 2016 10:27 PM
  • Hi Klamenick,

    Glad to hear you have had your issue resolved. And thanks for sharing us your result here.

    If you think David reply is helpful, please mark his or your reply as the answer, it will benefit to other community members who have the same issue. It will be easier for them to search the solution.

    Have a nice day:)

    Best Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.

    Click HERE to participate the survey.


    Tuesday, June 14, 2016 5:21 AM
    Moderator