Saltar al contenido
Underc0de /foro

[VB6] Collection of Useful Code

🌐 This is an automatic translation of a topic from Underc0de, the largest Spanish-speaking hacking and IT community. Read the original (Spanish) and reply →
Slore ·
In order to provide source code to the forum's programmers, I'm creating this post, where we'll be posting only source code in Visual Basic 6.<br />If anyone has some, just post it.<br />If possible, in case the code is a bit confusing or long, add a brief description of what the code does, and remember to edit the title with the name of the code.<br /><br />Sources and credits: Forosdelweb - Recursosvisualbasic<br /><br />Regards to everyone. <br /><br />Copy Paste of a post by antrax on a sister forum, I hope he doesn't mind :P
Slore ·
Include this code in the program so that it runs only once<br /><br />
Código: vb
Private Sub Form_Load()<br />Dim Ya_Existe As Integer<br />Ya_Existe = App.PrevInstance<br />If Ya_Existe <> 0 Then<br />MsgBox "El Programa ya se esta ejecutando", 0 + 48, "News"<br />End<br />End If<br />End Sub
Slore ·
How to use Random in a program<br /><br />Code:<br />
Código: text
Private Sub Form_Load()<br />  Dim Num As Double<br />  Randomize<br />  Num = Rnd<br />  MsgBox Num<br />End Sub
Slore ·
Find out which directory my application is running from<br />Code:<br /> 
Código: text
Private Sub Form_Load()<br /> Dim Directorio as String<br /> ChDir App.Path<br /> ChDrive App.Path<br /> Directorio = App.Path<br /> If Len(Directorio) > 3 Then<br /> Directorio = Directorio & "\"<br /> End If<br /> End Sub
Slore ·
How to check whether a file exists<br />Code:<br />
Código: text
Public Sub  VerificarFichero(sNombreFichero As String)<br />On Error Resume Next<br />Open sNombreFichero For Input As #1<br />If Err Then<br />MsgBox ("El fichero " & sNombreFichero & " no existe.")<br />Exit Sub<br />End If<br />Close #1<br />End Sub
<br /><br /><br />in a button:<br /><br />VerificarFichero "c:\prueba.txt"<br /><br />Code:<br />
Código: text
Dim Archivo As String<br />Archivo = "C:\MiTexto.txt"<br />If Dir(Archivo, vbArchive) = "" Then<br />MsgBox "El Fichero No Existe"<br />End If
Slore ·
How to allow only numbers in a text field<br />Code:<br />
Código: text
  If ((KeyAscii < 48 Or KeyAscii > 57) And (KeyAscii < 44 Or KeyAscii > 44)) Then<br />       If (KeyAscii <> 8) Then KeyAscii = 0<br />    End If
Slore ·
How to reverse a text<br />Let's imagine that, for whatever reason, we want to reverse the order of the characters in a text. Let's imagine we have the text in a variable called Texto and we store the contents of the text string reversed in the variable Otxet. For example: if we have the text Casa we will get asaC.<br /><br />To do this we should write the following code:<br /><br />Code:<br /> <br />
Código: text
For Contador = Len(Texto) To 1 Step -1<br /><br />Otxet = Otxet & Mid (Texto, Contador, 1) <br />Next Contador
Slore ·
How to move from one text box to another using Enter<br />Insert three TextBoxes and write the following code:<br /><br />Code:<br />
Código: text
Private Sub Text1_KeyPress(KeyAscii As Integer)<br />If KeyAscii = 13 Then<br />SendKeys "{tab}"<br />KeyAscii = 0<br />End If<br />End Sub<br />Private Sub Text2_KeyPress(KeyAscii As Integer)<br />If KeyAscii = 13 Then<br />SendKeys "{tab}"<br />KeyAscii = 0<br />End If<br />End Sub<br />Private Sub Text3_KeyPress(KeyAscii As Integer)<br />If KeyAscii = 13 Then<br />SendKeys "{tab}"<br />KeyAscii = 0<br />End If<br />End Sub 
<br /><br /><br />another way:<br />Insert three TextBoxes, set the form's KeyPreview property to True and write the following code:<br /><br />Code:<br />
Código: text
Private Sub Form_KeyPress(KeyAscii As Integer)<br />If KeyAscii = 13 Then<br />SendKeys "{tab}"<br />KeyAscii = 0<br />End If<br />End Sub
Slore ·
Using IF THEN ELSE ENDIF on a single line<br />Insert a CommandButton and a TextBox and write the following code:<br /><br />Code:<br />Private Sub Command1_Click()<br />Dim I As Integer<br />Dim A As String<br />I = 3<br />A = IIf(I <> 1, "True", "False")<br />Text1.Text = A<br />End Sub<br /><br /><br />A variant of the same code:<br />Insert a CommandButton and a TextBox and write the following code:<br /><br />Code:<br /> Private Sub Command1_Click()<br /> Dim I As Integer<br /> Dim A As String<br /> I = 3<br />If I <> 1 Then A = "True" Else A = "False"<br /> Text1.Text = A<br /> End Sub
Slore ·
Convert a text to uppercase or lowercase<br />Code:<br /> Create a form and place a TextBox. Write:<br />Private Sub Text1_Change()<br />Dim I As Integer<br />Text1.Text = UCase(Text1.Text)<br />I = Len(Text1.Text)<br />Text1.SelStart = I<br />End Sub
Slore ·
Shut down the computer, restart Windows, restart the System<br />Add three buttons to a form and write the following in the form's code:<br /><br />Code:<br /> Private Declare Function ExitWindowsEx& Lib "user32" (ByVal<br />uFlags&, ByVal dwReserved&)<br />Private Sub Command1_Click()<br />Dim i as integer<br />i = ExitWindowsEx(1, 0&) 'Apaga el equipo<br />End Sub<br />Private Sub Command2_Click()<br />Dim i as integer<br />i = ExitWindowsEx(0, 0&) 'Reinicia Windows con nuevo usuario<br />End Sub<br />Private Sub Command3_Click()<br />Dim i as integer<br />i = ExitWindowsEx(2, 0&) 'Reinicia el Sistema<br />End Sub<br />
Grupo de WhatsApp