Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Numeri primi e divisori - Form1.vb

Form1.vb

Caricato da: Dark_light
Scarica il programma completo

  1. Public Class Form1
  2.     Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
  3.         Dim KeyAscii As Short = Asc(e.KeyChar)
  4.         If KeyAscii < 48 And KeyAscii <> 24 And KeyAscii <> 8 Then
  5.             KeyAscii = 0
  6.         ElseIf KeyAscii > 57 Then
  7.             KeyAscii = 0
  8.             MessageBox.Show("Consentiti solo numeri")
  9.         End If
  10.         If e.KeyChar = "0" And TextBox1.TextLength = 0 Then
  11.             KeyAscii = 0
  12.         End If
  13.         e.KeyChar = Chr(KeyAscii)
  14.     End Sub
  15.     Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
  16.         Dim str As String = TextBox1.Text
  17.         ProgressBar1.Step = 5
  18.         ProgressBar1.Minimum = 0
  19.         ProgressBar1.Maximum = str
  20.         ProgressBar1.Value = e.ProgressPercentage
  21.     End Sub
  22.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  23.         ListBox1.Items.Clear()
  24.         If Me.TextBox1.Text = "" Then
  25.             MessageBox.Show("inserisci numero")
  26.         End If
  27.  
  28.         Dim str As String = TextBox1.Text
  29.         Dim n As Integer
  30.         If Integer.TryParse(str, n) Then
  31.             For x As Integer = 2 To n / 2
  32.                 If n Mod x = 0 Then
  33.                     ListBox1.Items.Add(CInt(n \ x))
  34.                     If ProgressBar1.Value = ProgressBar1.Maximum Then
  35.                         ProgressBar1.Value = ProgressBar1.Minimum
  36.                     Else
  37.                         BackgroundWorker1.ReportProgress(x)
  38.                     End If
  39.                 End If
  40.             Next
  41.         End If
  42.         If ListBox1.Items.Count = 0 Then
  43.             Label1.Text = "Il Numero è Primo"
  44.         Else
  45.             Label1.Text = "Il Numero non è Primo. I divisori sono:"
  46.         End If
  47.         Dim currentMax As Integer
  48.         Dim testItem As Integer
  49.         Dim ItemIndex As Integer
  50.         Dim ItemsSwapped As Boolean
  51.  
  52.         Do
  53.             Try
  54.                 ItemIndex = 0
  55.                 ItemsSwapped = False
  56.                 currentMax = ListBox1.Items.Item(ItemIndex)
  57.  
  58.             Catch ex As Exception
  59.                 Exit Do
  60.             End Try
  61.             Do
  62.                 ItemIndex += 1
  63.                 Try
  64.                     testItem = ListBox1.Items.Item(ItemIndex)
  65.                 Catch ex As Exception
  66.                     Exit Do
  67.                 End Try
  68.                 If currentMax > testItem Then
  69.                     ListBox1.Items.Item(ItemIndex) = currentMax
  70.                     ListBox1.Items.Item(ItemIndex - 1) = testItem
  71.                     ItemsSwapped = True
  72.  
  73.                     My.Application.DoEvents()
  74.  
  75.                 End If
  76.                 Label1.Text = "Il numero " + TextBox1.Text + " si fattorizza con :"
  77.                 currentMax = ListBox1.Items.Item(ItemIndex)
  78.                 BackgroundWorker1.ReportProgress(str)
  79.             Loop
  80.         Loop While ItemsSwapped
  81.         If TextBox1.Text Is String.Empty Then
  82.             Label1.Text = ""
  83.             ProgressBar1.Value = 0
  84.             Beep()
  85.         Else
  86.             BackgroundWorker1.ReportProgress(str)
  87.         End If
  88.     End Sub
  89.  
  90.     Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick
  91.  
  92.         If Me.ListBox1.Items.Count > 0 Then
  93.  
  94.             Dim sfd As New SaveFileDialog
  95.             sfd.Filter = "File di testo(*.txt)|*.txt"
  96.             sfd.FileName = "Divisori"
  97.             sfd.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.Desktop
  98.  
  99.             If sfd.ShowDialog() = DialogResult.OK Then
  100.                 Dim output As New IO.StreamWriter(sfd.FileName)
  101.                 output.WriteLine("I divisori del numero " + TextBox1.Text + " sono: ")
  102.                 For Each item As Object In ListBox1.Items
  103.                     output.WriteLine(ListBox1.GetItemText(item))
  104.                 Next item
  105.                 output.Close()
  106.             End If
  107.         End If
  108.     End Sub
  109.  
  110.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  111.  
  112.     End Sub
  113. End Class