segunda-feira, 17 de dezembro de 2018

C# - Lendo um arquivo de texto em textBox

Lendo um arquivo e Exibindo num textBox,
com quebra de linhas.







//Lendo um arquivo e Exibindo num textBox
//Com quebra de linhas
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.Windows.Forms;
using System.IO;
namespace keypress {

    public partial class Form1 : Form {
        public System.Windows.Forms.TextBox textBox;
        String str = "";
        /*============================================================*/
        public void Arquivo_Txt ( ) {
            textBox = new System.Windows.Forms.TextBox ( );
            int counter = 0;
            System.IO.StreamReader file =
                new System.IO.StreamReader ( "H:\\eclipse luna_two\\"
                  + "PROJETO\\PROJETO\\src\\Poesias famosas.txt" );
            try {
                while ( ( str = file.ReadLine ( ) ) != null ) {
                    str += "\n";
                    counter++;
                    textBox.Text += counter + "   ";
                    textBox.Text += str + "\r\n";
                }
                file.Close ( );
            } catch ( Exception e ) {
                MessageBox.Show ( "Não é possivel ler o arquivo" );
                MessageBox.Show ( e.Message );
            }
        }
        /*============================================================*/
        public void text_Box ( ) {
            textBox.ForeColor = Color.Black;
            textBox.Multiline = true;
            textBox.ScrollBars = ScrollBars.Vertical;
            textBox.Size = new System.Drawing.Size ( 565, 210 );
            textBox.Location = new Point ( 10, 40 );
            textBox.Font = new Font ( "Times New Roman", 12F,
            System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0 );
        }
        /*============================================================*/
        protected override void OnPaint ( PaintEventArgs e ) {
            this.Size = new System.Drawing.Size ( 600, 300 );
            this.Text = "Lendo um arquivo de texto em textBox";
            this.BackColor = Color.LightBlue;
            Graphics dc = e.Graphics;
            Pen BluePen = new Pen ( Color.Coral, 10 );
            dc.DrawRectangle ( BluePen, 5, 5, 575, 250 );
            Font myFont = new System.Drawing.Font ( "Helvetica", 14,
                FontStyle.Italic );
             Brush myBrush = new SolidBrush ( System.Drawing.Color.Black );
             dc.DrawString ( "Lendo um arquivo de texto em textBox",
            myFont, myBrush, 130, 10 );
        }
        /*============================================================*/
        public Form1 ( ) {
            Arquivo_Txt ( );
            text_Box ( );
            this.Controls.Add ( this.textBox );
        }
        /*============================================================*/
    }
    static class Program {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main ( ) {
            Application.EnableVisualStyles ( );
            Application.SetCompatibleTextRenderingDefault ( false );
            Application.Run ( new Form1 ( ) );
        }
    }
}

Nenhum comentário:

Postar um comentário