
Originariamente inviata da
oregon
Di cosa discutiamo ? Mi mostri tutto il codice aggiornato ?
Eccolo qui:
codice:
Imports System.IO
Public Class Mix
Dim elems As Integer
Dim cont, cerca As Integer
'Dim numeri(3) As Integer
Dim numeri() As Integer
'Sub Ordina
Private Sub ordina(ByRef a As Integer, ByRef b As Integer)
Dim t As Integer
If a > b Then
t = a
a = b
b = t
End If
End Sub
'Function MCD
Private Function mcd(ByVal numero As Integer, ByVal divisore As Integer) As Integer
Dim resto As Single
resto = numero Mod divisore
If resto = 0 Then
Return divisore
Else
Return mcd(divisore, resto)
End If
End Function
'Aggiungi
Private Sub ButtonAggiungi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonAggiungi.Click
ReDim numeri(elems)
If (cont < numeri.Length) Then
numeri(cont) = TextBoxNumero.Text
LabelElenco.Text = LabelElenco.Text + vbCrLf + Str(numeri(cont))
cont = cont + 1
Else
MsgBox("Numeri esauriti!")
End If
TextBoxNumero.ResetText()
End Sub
'Ordina
Private Sub ButtonOrdina_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonOrdina.Click
LabelOrdinato.Text = "Elenco ordinato:"
'ReDim numeri(elems)
For i = 0 To numeri.Length - 1
For j = 0 To numeri.Length - 2
ordina(numeri(j), numeri(j + 1))
Next
Next
For i = 0 To numeri.Length - 1
LabelOrdinato.Text = LabelOrdinato.Text + vbCrLf + Str(numeri(i))
Next
End Sub
'Cerca
Private Sub ButtonCerca_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonCerca.Click
Dim si As Boolean = False
'ReDim numeri(elems)
For i = 0 To numeri.Length - 1
If numeri(i) = cerca Then
si = True
End If
Next
If si = True Then
MsgBox("Il" + Str(cerca) + " è presente!")
Else
MsgBox("Il" + Str(cerca) + " non c'è!")
End If
End Sub
'MCD
Private Sub ButtonMCD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonMCD.Click
'ReDim numeri(elems)
LabelMCD.Text = "MCD:" + Str(mcd(numeri(0), numeri(1)))
End Sub
'Salva
Private Sub ButtonSalva_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSalva.Click
'ReDim numeri(elems)
Try
Dim StreamToWrite As StreamWriter
Dim nomeFile As String
nomeFile = "D:\Numeri.txt"
StreamToWrite = New StreamWriter(nomeFile)
For i = 0 To numeri.Length - 1
StreamToWrite.Write(Str(numeri(i)) + ";")
Next
StreamToWrite.Close()
MsgBox("Il file è stato salvato in " + nomeFile)
Catch ex As Exception
MsgBox("Non è possibile salvare il file.", MsgBoxStyle.Critical)
End Try
End Sub
'Leggi
Private Sub ButtonLeggi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonLeggi.Click
Try
Dim StreamToRead As StreamReader
Dim nomeFile As String
Dim FileLetto As String
Dim FileStringhe() As String
nomeFile = "D:\Numeri.txt"
StreamToRead = New StreamReader(nomeFile)
FileLetto = StreamToRead.ReadToEnd
FileStringhe = FileLetto.Split(";")
LabelLeggi.Text = "File letto: " '+ vbCrLf + FileLetto
For i = 0 To FileStringhe.Length - 1
LabelLeggi.Text = LabelLeggi.Text + vbNewLine + FileStringhe(i)
Next
StreamToRead.Close()
Catch ex As Exception
MsgBox("Non è possibile leggere il file.", MsgBoxStyle.Critical)
End Try
End Sub
'Ok
Private Sub ButtonOk_Click(sender As Object, e As EventArgs) Handles ButtonOk.Click
cerca = TextBoxCerca.Text
End Sub
'Array
Public Sub ButtonOkArray_Click(sender As Object, e As EventArgs) Handles ButtonOkArray.Click
elems = TextBoxArray.Text - 1
MsgBox("Array =" + Str(elems + 1))
End Sub
End Class
E questo è il form:
Form.PNG