MVB3- Bucles con Visual Basic (VB.NET). For, For Each, While
Se trata de una pequeña aplicacion para tratar de ayudar a entender los principales Bucles en Visual Basic.
Codigo:
Fichero de Texto: DIAS.TXT (en carpeta Debug):
LUNES
MARTES
MIERCOLES
JUEVES
VIERNES
SABADO
DOMINGO
MARTES
MIERCOLES
JUEVES
VIERNES
SABADO
DOMINGO
Form1
Imports System.IO.File
Imports Microsoft.VisualBasic.FileIO
Imports Microsoft.VisualBasic.FileIO
Public Class Form1
Private Sub ButtonFOR_Click(sender As Object, e As EventArgs) Handles ButtonFOR.Click
TextBoxTEXTO1.Text = ""
TextBoxTEXTO1.Text = ""
For I = CDbl(TextBoxI1.Text) To CDbl(TextBoxTO1.Text) Step CDbl(TextBoxSTEP1.Text)
TextBoxTEXTO1.Text = TextBoxTEXTO1.Text & "LINEA : " & I & vbCrLf
Next
End Sub
Private Sub ButtonARRAY1_Click(sender As Object, e As EventArgs) Handles ButtonARRAY1.Click
TextBoxTEXTO1.Text = ""
Dim DIAS As String() = {"LUNES", "MARTES", "MIERCOLES", "JUEVES", "VIERNES", "SABADO", "DOMINGO"}
For I = 0 To DIAS.Length - 1 Step 1
TextBoxTEXTO1.Text = TextBoxTEXTO1.Text & DIAS(I) & vbCrLf
Next
End Sub
Private Sub ButtonSALIDA1_Click(sender As Object, e As EventArgs) Handles ButtonSALIDA1.Click
TextBoxTEXTO1.Text = ""
TextBoxTEXTO1.Text = TextBoxTEXTO1.Text & "LINEA : " & I & vbCrLf
Next
End Sub
Private Sub ButtonARRAY1_Click(sender As Object, e As EventArgs) Handles ButtonARRAY1.Click
TextBoxTEXTO1.Text = ""
Dim DIAS As String() = {"LUNES", "MARTES", "MIERCOLES", "JUEVES", "VIERNES", "SABADO", "DOMINGO"}
For I = 0 To DIAS.Length - 1 Step 1
TextBoxTEXTO1.Text = TextBoxTEXTO1.Text & DIAS(I) & vbCrLf
Next
End Sub
Private Sub ButtonSALIDA1_Click(sender As Object, e As EventArgs) Handles ButtonSALIDA1.Click
TextBoxTEXTO1.Text = ""
For I = CDbl(TextBoxI1.Text) To CDbl(TextBoxTO1.Text) Step CDbl(TextBoxSTEP1.Text)
If I <> CDbl(TextBoxSALIDA1.Text) Then
TextBoxTEXTO1.Text = TextBoxTEXTO1.Text & "LINEA : " & I & vbCrLf
Else
Exit For
End If
TextBoxTEXTO1.Text = TextBoxTEXTO1.Text & "LINEA : " & I & vbCrLf
Else
Exit For
End If
Next
End Sub
End Sub
Private Sub ButtonFOREACH_Click(sender As Object, e As EventArgs) Handles ButtonFOREACH.Click
TextBoxTEXTO2.Text = ""
Dim DIAS As String() = {"LUNES", "MARTES", "MIERCOLES", "JUEVES", "VIERNES", "SABADO", "DOMINGO"}
TextBoxTEXTO2.Text = ""
Dim DIAS As String() = {"LUNES", "MARTES", "MIERCOLES", "JUEVES", "VIERNES", "SABADO", "DOMINGO"}
For Each ELEMENTO In DIAS
If ELEMENTO.Contains(TextBoxEACH1.Text.ToUpper) Then
TextBoxTEXTO2.Text = TextBoxTEXTO2.Text & ELEMENTO & vbCrLf
End If
Next
End Sub
If ELEMENTO.Contains(TextBoxEACH1.Text.ToUpper) Then
TextBoxTEXTO2.Text = TextBoxTEXTO2.Text & ELEMENTO & vbCrLf
End If
Next
End Sub
Private Sub ButtonWHILE_Click(sender As Object, e As EventArgs) Handles ButtonWHILE.Click
TextBoxTEXTO3.Text = ""
Dim CONTADOR As Integer = 0
While CONTADOR <= CDbl(TextBoxWHILE.Text)
TextBoxTEXTO3.Text = TextBoxTEXTO3.Text & "CONTADOR: " & CONTADOR & vbCrLf
CONTADOR += 1
End While
TextBoxTEXTO3.Text = ""
Dim CONTADOR As Integer = 0
While CONTADOR <= CDbl(TextBoxWHILE.Text)
TextBoxTEXTO3.Text = TextBoxTEXTO3.Text & "CONTADOR: " & CONTADOR & vbCrLf
CONTADOR += 1
End While
End Sub
Private Sub ButtonSIG1_Click(sender As Object, e As EventArgs) Handles ButtonSIG1.Click
ButtonARRAY1.Visible = True
ButtonSIG4.Visible = True
ButtonARRAY1.Visible = True
ButtonSIG4.Visible = True
End Sub
Private Sub ButtonSIG4_Click(sender As Object, e As EventArgs) Handles ButtonSIG4.Click
ButtonSALIDA1.Visible = True
TextBoxSALIDA1.Visible = True
ButtonSIG2.Visible = True
End Sub
Private Sub ButtonSIG2_Click(sender As Object, e As EventArgs) Handles ButtonSIG2.Click
Private Sub ButtonSIG4_Click(sender As Object, e As EventArgs) Handles ButtonSIG4.Click
ButtonSALIDA1.Visible = True
TextBoxSALIDA1.Visible = True
ButtonSIG2.Visible = True
End Sub
Private Sub ButtonSIG2_Click(sender As Object, e As EventArgs) Handles ButtonSIG2.Click
ButtonFOREACH.Visible = True
Label4.Visible = True
TextBoxVARIABLE1.Visible = True
TextBoxTEXTO2.Visible = True
Label6.Visible = True
TextBoxEACH1.Visible = True
Label8.Visible = True
ButtonSIG3.Visible = True
Label7.Visible = True
End Sub
Label4.Visible = True
TextBoxVARIABLE1.Visible = True
TextBoxTEXTO2.Visible = True
Label6.Visible = True
TextBoxEACH1.Visible = True
Label8.Visible = True
ButtonSIG3.Visible = True
Label7.Visible = True
End Sub
Private Sub ButtonSIG3_Click(sender As Object, e As EventArgs) Handles ButtonSIG3.Click
ButtonWHILE.Visible = True
ButtonSIG5.Visible = True
Label9.Visible = True
TextBoxWHILE.Visible = True
TextBoxTEXTO3.Visible = True
Label10.Visible = True
Label11.Visible = True
Label12.Visible = True
End Sub
ButtonWHILE.Visible = True
ButtonSIG5.Visible = True
Label9.Visible = True
TextBoxWHILE.Visible = True
TextBoxTEXTO3.Visible = True
Label10.Visible = True
Label11.Visible = True
Label12.Visible = True
End Sub
Private Sub ButtonSIG5_Click(sender As Object, e As EventArgs) Handles ButtonSIG5.Click
ButtonEndOfData.Visible = True
Label14.Visible = True
End Sub
ButtonEndOfData.Visible = True
Label14.Visible = True
End Sub
Private Sub ButtonEndOfData_Click(sender As Object, e As EventArgs) Handles ButtonEndOfData.Click
TextBoxTEXTO3.Text = ""
TextBoxTEXTO3.Text = ""
Dim ARCHIVO As String = Application.StartupPath & "\DIAS.TXT"
Dim CAMPOS As String()
Dim DELIMITADOR As String = ","
Using ANALIZADOR As New TextFieldParser(ARCHIVO)
ANALIZADOR.SetDelimiters(DELIMITADOR)
While Not ANALIZADOR.EndOfData
' Read in the fields for the current line
CAMPOS = ANALIZADOR.ReadFields()
' Add code here to use data in fields variable.
For I = 0 To CAMPOS.Length - 1
TextBoxTEXTO3.Text = TextBoxTEXTO3.Text & CAMPOS(I) & vbCrLf
Next
End While
End Using
End Sub
End Class
Dim CAMPOS As String()
Dim DELIMITADOR As String = ","
Using ANALIZADOR As New TextFieldParser(ARCHIVO)
ANALIZADOR.SetDelimiters(DELIMITADOR)
While Not ANALIZADOR.EndOfData
' Read in the fields for the current line
CAMPOS = ANALIZADOR.ReadFields()
' Add code here to use data in fields variable.
For I = 0 To CAMPOS.Length - 1
TextBoxTEXTO3.Text = TextBoxTEXTO3.Text & CAMPOS(I) & vbCrLf
Next
End While
End Using
End Sub
End Class
MVB3- Condicionales en Visual Basic (VB.NET). If, Else, ElseIf, Select Case
Se trata de una pequeña aplicacion para tratar de ayudar a entender los condicionales en Visual Basic.
Codigo:
Form1
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBox4.Text = ""
Try
Dim A As Integer = CInt(TextBox2.Text)
Dim B As Integer = CInt(TextBox3.Text)
If A > B Then TextBox4.Text = A 'RES= A
Catch ex As Exception
MsgBox(ex.Message)
End Try
TextBox4.Text = ""
Try
Dim A As Integer = CInt(TextBox2.Text)
Dim B As Integer = CInt(TextBox3.Text)
If A > B Then TextBox4.Text = A 'RES= A
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
TextBox7.Text = ""
Try
Dim A As Integer = CInt(TextBox9.Text)
Dim B As Integer = CInt(TextBox8.Text)
TextBox7.Text = ""
Try
Dim A As Integer = CInt(TextBox9.Text)
Dim B As Integer = CInt(TextBox8.Text)
If A > B And A = 4 Then TextBox7.Text = A 'RES=A
Catch ex As Exception
MsgBox(ex.Message)
End Try
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
TextBox12.Text = ""
TextBox12.BackColor = Color.Black
Try
Dim A As Integer = CInt(TextBox14.Text)
Dim B As Integer = CInt(TextBox13.Text)
TextBox12.Text = ""
TextBox12.BackColor = Color.Black
Try
Dim A As Integer = CInt(TextBox14.Text)
Dim B As Integer = CInt(TextBox13.Text)
If A > B Then
TextBox12.Text = A 'RES=A
TextBox12.BackColor = Color.Blue 'RES.BACKCOLOR=AZUL
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
TextBox12.Text = A 'RES=A
TextBox12.BackColor = Color.Blue 'RES.BACKCOLOR=AZUL
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
TextBox11.Text = ""
Try
Dim A As Integer = CInt(TextBox17.Text)
Dim B As Integer = CInt(TextBox16.Text)
If A > B Then
TextBox11.Text = A 'RES=A
Else
TextBox11.Text = B 'RES=B
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
TextBox19.Text = ""
Try
Dim A As Integer = CInt(TextBox21.Text)
Dim B As Integer = CInt(TextBox20.Text)
If A > B Then
TextBox19.Text = A 'RES=A
ElseIf B > A Then
TextBox19.Text = B 'RES=B
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
TextBox23.BackColor = Color.Black
Try
Dim A As Integer = CInt(TextBox25.Text)
Select Case A
Case 0
TextBox23.BackColor = Color.Blue 'RES.BACKCOLOR=AZUL
Case 1
TextBox23.BackColor = Color.Red 'RES.BACKCOLOR=ROJO
Case Else
TextBox23.BackColor = Color.Green 'RES.BACKCOLOR=VERDE
End Select
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
TextBox6.Visible = True
TextBox7.Visible = True
TextBox8.Visible = True
TextBox9.Visible = True
TextBox10.Visible = True
Button2.Visible = True
Button8.Visible = True
End Sub
Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
TextBox12.Visible = True
TextBox13.Visible = True
TextBox14.Visible = True
TextBox15.Visible = True
Button3.Visible = True
Button9.Visible = True
End Sub
Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
TextBox11.Visible = True
TextBox16.Visible = True
TextBox17.Visible = True
TextBox18.Visible = True
Button4.Visible = True
Button10.Visible = True
End Sub
Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
TextBox19.Visible = True
TextBox20.Visible = True
TextBox21.Visible = True
TextBox22.Visible = True
Button5.Visible = True
Button11.Visible = True
End Sub
Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
TextBox23.Visible = True
TextBox25.Visible = True
TextBox26.Visible = True
Button6.Visible = True
End Sub
End Class
No hay comentarios:
Publicar un comentario