Load a TxT file with data and pass these to a Textbox

0

You see, I need to do the following.
My level of programming in this is minimal since we have only followed a small guide provided by the teacher and it does not cover most of the things written here. Just the save.

Save data in a txt for example. Done and functional.
Open the Txt. (I do not know)
Load Txt data to different TextBox (I do not know)

Save Name: {0} (what you have written in the program) And save it with the file search where I indicated.

Now I need to know how to open that txt. Using the file browser. '

        SaveFileDialog saveDialog = new SaveFileDialog();
        saveDialog.DefaultExt = "txt";
        saveDialog.AddExtension = true;
        saveDialog.FileName = textboxApellidos.Text;
        saveDialog.InitialDirectory = @"C:\Users\Javier\Documents\";
        saveDialog.OverwritePrompt = true;
        saveDialog.Title = "Citas";
        saveDialog.ValidateNames = true;
        if (saveDialog.ShowDialog().Value)
        {
            using (StreamWriter writer = new StreamWriter(saveDialog.FileName))
            {
                writer.WriteLine("Nombre: {0}", textboxNombre.Text);
                writer.WriteLine("Apellido: {0}", textboxApellidos.Text);
                if (hombreCB.IsChecked == true)
                {
                    writer.WriteLine("Sexo: {0}", hombreCB.Content.ToString());
                }
                else
                {
                    writer.WriteLine("Sexo: {0}", mujerCB.Content.ToString());
                }
                writer.WriteLine("Próxima cita: {0}", proximaCita.Text);
                writer.WriteLine("Sintomas:{0} ", textboxSintomas.Text);
                writer.WriteLine("Diagnóstico:{0} ", textboxDiagnostico.Text);

                writer.WriteLine("Doctor: {0}", doctorsCB.Text);

                MessageBox.Show("Cita guardada.");

            }
        }

    }'

This is the code to save it, but I am unable to figure out how to open it, much less load the data I have saved in a textbox. Any help would be great.

This is the new code that I created with the help of a response. 'private void AccessCita_Click (object sender, RoutedEventArgs e)         {

OpenFileDialog openFileDialog1 = new OpenFileDialog();

openFileDialog1.InitialDirectory = "c:\" ;
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
openFileDialog1.FilterIndex = 2 ;
openFileDialog1.RestoreDirectory = true ;
 StreamReader fp; char[] buffer = new char[160]; int i = 0; string texto; string[] split = null; bool bok = true;

        if (openFileDialog1.ShowDialog() == true)
{
            string line;
            string filename = openFileDialog1.FileName;
            fp = new StreamReader(filename);
            if (fp != null)
            {
                Paciente concita = new Paciente();
                concita.ShowDialog();
                i = 0;
                do
                {
                    texto = fp.ReadLine();
                    i++;
                    if (texto != null)
                    {
                        if (texto.Length > 2)
                        {
                            split = texto.Split(';');

                            concita.textboxNombre.Text = split[0];
                            concita.textboxApellidos.Text = split[1];
                           // concita.textboxSexo.Text = split[2];
                            concita.textboxSintomas.Text = split[3];
                            concita.textboxDiagnostico.Text = split[4];
                        }
                    }
                } while (!fp.EndOfStream);
            }
            fp.Close();

            if (i > 1)
                MessageBox.Show("Carga de archivo terminada..." + i.ToString());
        }

    }'

The file search engine opens, I select the appointment and a new window opens, but the data is not filled in. In addition, closing the appointment window gives an error.

    
asked by Estoyjodido 22.04.2017 в 20:44
source

2 answers

1
  

if you have a flat file (txt) with the information separated by semicolons   The method in c # can be like the following:

public void Download (string fileName)         {             StreamReader fp;             char [] buffer = new char [160];             int i = 0;             text string;             string [] split = null;             bool bok = true;

        fp = new StreamReader(fileName);
        if (fp != null)
        {
            i = 0;
            do
            {
                texto = fp.ReadLine();
                i++;
                if (texto != null)
                {
                    if (texto.Length > 2)
                    {
                        split = texto.Split(';');

                        textboxNombre.Text      = split[0];
                        textboxApellidos.Text   = split[1];
                        textboxSexo.Text        = split[2];
                        textboxSintomas.Text    = split[3];
                        textboxDiagnostico.Text = split[4];
                    }
                }
            } while (!fp.EndOfStream);
        }
        fp.Close();
        frm.Close();
        if (i > 1)
                MessageBox.Show("Carga de archivo terminada..."+i.ToString());
    }
    
answered by 23.04.2017 / 18:13
source
0
  

Look at this fragment of what is called a flat file and then load it   The DownLoad () method is the one that you had previously sent   I recommend you to do two methods, one to record, and another to upload data   This is the method to upload data

private void UploadData () {

DialogResult result;

OpenFileDialog openFileDialog1 = new OpenFileDialog (); OpenFileDialog1.Title="Browse Ascii Files";

openFileDialog1.Filter="Text Files (.asc) | .txt | All Files ( .asc) | . "; openFileDialog1.FilterIndex = 2;

openFileDialog1.DefaultExt="txt";

result = openFileDialog1.ShowDialog ();

if (result == DialogResult.OK) {      Download (openFileDialog1.FileName.Trim ()); } }

    
answered by 27.04.2017 в 16:16