Simple but Neat Visual Basic Tips and Tricks Part 1

Use this board to post your code snippets - tips and tricks
7 posts Page 1 of 1
Contributors
User avatar
smashapps
Coding Guru
Coding Guru
Posts: 961
Joined: Tue Apr 05, 2011 8:41 am

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.
My name is Tom | Visit my blog where I post new content every day! Tom's Daily Blog | MineCraft is awesome!
User avatar
kolega28
VIP - Donator
VIP - Donator
Posts: 338
Joined: Mon Jan 17, 2011 7:12 pm

Cool, interesting stuff, knew a lot of these already but still a very good post, +rep :)
Image
User avatar
smashapps
Coding Guru
Coding Guru
Posts: 961
Joined: Tue Apr 05, 2011 8:41 am

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.
My name is Tom | Visit my blog where I post new content every day! Tom's Daily Blog | MineCraft is awesome!
User avatar
clanc789
Coding Guru
Coding Guru
Posts: 786
Joined: Tue Nov 02, 2010 4:45 pm

Downloaded it for you :) Could have been a skid but since you need those credits: off you go!
Practice makes perfect!

VIP since: 6-10-2011
User avatar
comathi
Coding God
Coding God
Posts: 1242
Joined: Fri Mar 26, 2010 1:59 pm

Well worth 3 credits ;)

It's always nice to have these little snippets around, just in case. Great job!
User avatar
noypikami
VIP - Donator
VIP - Donator
Posts: 151
Joined: Sat Dec 22, 2012 1:49 am

#smashapps

i'm looking for part 2, interesting post...! love it :mrgreen:
User avatar
smashapps
Coding Guru
Coding Guru
Posts: 961
Joined: Tue Apr 05, 2011 8:41 am

I ended up not doing it. If you didn't notice.

Sorry.
My name is Tom | Visit my blog where I post new content every day! Tom's Daily Blog | MineCraft is awesome!
7 posts Page 1 of 1
Return to “Quick Snips”