Public Class Form1
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Dim KeyAscii As Short = Asc(e.KeyChar)
If KeyAscii < 48 And KeyAscii <> 24 And KeyAscii <> 8 Then
KeyAscii = 0
ElseIf KeyAscii > 57 Then
KeyAscii = 0
MessageBox.Show("Consentiti solo numeri")
End If
If e.KeyChar = "0" And TextBox1.TextLength = 0 Then
KeyAscii = 0
End If
e.KeyChar = Chr(KeyAscii)
End Sub
Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
Dim str As String = TextBox1.Text
ProgressBar1.Step = 5
ProgressBar1.Minimum = 0
ProgressBar1.Maximum = str
ProgressBar1.Value = e.ProgressPercentage
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ListBox1.Items.Clear()
If Me.TextBox1.Text = "" Then
MessageBox.Show("inserisci numero")
End If
Dim str As String = TextBox1.Text
Dim n As Integer
If Integer.TryParse(str, n) Then
For x As Integer = 2 To n / 2
If n Mod x = 0 Then
ListBox1.Items.Add(CInt(n \ x))
If ProgressBar1.Value = ProgressBar1.Maximum Then
ProgressBar1.Value = ProgressBar1.Minimum
Else
BackgroundWorker1.ReportProgress(x)
End If
End If
Next
End If
If ListBox1.Items.Count = 0 Then
Label1.Text = "Il Numero è Primo"
Else
Label1.Text = "Il Numero non è Primo. I divisori sono:"
End If
Dim currentMax As Integer
Dim testItem As Integer
Dim ItemIndex As Integer
Dim ItemsSwapped As Boolean
Do
Try
ItemIndex = 0
ItemsSwapped = False
currentMax = ListBox1.Items.Item(ItemIndex)
Catch ex As Exception
Exit Do
End Try
Do
ItemIndex += 1
Try
testItem = ListBox1.Items.Item(ItemIndex)
Catch ex As Exception
Exit Do
End Try
If currentMax > testItem Then
ListBox1.Items.Item(ItemIndex) = currentMax
ListBox1.Items.Item(ItemIndex - 1) = testItem
ItemsSwapped = True
My.Application.DoEvents()
End If
Label1.Text = "Il numero " + TextBox1.Text + " si fattorizza con :"
currentMax = ListBox1.Items.Item(ItemIndex)
BackgroundWorker1.ReportProgress(str)
Loop
Loop While ItemsSwapped
If TextBox1.Text Is String.Empty Then
Label1.Text = ""
ProgressBar1.Value = 0
Beep()
Else
BackgroundWorker1.ReportProgress(str)
End If
End Sub
Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick
If Me.ListBox1.Items.Count > 0 Then
Dim sfd As New SaveFileDialog
sfd.Filter = "File di testo(*.txt)|*.txt"
sfd.FileName = "Divisori"
sfd.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.Desktop
If sfd.ShowDialog() = DialogResult.OK Then
Dim output As New IO.StreamWriter(sfd.FileName)
output.WriteLine("I divisori del numero " + TextBox1.Text + " sono: ")
For Each item As Object In ListBox1.Items
output.WriteLine(ListBox1.GetItemText(item))
Next item
output.Close()
End If
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class