Archive for the ‘visual’ Tag
Encrypt/Decrypt with Imports System.Security.Cryptography
This little tutorial puts some morge sunshine on the System.Security.Cryptography standard class in VB.net…
There are a few encryption possibilities, but i’m going to encrypt/decrypt on a TripleDes algorithm!
More info on the TripleDES: http://en.wikipedia.org/wiki/Triple_DES
The first thing we have to do is import the class
Imports System.Security.Cryptography
We also make a global (module) variable, for a Service Provider to handle the TripleDES settlement
Private TripleDes As New TripleDESCryptoServiceProvider
We then need 2 functions, A decrypt and Encrypt function…
We’ll start with the Encrypt function…
Public Function EncryptData(ByVal plaintext As String) As String
‘ Convert the plaintext string to a byte array.
Dim plaintextBytes() As Byte = System.Text.Encoding.Unicode.GetBytes(plaintext)
‘ Create the stream.
Dim ms As New System.IO.MemoryStream
‘ Create the encoder to write to the stream.
Dim encStream As New CryptoStream(ms,TripleDes.CreateEncryptor(),System.Security.Cryptography.CryptoStreamMode.Write)
‘ Use the crypto stream to write the byte array to the stream.
encStream.Write(plaintextBytes, 0, plaintextBytes.Length)
encStream.FlushFinalBlock()
‘ Convert the encrypted stream to a printable string.
Return Convert.ToBase64String(ms.ToArray)
End Function
To decrypt the following Encryption we will make the function beneath…
Public Function DecryptData(ByVal encryptedtext As String) As String
‘ Convert the encrypted text string to a byte array.
Dim encryptedBytes() As Byte = Convert.FromBase64String(encryptedtext)
‘ Create the stream.
Dim ms As New System.IO.MemoryStream
‘ Create the decoder to write to the stream.
Dim decStream As New CryptoStream(ms, TripleDes.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write)
‘ Use the crypto stream to write the byte array to the stream.
decStream.Write(encryptedBytes, 0, encryptedBytes.Length)
decStream.FlushFinalBlock()
‘ Convert the plaintext stream to a string.
Return System.Text.Encoding.Unicode.GetString(ms.ToArray)
End Function
This will do the trick!
You can also attach your personal Private / Personal key to the TripleDES Cryprography, here VB uses the standard Keys …
What you will have to do is following,
1. Declare your own private and public key ( wich are an array of bytes)
Dim key() As Byte = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24}
Dim iv() As Byte = {8, 7, 6, 5, 4, 3, 2, 1}
2. edit the line of EncryptData and DecryptData where you attach your own declared keys
Dim decStream As New CryptoStream(ms, TripleDes.CreateDecryptor(key, iv),_ System.Security.Cryptography.CryptoStreamMode.Write)
Using a OpenFileDialog
Opening a file using a openfiledialog like many programs use ….
This is how
First we drag a new OpenFileDialog attribute in the design mode, this can be found in the Toolbox menu, in the subcategorie: Dialogs.
This just added component has a few handy properties…
Filter: Only Text Files |*.txt - in this situation only textfiles will be shown
FileName : Only Text Files - with the standard filename: “Only Text Files”
How can we now adress this item with code…?
First , giving action to show this OpenFileDialog
OpenFileDialogName.ShowDialog()
After selecting a file, we want the file location , not?
This is how…
If OpenFileDialogName.FileName.Length > 0 Then
MsgBox(OpenFileDialogName.FileName)
End If
If the filename has a length of more than 0 charactars, chich means there was a file selected, a messagebox will be shown with the filelocation….
Other dialogs available

Visual Basic 2008 Express Edition Beta 2
Visual Basic 2008 Express Edition Beta 2 is out now…
Posting this little summary from the Microsoft website will give you a short previeuw of the newly added functions…
|
|
- Add cool, fun controls to your projects using the C4F Developer Toolkit and the C4F Vista P2P Developer Kit
- Create P2P chat applications with no lines of code
- Build cool applications with easy-to-use drag and drop controls which includes features like Smart Tags, all source code in Visual Basic, QuickStart documentation and much more; exclusively developed for Visual Studio 2008 and Windows Vista
- Stream videos to family with a custom WPF P2P video application
- Get started with the new WPF C4F Dashboard application
- Share projects with others using the Popfly Explorer
- Connect with your friends and create cool applications with the Facebook Developer Toolkit
- Checkout cool fun projects dedicated to Do-It-Yourself developers on Coding4Fun
Download the offline/online installer: http://www.microsoft.com/express/download/offline.aspx
StreamReader > Reading textfiles into Visual Studio
So what we wanna do in this little tut is to be able to load a text file from our local hard drive into visual basic,
i’ve got this text file that contains different players from a belgian soccer team , with there scores and player names…

So this is what we will do after reading the text file:
We first filter out the team name , we only want the player scores…. , which is the last number in each textline…
Using the Streamreader we can read text files into the Visual Basic program, not as hard as you might think…
Here we go …
First we declare a new StreamReader
Dim fileReader As System.IO.StreamReader
We than provide the StreamReader the file location of our textfile
fileReader = My.Computer.FileSystem.OpenTextFileReader(“C:\test.txt”)
Following we declare a new String variable stringReader, TekstLijn and sSpeeldag
Dim stringReader As String
Dim sSpeeldag As String
Dim TekstLijn As String
Using the Do … Loop Until we run through the text file …
Do
stringReader = fileReader.ReadLine()
TekstLijn = stringReader If TekstLijn.Length > 20 Then
Dim sInfo() As String sInfo = TekstLijn.Split(vbTab) Dim sPunt As String
sPunt = sInfo(5)MsgBox(“Score: ” & sPunt)
End If
Loop Until TekstLijn Is Nothing
What we actually do in the code above is running through each line of the textfile, we first check if the line we are currently at contains more than 20 characters, hereby we filter out the names of the soccer teams…
What we than do is split the whole line into different items, in this case we only need the player scores, …
So we will use the 5ft item of the split
sInfo(5)
After splitting the text line, we will use the MsgBox command to put the player score on the screen, and this for each textline in the textfile…
Easy afterall for those who didn’t know this function yet…
A StreamReader function is also available in Visial Studio, with this function we will be able to write to textfiles….
Leave a Comment
Leave a Comment
Leave a Comment