Friday, December 28, 2012

How to setup VPN in Windows 8

How to setup VPN in Windows 8


Set up a VPN (Virtual Private Network) could be done in less than 5 minutes. The steps are very simple. You need the IP address, the username and the password. That is all.

1. First, you need to access the Windows 8 Control Panel. Using the search Apps or from the classic desktop.
Control Panel

Search for Control Panel in Windows 8


Thursday, December 27, 2012

How could I get more results in Bing?

How could I get more results in Bing?

Bing is the second most popular search tool on the internet after Google. So is important to raise your search results in a single page to save you time. By default, you may have from 5 to 7 results on each page and the page you are looking for might be far away.

Here is a simple video showing you how to increase the number or results in Bing.com






(Sorry, on the video, my regional setting for MSDN is in French)

Thursday, December 20, 2012

Change font for any control in VB.NET

Change font for any control in VB.NET



change font in Visual Basic

Every controls from Windows.Forms in Visual Basic could get a new font using this easy formula :


New System.Drawing.Font("Minya Nouvelle", 12, FontStyle.Bold)


Monday, December 17, 2012

add icon in TreeView

Add icon to a TreeView for beginner


To add an icon or an image to each node in a TreeView, you need to associate an ImageList. In this post, I will show you have to do it base from a code from a previous post I made. I will simply add the missing lines in it and highlight them in yellow. So I will print 2 codes: before and after.

It is important to note that the code use here is efficient for small TreeView. It won’t be a good idea if the TreeView has a lot of items. The problem is performance.

Add icon to TreeView


Fill a TreeView from a Basic recursive search file

Fill a TreeView from a Basic recursive search file

Basic recursive search file


I pick a code from one of my previous post : search file search file - Improve performance . In that post a made a recursive function to search file file and directory over and over. All the result are injected in a ListView and not in a TreeView. Enventually, we will have to adapt the code because a ListView and a TreeView works differently.
While ListView group the items in a collection, Treeview are grouping the data with nodes.
Please take a look at the code and go to the next post.



    Private Sub findfiles()

        Dim dirPath As String
        Dim path_valid As Boolean
        path_valid = True

        dirPath = "c:\temp\" ' temp = no access problem
        dirPath = "c:\System Recovery" ' System Recovery = access problem



Sunday, December 16, 2012

Fill a TreeView with recursivity

Fill a TreeView with recursivity


TreeView are composed of nodes after nodes. If your TreeView represent a kind of explorer, you might have several types of object such as folder and files.

In this post, I have to give you a warning. I am not interested to vomit the answer. Instead, I’ll try to explain how I could reach the reach the answer so eventually; you could understand how I was able to build the code without any copy paste from any forum or blog over the internet.  This post might not for you. If not, you will immediately go straight to the answer at the far end of this article. You copy and paste the code and voilà. Here is the link: search file - Improve performance 

If you force yourself to read this article, please note I will separate the article into 4 parts

  1. Basic recursive search file 
  2. Analyzing the needs 
  3. Implementing the changes
  4. Run the code


To be honest, I can’t remember making any treeview for a Windows Start Menu in my lifetime. I never Bing’d or Google’d for the answer. I simply applied a logical pathway to create the code I want less than 10 minutes. Yes, under 10 minutes with music on my headphone. Anyway.



TreeView Example


Running the Code for your TreeView

Running the Code


Let’s remember I made a sample of a Windows 8 Start menu using VB.NET with Visual Studio 2010.  I was doing the program in the menu. Is not something hard, is just logic and simple if you carefully think of what are you doing.

Run the Visual Basic code when you are ready. Here you want to fill a treeview. Here is the final results:

TreeView Sample

Of course, the TreeView is terribly ugly. The list is upside down. There are no icons. The text is small and the whole path is displayed.

I guest you might do the same exercises, check for your existing code and find what is missing.


Previous Page : Implementing changes to your TreeView <<<====

Implementing the changes to your TreeView

Implementing the changes



Ok, time to change the recursive function with the question you ask yourself. You may have to do some tries or test before getting something to work perfectly.  Normally, if you analyze your problem and ask yourself a lot of question, you should be able to make the changes easily.

Before :


Private Sub findfiles(Optional dirPath As String = "c:\temp\"Optional String_to_Search As String = "*.dll")



For Each foundFile As String In My.Computer.FileSystem.GetFiles( _
      dirPath, FileIO.SearchOption.SearchTopLevelOnly, String_to_Search)

       oForm2.ListBox1.Items.Add(foundFile)

Next

For Each foundDirectory As String In My.Computer.FileSystem.GetDirectories( _ dirPath, FileIO.SearchOption.SearchTopLevelOnly)

      findfiles(foundDirectory) ''recursive search file : re-use "himself"

Next




For Each foundFile As String In My.Computer.FileSystem.GetFiles( _
       dirPath, FileIO.SearchOption.SearchTopLevelOnly, String_to_Search) 'recursive search file : you need to limit your search to 1 single directory : and put extension in search argument

       'oForm2.ListBox1.Items.Add(foundFile)
       'TreeView1.Nodes.Add(foundFile)

       no1.Nodes.Add(foundFile)

Next

For Each foundDirectory As String In my.Computer.FileSystem.GetDirectories( dirPath, FileIO.SearchOption.SearchTopLevelOnly)

    'findfiles(foundDirectory) ''recursive search file : re-use "himself"
     Dim no2 As TreeNode
     no2 = no1.Nodes.Add(foundDirectory)
     findfiles(no2, foundDirectory, String_to_Search) ''recursive search file : re-use "himself"
Next



After:


Private Sub findfiles(no1 As TreeNode, Optional dirPath As String = "c:\temp\"Optional String_to_Search As String = "*.dll")


I added no1 for node one, this will represent the main node or current node. It works exactly the same way compared to dirPath. dirPath acts like the current path. All the stuff in the argument in a recursive function are constantly reused and is always base from the current data. The current data here in this example is the current node with a current directory.



For Each foundFile As String In My.Computer.FileSystem.GetFiles( _
       dirPath, FileIO.SearchOption.SearchTopLevelOnly, String_to_Search) 'recursive search file : you need to limit your search to 1 single directory : and put extension in search argument

       'oForm2.ListBox1.Items.Add(foundFile)
       'TreeView1.Nodes.Add(foundFile)

       no1.Nodes.Add(foundFile)

Next

For Each foundDirectory As String In my.Computer.FileSystem.GetDirectories( dirPath, FileIO.SearchOption.SearchTopLevelOnly)

    'findfiles(foundDirectory) ''recursive search file : re-use "himself"
     Dim no2 As TreeNode
     no2 = no1.Nodes.Add(foundDirectory)
     findfiles(no2, foundDirectory, String_to_Search) ''recursive search file : re-use "himself"
Next




And here how I decided to start my findfiles function. I injected a first node called MAIN.


    Private Sub Button1_Click(sender As System.Object, e As System.EventArgsHandles Button1.Click
        'position()
        'Fill_TreeView()
        Me.TreeView1.Nodes.Add("MAIN")
        Dim no As TreeNode
        no = Me.TreeView1.Nodes.Item(0)
        findfiles(no, "C:\ProgramData\Microsoft\Windows\Start Menu""*.lnk")
    End Sub



Friday, December 14, 2012

Sad event today in Connecticut

my condolences to all the victims and their loved ones


Wednesday, December 12, 2012

search file - Improve performance

First in Visual Basic .NET


When you program, if the task in your loop is the same over and over, you might want to recycle some code from your program. This will multiply the power of your power by 2, by 5 or 10 and make your code shorter.
Here is an example from a previous post. The sample code in Visual Basic will search for all the files with a pattern in a directory/subdirectory and list them in a ListBox.  Isn’t that great? Yes and no. Even with the best tactic to speed up your code, you will wait forever if the directory has a lot of subdirectories with a lot of files.

Old code: search file without recursive feature


    Private Sub findfiles()

        Dim dirPath As String
        Dim path_valid As Boolean
        path_valid = True

Load Dll

Load DLL in VB.NET


Dll in Visual Basic must be load to make your software correctly. Dll are librairy use to run multiples actions. If there is a missing dll, you have to download it somewhere a place it back in the same place where it suppose to be.

one thing your program could do is to dynamically load your dll. That way, your program could handle missing dll and will be able to run without them.

 The sample presented in this post separated into several parts. The purpose of this post is to make your program more powerfull.


  1. The main program
  2. The DLL number 1
  3. The DLL number 2



You may have to change the path to get your DLL. I intentionally left the big string in the code so you could easily change it before using it.

This is the Layout sample:

  •   Form1
  •   MenuStrip1
  •   Multiples Menu Items (Fichier, Options, HQ, ….)


Sample Assembly Form Layout


Sample of form1.vb


Imports System.Reflection 'you need this line to use Assembly and Activator
Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgsHandles MyBase.Load

        load_dll()

    End Sub


    Private Sub load_dll()
        Dim oType As System.Type
        Dim oAssembly As System.Reflection.Assembly
        Dim oObject As System.Object
        Dim ms As System.Windows.Forms.ToolStripMenuItem

        'We load the assembly called HelloWorld.dll located in C:\
        oAssembly = Assembly.LoadFrom("C:\Users\checkkay\Documents\Visual Studio 2010\Projects\SampleAssembly\ClassLibrary1\bin\Debug\ClassLibrary1.dll")
        'We get the type of the application (Class_Root_Namespace.Name_Class)
        oType = oAssembly.GetType("ClassLibrary1.SayHello")
        'We create an type instance
        oObject = Activator.CreateInstance(oType)
        'We execute the methode Hello
        MsgBox(oObject.Hello()) 'call a command from the class1


        tsHQ1 = New System.Windows.Forms.ToolStripMenuItem("fonction 1")



       ms = Me.MenuStrip1.Items(0) 'Selecting the first menu Item (File or Fichier)
        ms.DropDownItems.Add(tsHQ1) 'Adding a MenuItem Under File or Fichier



        oAssembly = Assembly.LoadFrom("C:\Users\checkkay\Documents\Visual Studio 2010\Projects\SampleAssembly\ClassLibrary2\bin\Debug\ClassLibrary2.dll")
        'We get the type of the application (Class_Root_Namespace.Name_Class)
        oType = oAssembly.GetType("ClassLibrary2.SayHello")
        'WE create an type instance
        oObject = Activator.CreateInstance(oType)
        tsHQ2 = New System.Windows.Forms.ToolStripMenuItem("fonction 2")

        ms = Me.MenuStrip1.Items(0) 'Selecting the first menu Item (File or Fichier)
        ms.DropDownItems.Add(tsHQ2) 'Adding a MenuItem Under File or Fichier


    End Sub

    Private Sub FermerToolStripMenuItem_Click(sender As System.Object, e As System.EventArgsHandles FermerToolStripMenuItem.Click
        Me.Close()
        Me.Dispose()

    End Sub

End Class




Sample of place where you could declare your ToolStripMenuItem with events:

  • tsHQ1 as System.Windows.Forms.ToolStripMenuItem
  • tsHQ2 as System.Windows.Forms.ToolStripMenuItem




Imports System.Reflection
Imports System

Module Module1
    Public WithEvents tsHQ1 As System.Windows.Forms.ToolStripMenuItem
    Public WithEvents tsHQ2 As System.Windows.Forms.ToolStripMenuItem
    Public Sub launchetshq1() Handles tsHQ1.MouseDown
        MsgBox("allo 1")
    End Sub

    Public Sub launchetshq2() Handles tsHQ2.MouseDown
        MsgBox("allo 2")
    End Sub
End Module






Finally a sample of a home made DLL. ClassLibrary1 and ClassLibrary2 are virtually identical.


Public Class SayHello
    Private mName As String

    Function Hello()
        Return "Hello World"
    End Function

    Public Function Bonjour()
        Return "Bonjour le monde"
    End Function



    Public Sub New()
        Me.mName = "HQtool1"
    End Sub
End Class






What the code does is relatively simple.

First, the form will try to lead a first DLL and will put it in oAssembly. oAssembly will be our DLL inside the main program.

Then, you need to access to your stuff inside your DLL (Assembly). Here, you don’t have any choice, you need to know the Class Name, the Assembly Name; if required the Name Space and the root Name Space.

Root Namespace


Class Name



      oType = oAssembly.GetType("ClassLibrary1.SayHello")



Download the sample project : SampleAssembly.zip


Other posts: 
Add Control in your form dynamically


References and External Links:

Visual Studio 2012 from Microsoft Visual Studio site



Cheapest way to get Visual Studio any editions from Amazon:



           

How could you dynamically load your own DLL

In Visual Basic Express 2010, Visual Studio 2010 or any Visual Basic Edition, you could dynamically load any compatible DLL file. The sample presented is separated into several parts:


  1. The main program
  2. The DLL number 1
  3. The DLL number 2



You may have to change the path to get your DLL. I intentionally left the big string in the code so you could easily change it before using it.

This is the Layout sample:

  •   Form1
  •   MenuStrip1
  •   Multiples Menu Items (Fichier, Options, HQ, ….)


Sample Assembly Form Layout


Tuesday, December 11, 2012

Inherits Button

With Visual Basic .NET or Visual Studio 2010

Give a new aspect to your entire button in your program simply by creating a derived class. It might be difficult to understand but I guarantee is very simple to put in practice.

Here is the classic button from the Framework 4 .NET

regular Button

Nothing special, you use it, you change some initial settings. You put some events: left click, right click, …. The world seems great.

OK, now what happens if you want all your buttons to open a Process in Windows 7 or in Windows 8? What if you want all your buttons to change color to blue when the mouse is over? The problem is that you have to change each buttons to do exactly the same thing and do many copy a paste. This is mostly inefficient and heavy. Instead of using the basic Button from System.Windows.Forms, we will alter that button and reuse it.

New Button

First you have to create a new class anywhere in your problem but not in a other class.

Here is a sample:




Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgsHandles MyBase.Load

    End Sub


End Class




Public Class ButtonColor
    Inherits Button
   
End Class




Build your project and automatically, your new button will be available. In this project I named my button: ButtonColor.

It is important to put the line “Inherits Button” immediately after your first line of your class.

Now, if I put a simple member, a constructor methods a few events, here is what I have:



''' <summary>
''' Sample of Inherits Button / Derived Class Button
''' </summary>
''' <remarks>this is a sample</remarks>
Public Class ButtonColor
    Inherits Button

#Region "Members"
    Public mPathExe As String
    Public mArguments() As String

#End Region

#Region "constructor"
    Public Sub New()
        MyBase.New()
        mPathExe = "Explorer.exe" 'default value
        ReDim mArguments(0)
    End Sub
    Public Sub New(sPathExe As String)
        MyBase.New()
        mPathExe = sPathExe
        ReDim mArguments(0)
    End Sub

#End Region

#Region "Actions Events"
    Private Sub ButtonColor_Click(sender As Object, e As System.EventArgsHandles Me.Click

        Dim index1 As Integer
        Dim sArguments As String
        sArguments = ""

        For index1 = 0 To mArguments.Length - 1 Step 1
            sArguments = " " & mArguments(index1)
        Next
        If sArguments.Length > 0 Then
            Process.Start(mPathExe, sArguments)
        Else
            Process.Start(mPathExe)
        End If

    End Sub
#End Region


#Region "Color Events"
    Private Sub ButtonColor_MouseHover(sender As Object, e As System.EventArgsHandles Me.MouseHover
        Me.BackColor = Color.BlueViolet
    End Sub

    Private Sub ButtonColor_MouseLeave(sender As Object, e As System.EventArgsHandles Me.MouseLeave
        Me.BackColor = Color.LightGray
        Me.UseVisualStyleBackColor = True
    End Sub

#End Region
End Class



If you take a look to the code, if the person click on a button it calls the process.Start methods to run a fill or open something. That is if mPathExe as a valid path. The click event could also take arguments.
There are also 2 events exclusively intend for the color of the button. When the cursor is over the button, it will turn BlueViolet. When the mouse leaves the buttons, the button recovers his state.

This is a sample, so you could put more stuff in it such a error handling or more functions.  Whatever pleases you.


Use your Enhanced button


To use your derived class, is a good idea to remove the keywords WithEvents from your form. See image.

Remove WithEvents


You won’t need the form (in this case Form1) to handle your button event since the button could handle himself. You may save a tiny little bit of execution time and help you concentrate your basic code on your button inside the button.


Click on the enhanced button: ButtonColor


Ok, I need to personalize the command in each button so when I click on it, it will call for his own instruction. Here is a sample, very simple to show you how it works.







    Private Sub Form1_Load(sender As System.Object, e As System.EventArgsHandles MyBase.Load

       
        ButtonColor1.Text = Environment.UserName.ToString
        ButtonColor1.mPathExe = "C:\Users\" & Environment.UserName.ToString

        ButtonColor2.Text = "Documents"
        ButtonColor2.mPathExe = "explorer.exe"
        ButtonColor2.mArguments(0) = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)

        ButtonColor3.Text = "Images"
        ButtonColor3.mPathExe = "explorer.exe"
        ButtonColor3.mArguments(0) = System.Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)

        ButtonColor4.Text = "Music"
        ButtonColor4.mPathExe = "explorer.exe"
        ButtonColor4.mArguments(0) = System.Environment.GetFolderPath(Environment.SpecialFolder.MyMusic)

        ButtonColor5.Text = "Recents Items" 'need a little listView
        ButtonColor5.mPathExe = "explorer.exe"
        ButtonColor5.mArguments(0) = System.Environment.GetFolderPath(Environment.SpecialFolder.Recent)

        ButtonColor6.Text = "Computer"
        ButtonColor6.mPathExe = "explorer.exe"
        ButtonColor6.mArguments(0) = System.Environment.GetFolderPath(Environment.SpecialFolder.MyComputer)

        ButtonColor7.Text = "Network" 'i don't know how to get the same windows
        ButtonColor7.mPathExe = "explorer.exe"
        ButtonColor7.mArguments(0) = System.Environment.GetFolderPath(Environment.SpecialFolder.NetworkShortcuts)

        ButtonColor8.Text = "Control Panel"
        ButtonColor8.mPathExe = "C:\Windows\System32\control.exe"

        ButtonColor9.Text = "Devices and Printers"
        ButtonColor9.mPathExe = System.Environment.GetFolderPath(Environment.SpecialFolder.PrinterShortcuts)

        ButtonColor10.Text = "Default Programs"
        ButtonColor10.mPathExe = "C:\Windows\System32\control.exe"
        ButtonColor10.mArguments(0) = "/name Microsoft.DefaultPrograms"

    End Sub



Ok, I know the code is a bit big and could be improved. Is only to show you have simple it could be.
Because if I works the old fashion way, I might have more lines because I will have a Click , a MouseHover and a MouseLeave methods for each button instead of 1 unique events for every button.

At the end, when you run the program. You will have something like this:




Mouse Over


Previous Post : Search a file

Next Post : Create searchBox using UserControl





About 

I invite you to visit my blog for more articles and leave a comment. Check Technologies represents more than 10 years .... Computer and computer aided design.

Check Technologies ltée
Official Website Check Technologies