Click here to Skip to main content
15,896,207 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
So my problem is that I want to open data that are previously saved to a file from multiple textboxes, and I want when I open that file in order the data that has been saved in textboxes to show me same thing after I open, e.g. in textbox1 it is written name, textbox2 surname, textbox3 date of birth etc. so if I write in this order I want me to show, my problem is when I open it shows like this:
textbox1: NameSurnameDateofbirth, the exact same thing to all textboxes I get this after opening.

Here are the codes for saveFileDialog and OpenFileDialog:

Open File Dialog
C#
private void openToolStripMenuItem_Click(object sender, EventArgs e)
       {
           if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
           {
               label1.Text = openFileDialog1.FileName;
              textBox1.Text = File.ReadAllText(label1.Text);
               textBox2.Text = File.ReadAllText(label1.Text);
               textBox3.Text = File.ReadAllText(label1.Text);
               textBox4.Text = File.ReadAllText(label1.Text);
               textBox5.Text = File.ReadAllText(label1.Text);
               textBox6.Text = File.ReadAllText(label1.Text);
               textBox7.Text = File.ReadAllText(label1.Text);
               textBox8.Text = File.ReadAllText(label1.Text);
               textBox9.Text = File.ReadAllText(label1.Text);
           }
       }


And Save File Dialog:
C#
private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                File.WriteAllText(saveFileDialog1.FileName, "");
                    File.AppendAllText(saveFileDialog1.FileName, textBox1.Text);
                    File.AppendAllText(saveFileDialog1.FileName, textBox2.Text);
                    File.AppendAllText(saveFileDialog1.FileName, textBox3.Text);
                    File.AppendAllText(saveFileDialog1.FileName, textBox4.Text);
                    File.AppendAllText(saveFileDialog1.FileName, textBox5.Text);
                    File.AppendAllText(saveFileDialog1.FileName, textBox6.Text);
                    File.AppendAllText(saveFileDialog1.FileName, textBox7.Text);
                    File.AppendAllText(saveFileDialog1.FileName, textBox8.Text);
                    File.AppendAllText(saveFileDialog1.FileName, textBox9.Text);
            }
        }
Posted

1 solution

If I understand your question correctly, use ReadAllLines[^] instead of ReadAllText. This would give you an array of strings which you can use to assign correct 'lines' to corresponding text boxes.

So something like
C#
private void openToolStripMenuItem_Click(object sender, EventArgs e)
       {
          string fileName;
          string[] lines;

           if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
           {
               fileName = openFileDialog1.FileName;
               label1.Text = fileName;
               lines = File.ReadAllLines(fileName);
               textBox1.Text = lines[0];
               textBox2.Text = lines[1];
               textBox3.Text = lines[2];
               textBox4.Text = lines[3];
               textBox5.Text = lines[4];
               textBox6.Text = lines[5];
               textBox7.Text = lines[6];
               textBox8.Text = lines[7];
               textBox9.Text = lines[8];
           }
       }
 
Share this answer
 
v3
Comments
Medin Smaili 9-Sep-15 13:48pm    
the problem is at "lines = File.ReadAllLines(path);" it shows error "Error CS0103 The name 'path' does not exist in the current context"
Wendelius 9-Sep-15 14:26pm    
Sorry about the typo. path should be filename, the example is updated.
Medin Smaili 10-Sep-15 7:29am    
System.IndexOutOfRangeException was unhandled
HResult=-2146233080
Message=Index was outside the bounds of the array.
Source=Labaratori Ikre
StackTrace:
at Labaratori_Ikre.Form2.openToolStripMenuItem_Click(Object sender, EventArgs e) in C:\Users\Medo\documents\visual studio 2015\Projects\Labaratori Ikre\Labaratori Ikre\Form2.cs:line 77
at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
at System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStripMenuItem.ProcessCmdKey(Message& m, Keys keyData)
at System.Windows.Forms.ToolStripManager.ProcessShortcut(Message& m, Keys shortcut)
at System.Windows.Forms.ToolStripManager.ProcessCmdKey(Message& m, Keys keyData)
at System.Windows.Forms.ContainerControl.ProcessCmdKey(Message& msg, Keys keyData)
at System.Windows.Forms.Form.ProcessCmdKey(Message& msg, Keys keyData)
at System.Windows.Forms.Control.ProcessCmdKey(Message& msg, Keys keyData)
at System.Windows.Forms.TextBoxBase.ProcessCmdKey(Message& msg, Keys keyData)
at System.Windows.Forms.Control.PreProcessMessage(Message& msg)
at System.Windows.Forms.Control.PreProcessControlMessageInternal(Control target, Message& msg)
at System.Windows.Forms.Application.ThreadContext.PreTranslateMessage(MSG& msg)
at System.Windows.Forms.Application.ThreadContext.System.Windows.Forms.UnsafeNativeMethods.IMsoComponent.FPreTranslateMessage(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at Labaratori_Ikre.Program.Main() in C:\Users\Medo\documents\visual studio 2015\Projects\Labaratori Ikre\Labaratori Ikre\Program.cs:line 19
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:

Now this is what I get at "textBox2.Text = lines[0];"
Wendelius 10-Sep-15 9:25am    
If you open the text file for example into Notepad, does it contain the 10 lines?

Based on the error message it seems that the file contains no lines...
Medin Smaili 11-Sep-15 13:56pm    
one line and data has not been separated e.g. 123456789

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900