r/visualbasic • • Jan 07 '19

Is it possible to have multiple picture boxes on screen at once moving at different speeds?

6 Upvotes

So I'm a student in grade 11 and in computer science, I've been working on a falling fruit game where fruit falls from the top and the user catches them in a basket. I want to have 2-3 fruit spawn at the same time but fall at different, constant speeds (or else it would be impossible for the user to collect them all). So for example banana 1 could be moving down 2 pixels per tick meanwhile banana 4 could be moving down 1 pixel per tick, meanwhile strawberry 1 could be moving down 2.5 pixels per tick, etc. Here is the main code I wrote, with some commented lines. The double comments ('') are to just help understand what I mean and what I'm going for. If you have any questions, want the full code, etc feel free to ask :)

Public Sub timerFruitSpawn_Tick(sender As Object, e As EventArgs) Handles timerFruitSpawn.Tick

        x += 1 'x as integer


        For count As Integer = x To 3 'spawns 3 fruits at once

            Randomize()
            fruitKind = 5 * Rnd() 'dim fruitKind as integer, determines the type of fruit that will spawn 
            fruitSpawnPos = 925 * Rnd() 'Dim fruitSpawnPos as integer
            rndSpeed = 2.5 * Rnd() 'dim rndSpeed as Double



            Select Case fruitKind

                Case 0
                    fruitBanana(x) = New PictureBox
                    fruitBanana(x).Visible = False
                    fruitBanana(x).Image = My.Resources.bananna
                    fruitBanana(x).Location = New Point(fruitSpawnPos, 10)
                    fruitBanana(x).Width = 80
                    fruitBanana(x).Height = 80
                    fruitBanana(x).BackColor = Color.Transparent
                    Controls.Add(fruitBanana(x))
                    fruitBanana(x).Parent = picBackground
                    fruitBanana(x).Visible = True

                    ''fruitBanana(x).DropSpeed = rnd speed (Now ik .DropSpeed isnt a real thing which is where im having trouble creating something like that)

                    fruits.Add(fruitBanana(x)) 'Dim fruits As New List(Of PictureBox)

                    If timerFruitDrop.Enabled = False Then
                        timerFruitDrop.Enabled = True
                    End If

                Case 1
                    fruitCherry(x) = New PictureBox
                    fruitCherry(x).Visible = False
                    fruitCherry(x).Image = My.Resources.cherry
                    fruitCherry(x).Location = New Point(fruitSpawnPos, 10)
                    fruitCherry(x).Width = 80
                    fruitCherry(x).Height = 80
                    fruitCherry(x).BackColor = Color.Transparent
                    Controls.Add(fruitCherry(x))
                    fruitCherry(x).Parent = picBackground
                    fruitCherry(x).Visible = True

                    ''fruitCherry(x).DropSpeed = rndSpeed

                    fruits.Add(fruitCherry(x))

                    If timerFruitDrop.Enabled = False Then
                        timerFruitDrop.Enabled = True
                    End If

                Case 2
                    fruitGrapes(x) = New PictureBox
                    fruitGrapes(x).Visible = False
                    fruitGrapes(x).Image = My.Resources.grapes
                    fruitGrapes(x).Location = New Point(fruitSpawnPos, 10)
                    fruitGrapes(x).Width = 80
                    fruitGrapes(x).Height = 80
                    fruitGrapes(x).BackColor = Color.Transparent
                    Controls.Add(fruitGrapes(x))
                    fruitGrapes(x).Parent = picBackground
                    fruitGrapes(x).Visible = True

                    ''fruitGrapes(x).DropSpeed = rndSpeed

                    fruits.Add(fruitGrapes(x))

                    If timerFruitDrop.Enabled = False Then
                        timerFruitDrop.Enabled = True
                    End If

                Case 3
                    fruitOrange(x) = New PictureBox
                    fruitOrange(x).Visible = False
                    fruitOrange(x).Image = My.Resources.orange
                    fruitOrange(x).Location = New Point(fruitSpawnPos, 10)
                    fruitOrange(x).Width = 80
                    fruitOrange(x).Height = 80
                    fruitOrange(x).BackColor = Color.Transparent
                    Controls.Add(fruitOrange(x))
                    fruitOrange(x).Parent = picBackground
                    fruitOrange(x).Visible = True

                    ''fruitOrange(x).DropSpeed = rndSpeed

                    fruits.Add(fruitOrange(x))

                    If timerFruitDrop.Enabled = False Then
                        timerFruitDrop.Enabled = True
                    End If

                Case 4
                    fruitPear(x) = New PictureBox
                    fruitPear(x).Visible = False
                    fruitPear(x).Image = My.Resources.pear
                    fruitPear(x).Location = New Point(fruitSpawnPos, 10)
                    fruitPear(x).Width = 80
                    fruitPear(x).Height = 80
                    fruitPear(x).BackColor = Color.Transparent
                    Controls.Add(fruitPear(x))
                    fruitPear(x).Parent = picBackground
                    fruitPear(x).Visible = True

                    ''fruitPear(x).DropSpeed = rndSpeed

                    fruits.Add(fruitPear(x))

                    If timerFruitDrop.Enabled = False Then
                        timerFruitDrop.Enabled = True
                    End If

                Case 5
                    fruitStrawberry(x) = New PictureBox
                    fruitStrawberry(x).Visible = False
                    fruitStrawberry(x).Image = My.Resources.strawberry
                    fruitStrawberry(x).Location = New Point(fruitSpawnPos, 10)
                    fruitStrawberry(x).Width = 80
                    fruitStrawberry(x).Height = 80
                    fruitStrawberry(x).BackColor = Color.Transparent
                    Controls.Add(fruitStrawberry(x))
                    fruitStrawberry(x).Parent = picBackground
                    fruitStrawberry(x).Visible = True

                    ''fruitStrawberry(x).DropSpeed = rndSpeed

                    fruits.Add(fruitStrawberry(x))

                    If timerFruitDrop.Enabled = False Then
                        timerFruitDrop.Enabled = True
                    End If

            End Select

        Next


    End Sub


    Private Sub timerFruitDrop_Tick(sender As Object, e As EventArgs) Handles timerFruitDrop.Tick


        For Each fruit As PictureBox In fruits
            fruit.Location = New Point(fruit.Location.X, fruit.Location.Y + ''fruit.DropSpeed) ''I want this to make the certain fruit move at the constant random speed it was given when it was created, and not make all fruits move at the same speed
            
        Next
    
    End Sub

r/visualbasic • • Dec 03 '18

Open Source .NET – 4 years later

Thumbnail
mattwarren.org
6 Upvotes

r/visualbasic • • Dec 01 '18

My For and Do Loops keep freezing and nothing appears on the screen

6 Upvotes

I think the issues with the scrolling button appearing but not text it was working earlier fine but not so much. Although i am confused on to why they keep freezing. https://imgur.com/a/DgTjnfR

Public Class frmMain
    Dim i As Integer
    Private Sub btnDoLoop_Click(sender As Object, e As EventArgs) Handles btnDoLoop.Click
        ' Displays a multiplication table.
        Do Until i <= 9
            ListTable.Items.Add(i & " * " & Val(txtNumber.Text & " * " & i * Val(txtNumber.Text))
            i += 1
        Loop

    End Sub

    Private Sub btnForNext_Click(sender As Object, e As EventArgs) Handles btnForNext.Click
        ' Displays a multiplication table.
        For i = 1 To 9
            ListTable.Items.Add(i & " x " & txtNumber.Text & " = " & * CInt(txtNumber.Text))

        Next

    End Sub

r/visualbasic • • Nov 13 '18

Visual Basic in .NET Core 3.0

Thumbnail
blogs.msdn.microsoft.com
6 Upvotes

r/visualbasic • • Sep 28 '18

What is this thing and how do I make it happen in Visual Basic?

6 Upvotes

Please help! I'm having a hard time identifying what this thing at the bottom of the application is. Does anyone know what it is and how I create it?


r/visualbasic • • Aug 03 '18

Help: Learning Visual Basic in Excel

6 Upvotes

Good day, anyone of you here have any recommended books or videos that helps me in learning visual basic in excel.
Purpose of wanting to learn: I need it in my work ( I mostly work with excel), and I want to learn more. There are things i want to do in excel that is not possible with common/basic excel functions (can only be done with visual basic) and i want to create my own programs in excel that will help me improve my work (and possibly be promoted, yey)
Level of VB understanding: 5/10, we had VB in highschool but i forgot most of the ideas, but I can remember them (needs refresher)

I highly appreciate your responses and I thank you in advance.
If you have any recommendations not related to VB but can help me in my work, that is highly appreciated as well.


r/visualbasic • • Jun 15 '18

Trying to clean up my code by creating the objects of SAP in it's own sub?

6 Upvotes

I have dozens of small functions, and before each I have to do something like the code below. Is it possible to write this only once somewhere in my code and call it every time I need it?

If Not IsObject(SAPapplication) Then
   Set SapGuiAuto = GetObject("SAPGUI")
   Set SAPapplication = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
   Set Connection = SAPapplication.Children(0)
End If
If Not IsObject(Session) Then
   Set Session = Connection.Children(0)
End If
If IsObject(WScript) Then
  WScript.ConnectObject Session, "on"
   WScript.ConnectObject SAPapplication, "on"
End If

r/visualbasic • • May 19 '18

[VB6] Learn VB6 in 2018

6 Upvotes

This may sound crazy, but is it worthwhile to learn VB6 in 2018? My primary interest is to customize my computer to do things and that means programming the Office suite with VBA, but I'd also like a general programming language I can use outside of the confines of Office without having to learn two new languages. In an ideal world, I would do Python and R and C#, but I have very limited time and want to focus on one. Thanks for your comments.


r/visualbasic • • May 03 '18

NET Core 2 and VB.Net introduction.

6 Upvotes

TLDR; Thinking about NET/ASP Core 2 and Visual Basic posts. Is that something people would like to see?

As a mod I have been looking for ways to contribute content to /r/visualbasic and not just answer questions.

I have been spending a bit of time learning how to implement Net Core, and ASP.Net Core in Visual Basic. I am going to do some posts about how to work with and convert existing applications to Net Core and ASP.Net Core. I have been working towards getting rid of Windows Server and MS SQL and moving towards a Nginx and Mysql environment, this will allow me to deploy my applications on any platform, anywhere. This will include Amazon Web Services, Azure, Any modern Linux Distribution and any flavour of Windows server. For those of you who missed it Linux is the main driver for the web... :p

I will also be looking at creating restful services and consuming them in a few different ways. The restful layer will allow for much more flexibility with mobile apps, as well as make it easier to abstract the interface. I will be abstracting interface from the backend and segmenting the application into business rules and data tier. This means the web interfaces could be done with Angular or raw HTML5, CSS and ECMAscript (We should not call it Javascript anymore Oracle is bitchy about it). The restful layer will allow for much more flexibility with mobile apps, as well as

Both NET/ASP Core are available with full source and are under the MIT license;

A short and simple permissive license with conditions only requiring preservation of copyright and license notices. Licensed works, modifications, and larger works may be distributed under different terms and without source code.

This does not mean a lot for you out there who just want to learn some net/asp core but it does mean that the underlying model is exposed for those of us that want to be able to fix or modify the code and understand why something is going sideways in our code.

I like to start with Documentation;

Design of the Visual Basic .NET programming language and runtime library.

Documentation for ASP.NET and ASP.NET Core

I will be using these documents, the asp.net msdn documentation, other blogs, Teleriks C# convertor to help make sense of some of the implementations, and the MVC specification.

To be up front with you there is very little in the way of examples for vb.net and core 2, I had a hell of a time getting it sorted out because of this lack of outside knowledge. Almost everything is C#, and to be frank if I wanted to work in C# I would go back to Java :D

To follow the posts I will be introducing over the next while you will need Visual Studio 2017 installed, with the .Net Core 2 development workload installed. If you don't know how to do this I would refer to this.

https://www.visualstudio.com/vs/support/selecting-workloads-visual-studio-2017/

You are looking to install the NET Core cross-platform development workload. This is done via the Visual Studio installer.

Once this is done you will need to install Net Core 2.1.0 Preview 2 (2018/04/10) via NuGet it has a pile of advantages. I upgraded early because of the compiler speed. It's stable, and what I will be using.

I don't have a blog or a site that would fit this kind of work so I am starting here to see where I go with it. For now it will be housed in posts and the VB wiki here on /r/visualbasic

So is this some content the community would like to see or should I do something else?


r/visualbasic • • Nov 17 '17

Published my first game on itch.io this afternoon. It's not much, but I'm pretty proud as a hobbyist gamedev.

Thumbnail
lordkevnar.itch.io
6 Upvotes

r/visualbasic • • Sep 29 '17

Trying to convert a 64-bit hexadecimal to it's Floating Point representation(double).

6 Upvotes

Hello All,

As the title says I am trying to write a function/sub that will convert a Hexadecimal to it's floating point representation.

Example Hex Value:40BCC4A4 Double representation:5.899003982543945

This website is doing the conversion I want: http://babbage.cs.qc.cuny.edu/IEEE-754.old/32bit.html

I have found an example where I can convert the hex value to it's single representation, but I need the additional decimal places.

My working single function looks like:

        Dim hexValue As String = "40BCC4A4"
        Dim iInputIndex As Integer = 0
        Dim iOutputIndex As Integer = 0
        Dim bArray(3) As Byte
        For iInputIndex = 0 To hexValue.Length - 1 Step 2
            bArray(iOutputIndex) = Byte.Parse(hexValue.Chars(iInputIndex) & hexValue.Chars(iInputIndex + 1), Globalization.NumberStyles.HexNumber)
            iOutputIndex += 1
        Next
        Array.Reverse(bArray)
        MsgBox(BitConverter.ToSingle(bArray, 0))

The output is: 5.899004, does anyone know how I can convert this to double so I can get more decimal precision?

Thanks


r/visualbasic • • Aug 14 '17

New for Visual Basic: .NET Standard Class Libraries and the dotnet CLI!

Thumbnail
blogs.msdn.microsoft.com
6 Upvotes

r/visualbasic • • Jul 25 '17

VB.NET error "Insufficient memory to continue the execution of the program"

6 Upvotes

Hi, I'm running a listbox that is pulling data from an excel worksheet. This works by copying each line item to another sheet (that code works) and then pulling that value into the listbox. I get the insufficient memory error on the following line:

lbxResults.Items.AddRange(GlobalVariables.oWb.Worksheets("sheet2").Range("B1:B" & lastrow, "A1:U21").Value)

This is basically some modified vba code, so that syntax may bot be correct for .NET. Just looking how to proceed to get this working.

Thanks in advance!


r/visualbasic • • Jun 11 '17

VB.NET Help How do I add 2 different variables entered in 2 different text boxes together?

6 Upvotes

I am making a calculator and I am trying to add together two variables that are entered into 2 separate text boxes and display the answer into another text box. When I enter the values for it to add togther, if the addition is in the sub where the 'Uv' variable is declared it will only display the 'Uv' variable and not add the Uv and Vv variables together. This is the same if I put the addition in the sub where 'Vv' is. If I make new sub then nothing happens.

Form 2:

 Public Class Form2
    Private Sub Next2_Click(sender As Object, e As EventArgs) Handles Next2.Click
    Form3.Show()
    Hide()
End Sub

Private Sub Back1_Click(sender As Object, e As EventArgs) Handles Back1.Click
    Form1.Show()
    Hide()
End Sub
Public V As Integer
Public Sub cbV_CheckedChanged(sender As Object, e As EventArgs) Handles cbV.CheckedChanged
    V = IIf(sender.checked, 1, 0)
End Sub

Public U As Integer
Public Sub cbU_CheckedChanged(sender As Object, e As EventArgs) Handles cbU.CheckedChanged
    U = IIf(sender.checked, 1, 0)
End Sub

Public A As Integer
Public Sub cbA_CheckedChanged(sender As Object, e As EventArgs) Handles cbA.CheckedChanged
    A = IIf(sender.checked, 1, 0)
End Sub

Public T As Integer
Public Sub cbT_CheckedChanged(sender As Object, e As EventArgs) Handles cbT.CheckedChanged
    T = IIf(sender.checked, 1, 0)
End Sub

Public S As Integer
Public Sub cbS_CheckedChanged(sender As Object, e As EventArgs) Handles cbS.CheckedChanged
    S = IIf(sender.checked, 1, 0)
End Sub

End Class

Form 3: Public Class Form3

Private Sub Calc_Click(sender As Object, e As EventArgs) Handles Calc.Click
    Form4.Show()
    Hide()
End Sub
Private Sub changeV(ByRef V As Integer)
    Form2.V = V
End Sub
Private Sub changeU(ByRef U As Integer)
    Form2.U = U
End Sub
Private Sub changeA(ByRef A As Integer)
    Form2.A = A
End Sub
Private Sub changeT(ByRef T As Integer)
    Form2.T = T
End Sub
Private Sub changeS(ByRef S As Integer)
    Form2.S = S
End Sub

Private Sub Back2_Click(sender As Object, e As EventArgs) Handles Back2.Click
    Form2.Show()
    Hide()
End Sub
Public Sub VvText_TextChanged(sender As Object, e As EventArgs) Handles VvText.TextChanged
    Dim Vv As Integer

    Vv = Integer.Parse(VvText.Text)
End Sub

Public Sub UvText_TextChanged(sender As Object, e As EventArgs) Handles UvText.TextChanged
    Dim Uv As Integer

    Uv = Integer.Parse(UvText.Text)

    Dim Vv As Integer

    If (Form2.V = 1) And (Form2.U = 1) Then
        Vvlabel.Text = Vv + Uv
    End If
End Sub

Private Sub TvText_TextChanged(sender As Object, e As EventArgs) Handles TvText.TextChanged
    'NOT COMPLETE YET
End Sub

Private Sub SvText_TextChanged(sender As Object, e As EventArgs) Handles SvText.TextChanged
    'NOT COMPLETE YET
End Sub

End Class


r/visualbasic • • Dec 18 '16

Article Life Without On Error Resume Next -- Visual Studio Magazine

Thumbnail
visualstudiomagazine.com
6 Upvotes

r/visualbasic • • Dec 04 '16

[vb2015] Arrays

6 Upvotes

Good day everyone,

I am working a multidimensional array but for some reason I cannot get the array to display all 20 (4x5) of its items into a textbox.

So far I have this...which displays the first line respectively.

    Dim Rate(,) As Double = {{1, 1.5, 1.65, 1.85}, {1.58, 2, 2.4, 3.05}, {1.71, 2.52, 3.1, 4},_
    2.04, 3.12, 4, 5.01}, {2.52, 3.75, 5.1, 7.25}}


    Dim weightindex, zoneindex, output As Double

    weightindex = ListBox1.SelectedIndex & " "
    zoneindex = ListBox2.SelectedIndex & " "

    output = Rate(weightindex, zoneindex)

    If output < 3 Then
        NRLabel.Text = "$" & (output + output * 0.5).ToString("n2")

    ElseIf output >= 3 Then
        NRLabel.Text = "$" & (output - output * 0.2).ToString("n2")
    End If

    For counter = 0 To 3

        TextBox2.Text &= "$" & Rate(counter, counter).ToString("n2") & " " & vbnewline
    Next

I know my loop should run from 0 to 19 but for some reason, this does not work out...I feel like I should make multiple loops but I believe that would defeat the purpose of using a loop to consolidate. Am I missing a nested loop?

The output should look like the pic on the right below (there is also sorting involved...but I'm trying to grasp that concept as well)...thank you for your time.

http://imgur.com/a/va7nT


r/visualbasic • • Nov 12 '16

Trying to code my first VB program......

6 Upvotes

.....and im just a little confused on what i need to do next. The goal for me is to display a name and number at random based on a referenced excel document. so first off i have the RNG coded and i can get it to display in a textbox the random number. now where i need a little push in the right direction on how to modify the rng code to read and rng based on the excel document. Note: I am using VB2015


r/visualbasic • • Sep 19 '16

[VB 2015] Looking for a better way to do this, Batch copy files to usb.

6 Upvotes

I wrote a simple program that lets you select 1 to 6 (USB) drives, enter their drive letters, and copy a folder's contents to the drives.

I --KNOW-- there is a better way to code the Execute sub. But I am a pretty novice coder and this is what my brain came up with.

I know it's not good to call form objects in a BackgroundWorker. So what is a better way to do that in this example? Also, I have a progressbar in form2, and I can't get it to update the progressbar from the backgroundworker. It won't update til the worker completes, then the bar fills up. Anyway, here is the main UI and the code:

This is Form1, simple enough to understand

Imports System.ComponentModel

Public Class Form1

    Public intDrives As Integer

    Private Declare Function SetVolumeLabel Lib "kernel32" Alias "SetVolumeLabelA" (ByVal lpRootPathName As String, ByVal lpVolumeName As String) As Long

    Private Function ChangeVolumeLabel(DriveLetter As String, NewDriveVolume As String) As Boolean
        'Example:
        'ChangeVolumeLabel("C:\", "CDrive")

        Dim lAns As Long

        On Error Resume Next
        lAns = SetVolumeLabel(DriveLetter, NewDriveVolume)
        ChangeVolumeLabel = (lAns <> 0)

    End Function

    Private Function DriveCheck(DriveLetter As String) As Boolean
        Try
            If IO.Directory.Exists(DriveLetter & ":\") Then
                Return True
            Else
                Return False
            End If
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private Sub Execute()
        Dim sourceDir As String = txtSourceDir.Text
        intDrives = cboNumDrives.SelectedIndex + 1

        Select Case cboNumDrives.SelectedIndex
            Case 0
                If txtDrive1.Text <> "" And DriveCheck(txtDrive1.Text) = True Then
                    My.Computer.FileSystem.CopyDirectory(sourceDir, String.Format("{0}:\", txtDrive1.Text), True)
                    If chkLabel.Checked Then
                        ChangeVolumeLabel(String.Format("{0}:\", txtDrive1.Text), txtLabel.Text)
                    End If
                Else
                    cmdExecute.Enabled = True
                    MsgBox("Drives entered incorrectly or not found.  Please try again.")
                    Exit Sub
                End If
            Case 1
                If txtDrive1.Text <> "" And txtDrive2.Text <> "" And DriveCheck(txtDrive1.Text) = True And DriveCheck(txtDrive2.Text) = True Then
                    My.Computer.FileSystem.CopyDirectory(sourceDir, String.Format("{0}:\", txtDrive1.Text), True)
                    My.Computer.FileSystem.CopyDirectory(sourceDir, String.Format("{0}:\", txtDrive2.Text), True)
                    If chkLabel.Checked Then
                        ChangeVolumeLabel(String.Format("{0}:\", txtDrive1.Text), txtLabel.Text)
                        ChangeVolumeLabel(String.Format("{0}:\", txtDrive2.Text), txtLabel.Text)
                    End If
                Else
                    cmdExecute.Enabled = True
                    MsgBox("Drives entered incorrectly or not found.  Please try again.")
                    Exit Sub
                End If
            Case 2
                If txtDrive1.Text <> "" And txtDrive2.Text <> "" And txtDrive3.Text <> "" And DriveCheck(txtDrive1.Text) = True And DriveCheck(txtDrive2.Text) = True _
                                        And DriveCheck(txtDrive3.Text) = True Then
                    Form2.Label1.Text = String.Format("Copying files to drive 3 of {0}...", cboNumDrives.SelectedValue.ToString)
                    My.Computer.FileSystem.CopyDirectory(sourceDir, String.Format("{0}:\", txtDrive1.Text), True)
                    My.Computer.FileSystem.CopyDirectory(sourceDir, String.Format("{0}:\", txtDrive2.Text), True)
                    My.Computer.FileSystem.CopyDirectory(sourceDir, String.Format("{0}:\", txtDrive3.Text), True)
                    If chkLabel.Checked Then
                        ChangeVolumeLabel(String.Format("{0}:\", txtDrive1.Text), txtLabel.Text)
                        ChangeVolumeLabel(String.Format("{0}:\", txtDrive2.Text), txtLabel.Text)
                        ChangeVolumeLabel(String.Format("{0}:\", txtDrive3.Text), txtLabel.Text)
                    End If
                Else
                    cmdExecute.Enabled = True
                    MsgBox("Drives entered incorrectly or not found.  Please try again.")
                    Exit Sub
                End If
            Case 3
                If txtDrive1.Text <> "" And txtDrive2.Text <> "" And txtDrive3.Text <> "" And txtDrive4.Text <> "" And DriveCheck(txtDrive1.Text) = True _
                                        And DriveCheck(txtDrive2.Text) = True And DriveCheck(txtDrive3.Text) = True And DriveCheck(txtDrive4.Text) = True Then
                    Form2.Label1.Text = String.Format("Copying files to drive 4 of {0}...", cboNumDrives.SelectedValue.ToString)
                    My.Computer.FileSystem.CopyDirectory(sourceDir, String.Format("{0}:\", txtDrive1.Text), True)
                    My.Computer.FileSystem.CopyDirectory(sourceDir, String.Format("{0}:\", txtDrive2.Text), True)
                    My.Computer.FileSystem.CopyDirectory(sourceDir, String.Format("{0}:\", txtDrive3.Text), True)
                    My.Computer.FileSystem.CopyDirectory(sourceDir, String.Format("{0}:\", txtDrive4.Text), True)
                    If chkLabel.Checked Then
                        ChangeVolumeLabel(String.Format("{0}:\", txtDrive1.Text), txtLabel.Text)
                        ChangeVolumeLabel(String.Format("{0}:\", txtDrive2.Text), txtLabel.Text)
                        ChangeVolumeLabel(String.Format("{0}:\", txtDrive3.Text), txtLabel.Text)
                        ChangeVolumeLabel(String.Format("{0}:\", txtDrive4.Text), txtLabel.Text)
                    End If
                Else
                    cmdExecute.Enabled = True
                    MsgBox("Drives entered incorrectly or not found.  Please try again.")
                    Exit Sub
                End If
            Case 4
                If txtDrive1.Text <> "" And txtDrive2.Text <> "" And txtDrive3.Text <> "" And txtDrive4.Text <> "" And txtDrive5.Text <> "" _
                                        And DriveCheck(txtDrive1.Text) = True And DriveCheck(txtDrive2.Text) = True And DriveCheck(txtDrive3.Text) = True _
                                        And DriveCheck(txtDrive4.Text) = True And DriveCheck(txtDrive5.Text) = True Then
                    Form2.Label1.Text = String.Format("Copying files to drive 5 of {0}...", cboNumDrives.SelectedValue.ToString)
                    My.Computer.FileSystem.CopyDirectory(sourceDir, String.Format("{0}:\", txtDrive1.Text), True)
                    My.Computer.FileSystem.CopyDirectory(sourceDir, String.Format("{0}:\", txtDrive2.Text), True)
                    My.Computer.FileSystem.CopyDirectory(sourceDir, String.Format("{0}:\", txtDrive3.Text), True)
                    My.Computer.FileSystem.CopyDirectory(sourceDir, String.Format("{0}:\", txtDrive4.Text), True)
                    My.Computer.FileSystem.CopyDirectory(sourceDir, String.Format("{0}:\", txtDrive5.Text), True)
                    If chkLabel.Checked Then
                        ChangeVolumeLabel(String.Format("{0}:\", txtDrive1.Text), txtLabel.Text)
                        ChangeVolumeLabel(String.Format("{0}:\", txtDrive2.Text), txtLabel.Text)
                        ChangeVolumeLabel(String.Format("{0}:\", txtDrive3.Text), txtLabel.Text)
                        ChangeVolumeLabel(String.Format("{0}:\", txtDrive4.Text), txtLabel.Text)
                        ChangeVolumeLabel(String.Format("{0}:\", txtDrive5.Text), txtLabel.Text)
                    End If
                Else
                    cmdExecute.Enabled = True
                    MsgBox("Drives entered incorrectly or not found.  Please try again.")
                    Exit Sub
                End If
            Case 5
                If txtDrive1.Text <> "" And txtDrive2.Text <> "" And txtDrive3.Text <> "" And txtDrive4.Text <> "" And txtDrive5.Text <> "" _
                                        And txtDrive6.Text <> "" And DriveCheck(txtDrive1.Text) = True And DriveCheck(txtDrive2.Text) = True _
                                        And DriveCheck(txtDrive3.Text) = True And DriveCheck(txtDrive4.Text) = True And DriveCheck(txtDrive5.Text) = True _
                                        And DriveCheck(txtDrive6.Text) = True Then
                    Form2.Label1.Text = String.Format("Copying files to drive 6 of {0}...", cboNumDrives.SelectedValue.ToString)
                    My.Computer.FileSystem.CopyDirectory(sourceDir, String.Format("{0}:\", txtDrive1.Text), True)
                    My.Computer.FileSystem.CopyDirectory(sourceDir, String.Format("{0}:\", txtDrive2.Text), True)
                    My.Computer.FileSystem.CopyDirectory(sourceDir, String.Format("{0}:\", txtDrive3.Text), True)
                    My.Computer.FileSystem.CopyDirectory(sourceDir, String.Format("{0}:\", txtDrive4.Text), True)
                    My.Computer.FileSystem.CopyDirectory(sourceDir, String.Format("{0}:\", txtDrive5.Text), True)
                    My.Computer.FileSystem.CopyDirectory(sourceDir, String.Format("{0}:\", txtDrive6.Text), True)
                    If chkLabel.Checked Then
                        ChangeVolumeLabel(String.Format("{0}:\", txtDrive1.Text), txtLabel.Text)
                        ChangeVolumeLabel(String.Format("{0}:\", txtDrive2.Text), txtLabel.Text)
                        ChangeVolumeLabel(String.Format("{0}:\", txtDrive3.Text), txtLabel.Text)
                        ChangeVolumeLabel(String.Format("{0}:\", txtDrive4.Text), txtLabel.Text)
                        ChangeVolumeLabel(String.Format("{0}:\", txtDrive5.Text), txtLabel.Text)
                        ChangeVolumeLabel(String.Format("{0}:\", txtDrive6.Text), txtLabel.Text)
                    End If
                Else
                    cmdExecute.Enabled = True
                    MsgBox("Drives entered incorrectly or not found.  Please try again.")
                    Exit Sub
                End If
        End Select
    End Sub


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles cmdExecute.Click
        Label9.Visible = False
        If BackgroundWorker1.IsBusy Then Exit Sub

        If Not My.Computer.FileSystem.DirectoryExists(txtSourceDir.Text) Then
            cmdExecute.Enabled = True
            MsgBox("Please select a valid source directory.")
            Exit Sub
        End If

        cmdExecute.Enabled = False

        Dim form2 As New Form2
        form2.Label1.Text = String.Format("Copying files to {0} drive(s)...", cboNumDrives.SelectedIndex + 1)
        form2.Show()
        BackgroundWorker1.RunWorkerAsync()
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Control.CheckForIllegalCrossThreadCalls = False
        cboNumDrives.SelectedIndex = 0
        txtDrive1.Select()
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        If chkLabel.Checked Then
            txtLabel.Enabled = True
        Else
            txtLabel.Enabled = False
        End If
        Select Case cboNumDrives.SelectedIndex
            Case 0
                txtDrive1.Enabled = True
                txtDrive2.Enabled = False
                txtDrive3.Enabled = False
                txtDrive4.Enabled = False
                txtDrive5.Enabled = False
                txtDrive6.Enabled = False
            Case 1
                txtDrive1.Enabled = True
                txtDrive2.Enabled = True
                txtDrive3.Enabled = False
                txtDrive4.Enabled = False
                txtDrive5.Enabled = False
                txtDrive6.Enabled = False
            Case 2
                txtDrive1.Enabled = True
                txtDrive2.Enabled = True
                txtDrive3.Enabled = True
                txtDrive4.Enabled = False
                txtDrive5.Enabled = False
                txtDrive6.Enabled = False
            Case 3
                txtDrive1.Enabled = True
                txtDrive2.Enabled = True
                txtDrive3.Enabled = True
                txtDrive4.Enabled = True
                txtDrive5.Enabled = False
                txtDrive6.Enabled = False
            Case 4
                txtDrive1.Enabled = True
                txtDrive2.Enabled = True
                txtDrive3.Enabled = True
                txtDrive4.Enabled = True
                txtDrive5.Enabled = True
                txtDrive6.Enabled = False
            Case 5
                txtDrive1.Enabled = True
                txtDrive2.Enabled = True
                txtDrive3.Enabled = True
                txtDrive4.Enabled = True
                txtDrive6.Enabled = True
        End Select
    End Sub

    Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles btnSelectSource.Click
        Dim fbd As New FolderBrowserDialog

        'write new pat1h to IID database to line 1 of settings.ini
        If fbd.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            txtSourceDir.Text = fbd.SelectedPath
        Else
            Return
        End If
    End Sub
    Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        Call Execute()
    End Sub

    Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        cmdExecute.Enabled = True
    End Sub
End Class

r/visualbasic • • Aug 13 '16

[VBA] Easy tutorial: fractals in only 16 lines of code

Thumbnail
slicker.me
6 Upvotes

r/visualbasic • • Feb 24 '16

I need to generate 3 random numbers within a range (0<X<10), and assign those numbers to variables, all inside a sub-procedure. Every attempt at researching "making a random number" brings me to generating a number by pressing a button. Can anyone help me with this problem?

6 Upvotes

This is the full text of my problem, for added context.

Triangle inequality theorem states that the sum of two sides of a triangle must be greater than the third side. If this is true for all three combinations, then you will have a valid triangle.

Write a VB application that will randomly generate the sides of 5 triangles and calculate the perimeter and area of each triangle. When the user clicks btnTrianglesbutton, it should complete the following tasks:

(a)Call getTriangleSides(...) sub-procedure within click event procedure of btnTriangles. This procedure will randomly generate three sides of a triangle.Each side of a triangle must be greater than zero and less than 10.

(b)Call getProperties(...) sub-procedure within click event procedure of btnTriangles. This procedure will calculate the perimeter and area of the triangle.

(c)Display sides of 5 triangles with perimeter and area in tabular format within click event procedure of btnTriangles. Format numbers to one decimal place.Add clear button in your application.


r/visualbasic • • Feb 01 '16

[VB2015] User Input Loop question

6 Upvotes

Hello I am working on an visual basic assignment. For what its worth I got it to work/filled the requirements for it but I want to try to get it to work in a loop. So I did a do while loop. And that works as well...but can't exit out of the loop leading me into a infinite loop. Is there anyway for me to exit out properly?

Code pasted here: http://paste.ofcode.org/6Qn5ViBt33442Fwum2CRyZ


r/visualbasic • • Jan 23 '16

DynamicNumbers

6 Upvotes

I only finished a third of the requirements. I need to make a findLowest function that returns the index of the lowest value of the arrayusing the code that I created. I also need to make a sort() procedure that repeateldy finds the lowest value in an array A, removes it and adds it to array T. When all the values of A have been moved, the elements of T are copied to A wth an assginment statement and a loop.. AddItem() and RemoveItem() is a hint. I only completed the part where it says to create an array of random numbers in a listbox. Here is what I finished so far:

Private Sub btnCreate_Click(sender As Object, e As EventArgs) Handles btnCreate.Click
    Dim number(txtBox.Text)
    Dim rand As New Random
    For count As Integer = 0 To txtBox.Text - 1
        RandomNumbers.Items.Add(count & vbTab & rand.Next(1, 99))
        number(count) = RandomNumbers.Items(count)
    Next count
End Sub

Can you guys tell me how to complete this program? I'm a beginner for visual basic programming.


r/visualbasic • • Jan 01 '16

[VB2013] Why do enjoy/how did you start with Visual Basic?

6 Upvotes

There was only about a four or five-year period early on in my career in which I was developing in Visual Basic on a daily basis. I started with VB6 and ended with .Net2003. At this time, if one was developing for Windows systems, enterprise systems would be written in VB6 or even FoxPro or Access for smaller systems. I think C++ was considered overkill for in-house applications, so I stuck to VB and Access.

For the past ten years, I've really only coded in SQL and MDX on a daily basis, but I still enjoy coming back to VB and writing something every now and then - either for a personal project or a quick application at work. Granted, the scale of my VB applications are much smaller these days, but I don't feel out of place in the IDE or with the language when using newer versions. I think that VB has become even better to work with as the years have gone on - in 2002, it was very controversial that the .Net framework was a 20MB requirement, dwarfing the old VB6 runtime. There were still users and companies on dial-up or 256K connections. Now, every Windows PC has the .Net framework as standard, the Express versions of Visual Studio can use Git, and the object-oriented features of VB are very mature. I’m glad that Microsoft hasn’t let VB go the way of FoxPro.

It seems like there’s been a shift over the past few years for more openness back towards scripting and plainer-English languages. Though Visual Basic isn’t a scripting language, it’s pretty easy to get something developed and distributed. I’ve been learning more R and Python these days, but I still think that VB has a solid case for use in years to come, and hope that others give it a look.


r/visualbasic • • Aug 07 '15

Writing clean code - simplify your code.

6 Upvotes

I was handed an application recently from a developer who is leaving the company and it contained many oracle functions like the following;

Protected Sub SaveAdjustment_Names(ByVal sadjnameid As String, ByVal sADj_Name As String, ByVal sComments As String, ByVal sEntered_BY As String, ByVal sADJ_STATUS As String, ByVal sACTION As String)
    'TODO:   refactor this
    Try
        Dim sconstr As New AppFunctions
        Dim oracon As New OracleConnection(sconstr.sConstring)
        Dim cmdOra As New OracleCommand
        cmdOra.CommandType = CommandType.StoredProcedure
        cmdOra.CommandText = "WAP.AP_ADJUST_NAME_CHANGE"  '
        cmdOra.Connection = oracon
        Dim pavtid As New OracleParameter
        pavtid.OracleDbType = OracleDbType.Varchar2
        pavtid.ParameterName = "SADJNAMEID"
        pavtid.Direction = ParameterDirection.Input
        pavtid.Value = Trim(sadjnameid)

        Dim pavtType As New OracleParameter
        pavtType.OracleDbType = OracleDbType.Varchar2
        pavtType.ParameterName = "SADJ_NAME"
        pavtType.Direction = ParameterDirection.Input
        pavtType.Value = Trim(sADj_Name)

        Dim pavtCode As New OracleParameter
        pavtCode.OracleDbType = OracleDbType.Varchar2
        pavtCode.ParameterName = "SCOMMENTS"
        pavtCode.Direction = ParameterDirection.Input
        pavtCode.Value = Trim(sComments)

        Dim pavtSecCode As New OracleParameter
        pavtSecCode.OracleDbType = OracleDbType.Varchar2
        pavtSecCode.ParameterName = "SENTERED_BY"
        pavtSecCode.Direction = ParameterDirection.Input
        pavtSecCode.Value = sEntered_BY 'Trim(Me.T_avtcode.Text.ToString)

        Dim pavtDesc As New OracleParameter
        pavtDesc.OracleDbType = OracleDbType.Varchar2
        pavtDesc.ParameterName = "SADJ_STATUS"
        pavtDesc.Direction = ParameterDirection.Input
        pavtDesc.Value = Trim(sADJ_STATUS)

        Dim pAction As New OracleParameter
        pAction.OracleDbType = OracleDbType.Varchar2
        pAction.ParameterName = "SACTION"
        pAction.Direction = ParameterDirection.Input
        pAction.Value = Trim(sACTION)

        cmdOra.Parameters.Add(pavtid)
        cmdOra.Parameters.Add(pavtType)
        cmdOra.Parameters.Add(pavtCode)
        cmdOra.Parameters.Add(pavtSecCode)
        cmdOra.Parameters.Add(pavtDesc)
        cmdOra.Parameters.Add(pAction)

        cmdOra.Connection.Open()
        cmdOra.ExecuteNonQuery()
        cmdOra.Connection.Close()
    Catch ex As Exception
        Session("Error") = ex
        Response.Redirect("ErrorInfo.aspx")
    End Try

End Sub

There is nothing wrong with this code. It works and is easy to understand.

When using clean code principals we can reduce this to something easier to understand. The first thing I noticed when I looked at this function was that there were 6 identical chunks of code that created an oracle parameter. The rule of thumb for clean code is

"If a block of code appears more than once, break it out to a function"

Following this rule I created this function.

Public Function CreateOracleParameter(paramName As String) As OracleParameter
    Dim opParm As New OracleParameter
    opParm.OracleDbType = OracleDbType.NVarchar2
    opParm.ParameterName = Trim(paramName.ToUpper)
    opParm.Direction = ParameterDirection.Input
    opParm.Value = Trim(paramName)
    Return opParm
End Function

Which changed the intial function to;

Protected Sub SaveAdjustment_Names(ByVal sadjnameid As String, ByVal sADj_Name As String, ByVal sComments As String, ByVal sEntered_BY As String, ByVal sADJ_STATUS As String, ByVal sACTION As String)

    Try
        Dim sconstr As New AppFunctions
        Dim oracon As New OracleConnection(sconstr.getConnectionString)

        Dim cmdOra As New OracleCommand
        cmdOra.CommandType = CommandType.StoredProcedure
        cmdOra.CommandText = "WAP.AP_ADJUST_NAME_CHANGE"  '
        cmdOra.Connection = oracon

        cmdOra.Parameters.Add(CreateOracleParameter(sadjnameid))
        cmdOra.Parameters.Add(CreateOracleParameter(sADj_Name))
        cmdOra.Parameters.Add(CreateOracleParameter(sComments))
        cmdOra.Parameters.Add(CreateOracleParameter(sEntered_BY))
        cmdOra.Parameters.Add(CreateOracleParameter(sADJ_STATUS))
        cmdOra.Parameters.Add(CreateOracleParameter(sACTION))

        cmdOra.Connection.Open()
        cmdOra.ExecuteNonQuery()
        cmdOra.Connection.Close()

    Catch ex As Exception
        Session("Error") = ex
        Response.Redirect("ErrorInfo.aspx")
    End Try

End Sub

As you can see much easier to read and thus easier to maintain and trace issues. In this specific case I was able to refactor 168 calls for parameters and reduced the code base by over 20 percent.

Remember there is no wrong way to program, only better ways.

Improve your craft one method at a time.

B


r/visualbasic • • May 26 '15

Sort [VB.NET] Failed implementation of quicksort

6 Upvotes

Hey guys, can someone tell me why my quicksort doesn't work? It's an implementation of the pseudocode found at wiki and, as far as I can figure out, everything seems to be correct. I even followed it on paper and, as far as I can see, the logic is right: but the results I end up getting are far from sorted.

http://pastebin.com/766uDJGK

Appreciate any help!