Simple but Neat Visual Basic Tips and Tricks Part 1
Posted: Tue Jan 01, 2013 2:15 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
#10: InputBox with a question
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.
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
#2: Convert a String to Upper, Lower and Title caseImports System.IO
ListBox1.Items.AddRange(System.IO.Directory.GetLogicalDrives)
Code: Select all
#3: Text to SpeechTextBox1.Text = TextBox1.Text.ToUpper TextBox1.Text = TextBox1.Text.ToLower TextBox1.Text = StrConv(TextBox1.Text, VbStrConv.ProperCase)
Code: Select all
#4: Read one line of text from a fileDim SAPI
SAPI = CreateObject("SAPI.spvoice")
SAPI.Speak(TextBox1.Text)
Code: Select all
#5: Read text from encrypted fieldsDim 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)
Code: Select all
#6: RGB ColoursDim fileReader As String
fileReader = My.Computer.FileSystem.ReadAllText(My.Computer.FileSystem.SpecialDirectories.Desktop & "\Document.txt",
System.Text.Encoding.UTF32)
TextBox1.Text = fileReader
Code: Select all
#7: Form SizeColor.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
Code: Select all
#8: Minimize all forms at onceMe.Size = New Size(0, 0)
Code: Select all
#9: Random NumbersFor Each frm As Form in Application.OpenForms
frm.WindowState = FormWindowState.Minimized
Next frm
Code: Select all
This one will generate a random number from the range 1000 to 1 and then add it to the textbox. Randomize is the initialization.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
#10: InputBox with a question
Code: Select all
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.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
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.