[Tutorial]Convert Bits 2 Bytes

Posts 1–8 of 8 · Page 1 of 1
[Tutorial]Convert Bits 2 Bytes
Convert Bits to Bytes and Bytes to Bits

In this tutorial we will learn how to make a Program to convert bytes to bits and Vice versa,

  • As always , create a new vb.net project
  • Name it ConvertBits (or whatever you like)
  • On your form you are going to add


  1. 2 Texboxes
  2. 2 Radio Controls
  3. 1 CheckBox
  4. 1 Label
  5. 2 Buttons


  • Arrange them however you like (you can use the below image for guidance)





  • Set the Text for the following controls to


  1. Radio Control 1 - To Bits
  2. Radio Control 2 - To Bytes
  3. CheckBox - Abbreviated
  4. Button 1 - Convert
  5. Button 2 - Exit


  • Change the name of your label to lblbit


It should look something like this



As always here is the code, I have noted everything you will need to know in order to learn from this example

Code:
Option Strict On
Imports System.Convert

Public Class Form1
    'True = Bytes to bits
    'False = bits to Bytes

    Dim objBitByte As New Converter()

    Private Sub ButtonX1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonX1.Click
        If CheckBox1.Checked = False Then
            'Unabbreviated output
            TextBox2.Text = objBitByte.Convert(CLng(TextBox1.Text), RadioButton1.Checked)
        Else
            'Abbreviated output
            TextBox2.Text = objBitByte.Format(CLng(objBitByte.Convert(CLng(TextBox1.Text), RadioButton1.Checked)), RadioButton1.Checked)
        End If
    End Sub

    Private Sub ButtonX2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonX2.Click
        Me.Close()

    End Sub

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

    End Sub

    Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
        'Change lblBit to display "Bytes" when converting to bits
        If RadioButton1.Checked = True Then
            lblbit.Text = "Bytes"
        End If
    End Sub

    Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
        'Change lblBit to display "bits" when converting to Bytes
        If RadioButton2.Checked = True Then
            lblbit.Text = "bits"
        End If
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        If Not IsNumeric(TextBox1.Text) Then
            TextBox1.Clear()
        End If
    End Sub
End Class
Public Class Converter

#Region "Methods"

      Function Convert(ByVal intBitByte As Int64, ByVal blnType As Boolean) As String
        If blnType = True Then
            'Convert from bits to Bytes 
            Try
                Return CStr(intBitByte * 8)
            Catch err As ApplicationException
                MessageBox.Show(err.Message)
            End Try
        Else
            'Convert from Bytes to bits 
            Try
                Return CStr(intBitByte \ 8)
            Catch err As ApplicationException
                MessageBox.Show(err.Message)
            End Try
        End If
    End Function

    Function Format(ByVal intData As Int64, ByVal blnType As Boolean) As String
        Dim strData As String
        If intData < 1024 Then
            If blnType = False Then
                'B
                strData = CStr((intData) & " B")
                Return strData
            Else
                ' b
                strData = CStr((intData) & " b")
                Return strData
            End If
        ElseIf intData >= 1024 And intData < 1048576 Then
            If blnType = False Then
                ' KB
                strData = CStr((intData * 10 ^ -3) & " KB")
                Return strData
            Else
                'Kb
                strData = CStr((intData * 10 ^ -3) & " Kb")
                Return strData
            End If
        ElseIf intData >= 1048576 And intData < 1073741824 Then
            If blnType = False Then
                ' MB
                strData = CStr((intData * 10 ^ -6) & " MB")
                Return strData
            Else
                ' Mb
                strData = CStr((intData * 10 ^ -6) & " Mb")
                Return strData
            End If
        ElseIf intData >= 1073741824 And intData < 1099511627776 Then
            If blnType = False Then
                'GB
                strData = CStr((intData * 10 ^ -9) & " GB")
                Return strData
            Else
                'o Gb
                strData = CStr((intData * 10 ^ -9) & " Gb")
                Return strData
            End If
        ElseIf intData >= 1099511627776 And intData < 1125899906842624 Then
            If blnType = False Then
                'TB
                strData = CStr((intData * 10 ^ -12) & " TB")
                Return strData
            Else
                ' Tb
                strData = CStr((intData * 10 ^ -12) & " Tb")
                Return strData
            End If
        Else
            MessageBox.Show("ERROR: overflow", "Program does not support EB/Eb or higher!", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly)
        End If
    End Function

#End Region
    Public Class Form1
        'True = Bytes to bits
        'False = bits to Bytes

        Dim objBitByte As New Converter

    End Class
End Class
End Function in Region may give errors, pay no mind to it.



Hope This helps Someone

' As always you may have to change the control names to make this work for your program
' Didn't include the source for this one yet, I will sometime soon
' Just in a rush this morning
Is there any difference between a radiobutton and a checkbox? just wondering
In a sense, though the outcome can be achieved using either or.

*radiobuttons only allow one decision
checkboxs allow the selction of mulitiple options.

Example:

RadioButton
-----------
Gender = o Male o Female
Animal = o Cat o Dog

CheckBox
--------
Favorite programming languages
o Visual Basic o C++ o Assembly

here you can select all 3
Helpful tutorial, GJ nextgen.
Quote Originally Posted by Lolland View Post
Helpful tutorial, GJ nextgen.
Thanks , Hope this helps here
nice tutorial!
nice tutorial and btw do u use VB 10 cus ur buttons look different O.o
I use VB 10 (on occasions)but not in this case, the buttons are ButtonXControls.
The downloads for all of the UI Components are found in my

How To:UI Tutorial
and
Download:UI Components

Hope this helps
Posts 1–8 of 8 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Need help?