Sunday, February 26, 2012

Read a file line by line to the end in VB.NET

This tutorial it's about reading a file line by line until the end in VB.NET. This is usefull, for instance, if you are using this file as a settings file and you inserted one setting per line then you can read them by order.

Visual Studio screenshot:


Code:


Imports System.IO
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim file_dir As String = "file.txt"
        'parse file
        Try
            Dim sr As StreamReader = New StreamReader(file_dir)
            Do Until sr.EndOfStream
                TextBox1.Text += vbNewLine + sr.ReadLine()
            Loop
            sr.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
End Class

In this case you are reading and adding each line to a textbox but you can read this to a string array and keep the information to work with later. It's also important to use try catch because if the file doesn't exist or you don't have permissions to read it will not crash the application.

Monday, February 20, 2012

VB.NET Write data to XML using a DataSet


In this tutorial i will talk about getting the name of all the files (in this case images) in a directory and store them into a XML file using a DataSet in VB.NET.


Code:
Imports System.IO

Public Class Form1
    Dim images_dir As String = "C:\myfolder"
    Dim xml_dir As String = "C:\myfolder"
    Dim image_list As String()

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        DataSet_images.Tables("Table1").Clear()
        image_list = Directory.GetFiles(images_dir)
        Dim filename As String
        Dim counter As Integer
        For Each filename In image_list
            counter += 1
            DataSet_images.Tables("Table1").Rows.Add(New Object() {counter, filename})
        Next filename
        DataSet_images.Tables("Table1").WriteXml(xml_dir + "\images.xml", XmlWriteMode.IgnoreSchema)
    End Sub
End Class
Video Tutorial:





Sunday, February 12, 2012

VB.NET Limit textbox minimum and maximum characters

Let's imagine that you hava a textbox for a user to input something and you want that to be between a minimum and a maximum number of characters, how you do it on VB.NET? Let's see!

Code:

Maximum size limitation can be done in the textbox properties in the "MaxLenght" option.

textbox vb.net
To minimum size limitation it's only necessary to check when user clicks the button to send that information.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        check_text()
    End Sub

    Private Sub check_text()
        If TextBox1.Text.Length = 3 Then
            MessageBox.Show("Ok")
        Else
            MessageBox.Show("You need to insert 3 numbers")
        End If
    End Sub


Video tutorial:


Monday, February 6, 2012

Textbox for numbers only VB.NET

Sometimes we need to limit our user inputs to only numbers for instance in a quantity input and for that i leave here a tutorial on how to do that in VB.NET.

Code:

Public Class Form1    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If (Asc(e.KeyChar) >= 48 And Asc(e.KeyChar) <= 57) Or Asc(e.KeyChar) = 8 Then
            e.Handled = False
        Else
            e.Handled = True
        End If
    End Sub



End Class


Video tutorial: