Tuesday, December 11, 2007

How to implement your FileSystemWatcher

How to implement your FileSystemWatcher

Step 1

Once you have created your FileSystemWatcher, you need to set certain parameters for it.

Step 2

We will do this in the Form_Load event. What we are trying to do is to create a FileSystemWatcher event that will detect creation of file with the extension TXT to it’s directory only – not inclusive of subdirectories. This is all configuring with the codes below.

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

FileSystemWatcher1.Path = "c:\im"

FileSystemWatcher1.Filter = "*.txt"

FileSystemWatcher1.IncludeSubdirectories = False

FileSystemWatcher1.EnableRaisingEvents = True

End Sub

Step 3

Then to implement our FILE CREATION event,

Step 4

Go to your FileSytemWatcher1 object and then select it’s events under Declaration in the Created

Step 5

Under this control, use the MsgBox to notify user that a TXT file has been created in your folder called IM.

Private Sub FileSystemWatcher1_Created(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Created

MsgBox("A newly created TXT file is detected")

End Sub

Edited By:Melanie Michael Jominin

© 2001 ISC/Inner Esteem

All rights reserved. All other product names and trademarks are registered properties of their respective owners.

301001

How to create FileSystemWatcher

How to create FileSystemWatcher

Step 1

To create Flesystemwatcher, go to your Toolbox. Under the component tab, pull the controlbox into your form.

You can also declare this component using the following codes

Dim mwatcher as New System.IO.FilesystemWatcher()

What is FileSystemWatcher

What is FileSystemWatcher

FileSystemWatcher is a component that allows you to monitor changes to your filesystem such as creation of a new file. FilesystemWatcher do not have a user interface.

How to add text to your Toolbar

How to add text to your Toolbar

Step 1

To add text to your Toolbar, click on Button under your toolbar properties.

Step 2

Then when a window popup, change the text to let’s say Sample1.

Step 3

Click on Ok when done.

An example of output looks like this above.

How to create Toolbar

How to create Toolbar

Step 1

To create a toolbar in Visual Basic, user will need to create a MDI application and then create your menu as we describe above.

Step 2

Once we have got all that, we can create our toolbar, by using Toolbar control and picture box. Drag a component called Toolbar and place it just below your main menu. Then it will automatically scale to fit the menu. As shown in the diagram above.

Step 3

To create each Toolbar button, you need to use ImageList control

Step 4

You have to create an ImageList and then bind it your toolbar

Step 5

In your Window Form toolbar, create an ImageList Control.

Step 6

First we will create an Images for ImageList control.

Step 7

Click on Add to insert new images to this control. We will use some sample images for our demo here.

An example of our output here had shown above.

Step 8

Then we will proceed to setup our toolbar in VB.net. Switch to our Toolbar and then under its imageList set the properties to ImageList1 control that we have created earlier.

Step 9

Okay, then we will set it’s Button properties. Under your toolbar properties select Button and then click on to open a window.

Step 10

Click on Add to add a toolbar. Under the ImageIndex, set it to an Imagelist that you have created. In this example we will create 2 of this toolbar.

If you would like to set your Tooltip you can do so here.

This is an example of your Toolbar.

Step 11

Then just press F5 and run it. Off course there will be no function to run as you did not put it in.

How to create Custom User Control

How to create Custom User Control

Step 1

You can create custom user control to allow rapid application development. To create a custom control, go to Visual Studio.Net.

Step 2

Go to File and select New then choose Project and select Window Application.

Step 3

Once your project has been created, go to File and select Add New Item.

We will create Custom User control that looks something like this.

Step 4

So just click on UserControl1.vb [Design] tab and then proceed to create a textbox and a button here.

Step 5

Then you can Build it.

Your control which is call UserControl1 will appear in your Toolbox. To use it, click on your Form1 and then drag the user control over your form.

It will look something like this.

Step 6

If you have another form called Form2, by dragging the user component over would allow you to create your control rapidly without having to redesign it.

How to read an XML file to DataSet

How to read an XML file to DataSet

Step 1

To read an XML file to a DataSet, first of all you need to declare a dataset first and then use the method called ReadXML to read your XML file to your dataset


Step 2

Create a datagrid and a button on your form. Then use the following code

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

Dim ds As New DataSet("Product")

ds.ReadXml("c:\product2.xml")

With DataGrid1

.DataSource = ds

End With

End Sub

Step 3

This will create a Dataset and then use it’s method of ReadXML to get data from your file. Then associate your dataset to datagrid.

An example of the screen output from the code above is shown below:

How to create MDI Child

How to create MDI Child

Step 1

To create an MDI child, go to the File and select New click event and then declare the following codes.


It is actually the same as creating and show a normal form except the form MdiParemt properties is set to it’s parent.

Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click

Dim f2 As New Form2()

f2.MdiParent = Me

f2.Show()

End Sub

An example of the MDI application that we have created are shown below

How to create MDI container

How to create MDI container

Step 1

To create an MDI container, go to your Form Properties and then set the value IsMdiContainer to TRUE. As shown in the diagram below

How to remove your MenuItem during RunTime

How to remove your MenuItem during RunTime

Step 1

To remove your menu item, you need to know the structure of your menu. In this tutorial we have File (menuitem1) menu. Then under file we have Help (menuitem2), test1 (menuitem4), test2(menuitem5), Delete Item (menuitem6)

Initially our menu look like this.

Step 2

Let’s say we will remove test1 from our menu. To do that, we need to go from MenuItem1 which is our main menu item. Then under it we have menuitem4. So to remove it we use the following code.

Private Sub MenuItem6_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem6.Click

MenuItem1.MenuItems.Remove(MenuItem4)

End Sub

Output from the menuitem that we have removed.

How to disable your MenuItem

How to disable your MenuItem

Step 1

Disabling or Enabling your MenuItem is really easy. All you need to do is determine what is the ID of your Menu Item that you would like to disable or enable

Step 2

Then use Enable() to set it

How to create your menu during runtime

How to create your menu during runtime

Step 1

Now you will create a menu at runtime, all you need to do is use the following code and put it in a button command or a menu. In this example we will use a submenu.

This is an example of a menu before you activate your runtime menu

Step 2

Once you click on your menu it will become something like this. Your File menu has been changed to Tools

Private Sub MenuItem5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem5.Click

Dim rtmenu = New MainMenu()

Dim rtmenuitem = New MenuItem()

rtmenuitem = New MenuItem("Tools", New System.EventHandler(AddressOf MenuItem2_Click))

rtmenuitem.MenuItems.Add("&Test", New System.EventHandler(AddressOf MenuItem2_Click))

rtmenu.MenuItems.Add(rtmenuitem)

Me.Menu = rtmenu

End Sub

How to create menu item Runtime Concepts

How to create menu item Runtime Concepts

Step 1

To create a menu item in runtime, you will need to create a Menu item that will hold your menuitem. Then you will need to create menuitems. After you have created it, associate each event with this menu. Then bind menitems to main menu. Finally set your form menu to this newly created menu.

How to load a second set of menu

How to load a second set of menu

Step 1

To load a second set of menu, first you have to create another menu item as shown in the diagram below

Step 2

Then under one of your sub-menu item, double click on it and then use the following codes to load the second set of menu that you have just created.

Private Sub MenuItem4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem4.Click

Me.Menu = MainMenu2

End Sub

How to add event handler to your menu item

How to add event handler to your menu item

Step 1

To add event handler to your menu item, just double click on your menu item. The code window will appear. Then we will provide a simple messagebox when you have click on it as show here.

Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click

MsgBox("Hi, you have just clicked on File->About Menu", MsgBoxStyle.OKOnly, "System Message")

End Sub

Step 2

Then press F5 to run you application.

The output is shown above.

How to create menu in VB.NET

How to create menu in VB.NET

Step 1

To create a menu, drag the menu control from your toolbox to your form as shown in the diagram below:

Step 2

Then to create a menu, under the Type Here, just type in the name of your menu. In this example we will create File menu.

Step 3

Then if you would like to create a sub menu item, enter a name in Type Here just below the main menu that you have just provided.

Step 4

We will call this sub menu Help.

How to change your SQLcommand during runtime

How to change your SQLcommand during runtime

Step 1

To change your SQL statement during runtime, all you need to do is set certain parameter to your SQL statement

The code bellows shows that we have set OleDbSelectCommand.CommandText to Select * from Employee which is another table in your database.

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

Me.OleDbSelectCommand1.CommandText = "SELECT * from employee"

Me.OleDbSelectCommand1.Connection = Me.OleDbConnection1

OleDbDataAdapter1.SelectCommand = OleDbSelectCommand1

OleDbDataAdapter1.Fill(DataSet11)

End Sub

Step 2

We need to create a button with the text Populate with Employee. Then double click on it to enter the code provided above.

How to bind data to your control item

How to bind data to your control item

Step 1

To bind your control item, for example TextBox, you need to create a TextBox. Then under its DataBinding properties, set the Text to a DataSet properties called name.

This will create a direct binding between these items.

How to populate your DataGrid

How to populate your DataGrid

Step 1

To populate your Dataset, create another Button on your form. When we click on it, your DataSet will be populated and then it will be reflected in your DataGrid.

Step 2

Create a button with its text Populate and then double click on it to go to Code window.

Step 3

We will use DataAdapter method called Fill() to populate our data in the datagrid.

The method is shown below.

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

OleDbDataAdapter1.Fill(DataSet11)

End Sub

Dataset11 is the name of our dataset given earlier.

Step 4

The output result when you click on Populate button.

How to bind your Datagrid to Dataset

How to bind your Datagrid to Dataset

Step 1

To bind your datagrid to a Dataset that you have created earlier, click on DataGrid properties if it is not chosen. Then in its Properties set the DataSource to Dataset1.Profile.

Step 2

Then your DataGrid would change accordingly when its DataSource is set. If you run your application now, this will appear. This is because it is not populated yet.

How to create your Datagrid

How to create your Datagrid

Step 1

To create your datagrid, just click on Window form tab in your toolbox. Then choose Datagrid from there.

Step 2

Then we will draw out our Datagrid as shown in the diagram above.

How to view your DataSet Properties

How to view your DataSet Properties

Step 1

To view the DataSet that we have created earlier, just right click on your Dataset and then under the popup menu select Dataset Properties as shown in the diagram below.

Another window will appear as shown below.

From here, users are able to view Dataset that contains Table and its fields.

How to generate Dataset from OleDBDataAdapter that you have created

How to generate Dataset from OleDBDataAdapter that you have created

Step 1

To create a dataset, just go to Data tab in your toolbox and then select DataSet

Another window will appear as shown below

Step 2

Since there’s only one OleDBDataAdapter, we will choose Profile which we have created earlier.

Step 3

Click on Ok when done.

How to connect to database

How to connect to database

Step 1

To connect to your database, just click on the Data tab under your Toolbox. The click on OleDBDataAdapter and pull it to your form.

Data Adapter Configuration wizard will appear.

Step 2

Click Next to continue.

Step 3

In the Choose connection, click on New Connection

Step 4

Select Microsoft Jet 4.0 OLED DB Provider and click Next.

Step 5

We will use an Access database that is located in C:\inetpub\wwwroot\net as show above.

Then you can set the access permission to your database.

Step 6

Click on Ok when done.

Step 7

Click on Next to continue.

Step 8

Verify that use SQL Statement is chosen and click on Next to continue.

Step 9

In the generate SQL statement, just use an SQL that will be able to retrieve data from your database. In this example we will use SELECT * FROM PROFILE where profile is a table in this database.

Step 10

Click on Next to continue.

Step 11

Click on Finish to complete it.

Note that OleDBConnection is automatically generated.

How to step through your program

How to step through your program

Stepping through your codes provide an easy and efficient way of debugging your application. Step through your code is normally implemented together with breakpoint.

Step 1

To step through your code just press F8 and then each line of your code is executed one at a time – every time you press it.

How to set breakpoint

How to set breakpoint

Step 1

To set breakpoint in VB.NET, go to the line of your code that you would like to establish breakpoint. In this example here, we would like to create breakpoint in the class definition. Place your cursor there and then press F9 to create a breakpoint.

How to add references in VB.Net

How to add references in VB.Net

Step 1

To add references in VB.Net either to use a NET or a COM component, go to Project and select Add Reference

As shown in the diagram above.

In this example we will create a reference to a Microsoft ADO 2.7 COM.

Step 2

Click on COM tab and then choose Microsoft ADO Ext 2.7.

Step 3

Double click on it and then click on Ok to continue.

How to use your class

How to use your class

Step 1

To use your class, you can try the following. This will actually set your BookName to a value in the textbook. So when you click on Click Me this will then return the name of the book that you have set using the GetBookNme method defined in your class earlier.

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

Dim mystr As String

Dim cbook As Book

cbook = New Book()

cbook.SetBookName = TextBox1.Text

mystr = cbook.GetBookName

MsgBox(mystr, MsgBoxStyle.OKOnly)

End Sub

How to declare you Dataset

How to declare you Dataset

Step 1

To declare you dataset in VB.NET all you need to do is use the DIM keyword

The following code will declare a Dataset called Product

Dim ds as new Dataset("Product")

How to declare your class property in VB.Net

How to declare your class property in VB.Net

Step 1

To declare class property in VB.net, use the keyword Property and together with Get and Set method.

Private BookName As String

Property SetBookName() As String

Get

Return BookName

End Get

Set(ByVal Value As String)

BookName = Value

End Set

End Property

ReadOnly Property GetBookName() As String

Get

Return BookName

End Get

End Property

This actually defines a class with 2 method called GetBookName() and SetbookName()

How to add class in VB.Net

How to add class in VB.Net

Step 1

To add a new class in VB.NET, go to File and select Add Existing Item

Step 2

Then when a new window appear, click on Class

Step 3

Click on Open to create a class.

This create a class called Class1

Public Class Class1

End Class