Page 1 of 1

Simple but Neat Visual Basic Tips and Tricks Part 1

Posted: Tue Jan 01, 2013 2:15 am
by smashapps
Simple but Neat Visual Basic Tips and Tricks Part 1

This is a part of a series called Simple but Neat Visual Basic Tips and Tricks that will show lots of small code snippets that you may find useful. Or maybe you’re looking for something to experiment with. Not sure but you may find as well every now and then some of it could help you. Good especially if you’re new to Visual basic.

If you want to contribute feel free to PM me anything.

#1: Display List of Logical Drives in a ListBox control
Code: Select all
Imports System.IO
ListBox1.Items.AddRange(System.IO.Directory.GetLogicalDrives)
#2: Convert a String to Upper, Lower and Title case
Code: Select all
TextBox1.Text = TextBox1.Text.ToUpper TextBox1.Text = TextBox1.Text.ToLower TextBox1.Text = StrConv(TextBox1.Text, VbStrConv.ProperCase)
#3: Text to Speech
Code: Select all
Dim SAPI
SAPI = CreateObject("SAPI.spvoice")
SAPI.Speak(TextBox1.Text)
#4: Read one line of text from a file
Code: Select all
Dim fileReader As System.IO.StreamReader
fileReader =
My.Computer.FileSystem.OpenTextFileReader("C:\\testfile.txt")
Dim stringReader As String
stringReader = fileReader.ReadLine()
MsgBox("The first line of the file is " & stringReader)
#5: Read text from encrypted fields
Code: Select all
Dim fileReader As String
fileReader = My.Computer.FileSystem.ReadAllText(My.Computer.FileSystem.SpecialDirectories.Desktop & "\Document.txt",
System.Text.Encoding.UTF32)
TextBox1.Text = fileReader
#6: RGB Colours
Code: Select all
Color.FromArgb(155, 195, 231)
Bonus: Here are some of my favourite RGB colours. They look nice.
0 155 72
65 105 255
0 105 170
#7: Form Size
Code: Select all
Me.Size = New Size(0, 0)
#8: Minimize all forms at once
Code: Select all
For Each frm As Form in Application.OpenForms
frm.WindowState = FormWindowState.Minimized
Next frm
#9: Random Numbers
Code: Select all
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
Timer1.Start()
Randomize()
Dim val As Integer = CInt(Int((1000 * Rnd()) + 1))
TextBox1.Text += val & vbCrLf
End Sub
This one will generate a random number from the range 1000 to 1 and then add it to the textbox. Randomize is the initialization.

#10: InputBox with a question
Code: Select all
Public answer As Integer = 12
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim question, title As String 'declare our string variables to hold the question and title
Dim test As Object 'this is our object that will become the inputbox and display the question and title
question = "What is 2 + 10?" 'set the question
title = "Answer the question!" 'set the title
test = InputBox(question, title) 'display the input box
If test = answer Then 'test answer
MsgBox("Correct!")
Else
MsgBox("Incorrect! It was 12")
test = InputBox(question, title)
End If
End Sub
Here I created an answer which was set to 12. We set everything our InputBox needs, display it then check the users answer. If it is 12 it displayed a message saying correct, if it is wrong say it is incorrect then show the question again.

I will be expanding this and releasing Part 2 in a month or two. If it has helped or you like it please +rep.

You can download the PDF of this for just 3 small credits. I am low on credits and wouldn't mind building them up.

Thanks.


This file is hosted off-site.

Re: Simple but Neat Visual Basic Tips and Tricks Part 1

Posted: Tue Jan 01, 2013 2:20 am
by kolega28
Cool, interesting stuff, knew a lot of these already but still a very good post, +rep :)

Re: Simple but Neat Visual Basic Tips and Tricks Part 1

Posted: Tue Jan 01, 2013 2:22 am
by smashapps
Thanks kolega28.

The plan is to slowly add more and more complex stuff. This first part is aimed at a lot of beginners. I am sure a lot didn't know what I posted though.
I was originally going to have the first part include 25 different things but would of taken a lot longer. The second part might have at least 20. I am not sure yet though.

Re: Simple but Neat Visual Basic Tips and Tricks Part 1

Posted: Tue Jan 01, 2013 6:32 pm
by clanc789
Downloaded it for you :) Could have been a skid but since you need those credits: off you go!

Re: Simple but Neat Visual Basic Tips and Tricks Part 1

Posted: Tue Jan 01, 2013 7:58 pm
by comathi
Well worth 3 credits ;)

It's always nice to have these little snippets around, just in case. Great job!

Re: Simple but Neat Visual Basic Tips and Tricks Part 1

Posted: Sun Mar 10, 2013 6:18 pm
by noypikami
#smashapps

i'm looking for part 2, interesting post...! love it :mrgreen:

Re: Simple but Neat Visual Basic Tips and Tricks Part 1

Posted: Wed Mar 27, 2013 11:15 am
by smashapps
I ended up not doing it. If you didn't notice.

Sorry.