Utilizzando il sistema Visual Basic, è possibile inserire un file di testo in un programma, all'interno di una finestra chiamata "RichTextBox", disponibile nella casella degli strumenti. I metodi utilizzabili sono due: il read to end, che va a prendere il file di testo inserendolo interamente nel programma; il read to line, che invece scorre il testo riga per riga e lo inserisce man mano nella finestra. L'implementazione di entrambi i metodi è differente a seconda del linguaggio utilizzato (vbnet o c#).
Read to end
Vb.Net
Imports System
Imports System.IO
Imports System.IO
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.RichTextBox1.Text = “Il mio primo programma”
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.RichTextBox1.Text = “Il mio primo programma”
‘Vogliamo aprire un file di testo’
Dim o As New OpenFileDialog
o.ShowDialog()
Dim NomeFile As String = o.FileName
Dim o As New OpenFileDialog
o.ShowDialog()
Dim NomeFile As String = o.FileName
Me.RichTextBox1.AppendText(Environment.NewLine & NomeFile)
‘Definizione del lettore’
Dim s As New StreamReader(NomeFile)
Dim TuttoIlfile As String = s.ReadToEnd()
Me.RichTextBox1.AppendText(Environment.NewLine & TuttoIlfile)
End Sub
End Class
End Class
C#
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
public class Form1
{
private void Button1_Click(object sender, EventArgs e)
{
this.RichTextBox1.Text = “Il mio primo programma”;
{
private void Button1_Click(object sender, EventArgs e)
{
this.RichTextBox1.Text = “Il mio primo programma”;
//Vogliamo aprire un file di testo’
OpenFileDialog o = new OpenFileDialog();
o.ShowDialog();
string NomeFile = o.FileName;
OpenFileDialog o = new OpenFileDialog();
o.ShowDialog();
string NomeFile = o.FileName;
this.RichTextBox1.AppendText(Environment.NewLine + NomeFile);
//Definizione del lettore’
StreamReader s = new StreamReader(NomeFile);
string TuttoIlfile = s.ReadToEnd();
this.RichTextBox1.AppendText(Environment.NewLine + TuttoIlfile);
}
}
}
Read Line
Vb.Net
Imports System
Imports System.IO
Imports System.IO
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.RichTextBox1.Text = “Il mio primo programma”
‘Vogliamo aprire un file di testo
Dim o As New OpenFileDialog
o.ShowDialog()
Dim NomeFile As String = o.FileName
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.RichTextBox1.Text = “Il mio primo programma”
‘Vogliamo aprire un file di testo
Dim o As New OpenFileDialog
o.ShowDialog()
Dim NomeFile As String = o.FileName
Me.RichTextBox1.AppendText(Environment.NewLine & NomeFile)
‘Definizione del lettore
Dim s As New StreamReader(NomeFile)
Dim s As New StreamReader(NomeFile)
Dim Contatore As Integer = 1
Do
Dim RigaDelFile As String = s.ReadLine
Contatore = Contatore + 1
Me.RichTextBox1.AppendText(Environment.NewLine & “Riga #” & Contatore & “:” & RigaDelFile)
Loop Until s.EndOfStream
Contatore = Contatore + 1
Me.RichTextBox1.AppendText(Environment.NewLine & “Riga #” & Contatore & “:” & RigaDelFile)
Loop Until s.EndOfStream
End Sub
End Class
End Class
Sotto, viene riportato il codice codificato in linguaggio C#:
C#
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
public class Form1
{
private void Button1_Click(object sender, EventArgs e)
{
this.RichTextBox1.Text = “Il mio primo programma”;
//Vogliamo aprire un file di testo
OpenFileDialog o = new OpenFileDialog();
o.ShowDialog();
string NomeFile = o.FileName;
{
private void Button1_Click(object sender, EventArgs e)
{
this.RichTextBox1.Text = “Il mio primo programma”;
//Vogliamo aprire un file di testo
OpenFileDialog o = new OpenFileDialog();
o.ShowDialog();
string NomeFile = o.FileName;
this.RichTextBox1.AppendText(Environment.NewLine + NomeFile);
//Definizione del lettore
StreamReader s = new StreamReader(NomeFile);
StreamReader s = new StreamReader(NomeFile);
int Contatore = 1;
do {
string RigaDelFile = s.ReadLine;
Contatore = Contatore + 1;
this.RichTextBox1.AppendText(Environment.NewLine + “Riga #” + Contatore + “:” + RigaDelFile);
} while (!(s.EndOfStream));
do {
string RigaDelFile = s.ReadLine;
Contatore = Contatore + 1;
this.RichTextBox1.AppendText(Environment.NewLine + “Riga #” + Contatore + “:” + RigaDelFile);
} while (!(s.EndOfStream));
}
}
}
Nessun commento:
Posta un commento