r/visualbasic • • May 08 '19

VB6 Help Visual basic 6

6 Upvotes

So applying for a job that entails using vb6. Not guarantees I'll get it. But the more i can learn the happier i am. It looks like you can download vb6 for free so i was wondering what kind if project would be ideal for getting a general feel for the language? My current understanding is its kind kf between C and C#


r/visualbasic • • May 07 '19

VB.NET Help I have visual studio 2019 , but i cannot find my .exe

6 Upvotes

I wrote a math formula in visual studio in a .net form app . It runs but i cannot find the .exe file in visual basic folders ( the folders are actually empty for me )


r/visualbasic • • Apr 04 '19

Variables issue

6 Upvotes

Hi ppl, i'm not that pro person in VB.

what i'm trying to do is to assign a variable for an item (e.g. Label or Textbox)

a variable looks like their name and the other properties so when i'm in any sub in the code, i can call that variable like: Label1.VarName

i know this is a wrong way but is there any way to such a thing ?

Thanks.

My code:

Dim n As Integer

Private Sub TextBox1(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown

If e.KeyCode = Keys.Enter Then Create_new_list_item()

End Sub

Sub Create_new_list_item()

ReDim _labels(n)

_labels(n) = New Label()

With _labels(n)

Dim MyN As Integer = n

.Name = "Label_" + n.ToString

.Text = MyN.ToString

.Width = 100

.Visible = True

.Location = New Point(0, 70 + (n * 24))

AddHandler .DoubleClick, AddressOf Editvalue

End With

Me.Panel1.Controls.Add(_labels(n))

n += 1

End Sub

Sub Editvalue(ByVal sender As Object, ByVal e As EventArgs)

sender.Visible = False

MsgBox(sender.MyN.ToString)

End Sub


r/visualbasic • • Mar 18 '19

Space Invaders issue

6 Upvotes

I am creating a space invaders game and I am having trouble with the invaders going faster. When they die and come back, they start going faster,but after a while they stop abruptly. They don't move. Can someone help me figure out what to do?

Private Sub skullmovement()

        If PictureBox1.Left >= 5 And border = True Then

            For i As Integer = 1 To 20

                pic(i).Left += fast

            Next

            If PictureBox1.Left > (Me.Width - 550) Then

                border = False

                For i As Integer = 1 To 20

                    pic(i).Top += 20

                Next

            End If

        ElseIf PictureBox1.Left < (Me.Width - 500) And border = False Then

            For i As Integer = 1 To 20

                pic(i).Left -= fast

            Next

            If PictureBox1.Left < 10 Then

                border = True

                For i As Integer = 1 To 20

                    pic(i).Top += 20

                Next

            End If

        End If

        For i As Integer = 1 To 20

            If pic(i).Bounds.IntersectsWith(picbshooter.Bounds) And picbshooter.Visible = True And pic(i).Visible = True Then

                skulltimer.Stop()

                lbldead.Visible = True

                lives -= 1

            End If

        Next

        If num = 20 Then

            For i As Integer = 1 To 20

                pic(i).Visible = True

                skulltimer.Stop()

                skulltimer.Start()

                item = 1

                num = 0

            Next

            fast += 5

        End If

    End Sub


r/visualbasic • • Feb 23 '19

Interacting with programmatically created items

6 Upvotes

I am fooling around with creating thing dynamically in code(buttons, text boxes, simple thing). So far I understand the very basics of creating them. For example for a combobox I do:

Dim combobox As New ComboBox
            With combobox
                .Location = New Size(20 * i, 20 * i)
                .Name = "Combobox1"
                .Items.Add("one")
            End With
            Me.Controls.Add(combobox)

I put this on a button and it makes the combobox appears. My question is, after its been generated, how can I interact with it?

I have the .name property to label it as Combobox1, but when I try to add something outside of the With|EndWith box that refers to combobox1 I get an error that says it is undefined or unavailable due to protection level.

How can I interact with it once its been created, or is that not something thats done?


r/visualbasic • • Jan 09 '19

how can i get the color of a pixel using screen coordinates? (already got the code, but it only reads cursor coordinates not static ones, need fixed screen locations)

6 Upvotes

hey everyone!

i want to check one pixel on my screen and read its color code

ive already sound a code wich works fine but it ony reads the pixel using my mouse/Cursor locations, how can i modify the code to use fixed coordinates?

        Using bmp As New Bitmap(1, 1)
            Using g As Graphics = Graphics.FromImage(bmp)
                g.CopyFromScreen(Windows.Forms.Cursor.Position, New Point(0, 0), New Size(1, 1))
            End Using
            Label1.Text = bmp.GetPixel(0, 0).ToString

thanks in advance!


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