|
|||||||||||||||||||||||||
|
|||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionAfter writing the article 'Using Paneled Forms in Windows Applications', I decided to spice up something in VS 2005 Beta 2. Though at first my intentions were to write a simple application exploring the How-to of Paneled Forms development in Beta 2, as I delved more and more into the studio, I found myself adding more and more stuff to the application. In the end, I ended up with a simple address book, using Paneled Forms in VB.NET with SQL Express as back-end. Paneled FormsI think I'll start with Paneled Forms first and explain the how-to of them in Visual Studio 2005. As explained in my previous article, "Using Paneled Forms in Windows Applications", we can use a single form with a panel control that can be used to load user controls. What does user controls offer? Everything, except having the ability to act as a form, i.e. a form is independent, but a user control is dependent. A user control is a Type, whereas a form is a Member of the Windows application. (Create a project, and delete any forms, try creating a user control as start-up, the following error is returned: 'UserControl1' is a type in 'TestApp' and cannot be used as an expression.) It is important to mention here that user controls have almost all the properties as a Form… for instance, we can add menus to each individual user control, set opacity, auto size the control, etc. Here I would like to request those of you who haven’t read the article 'Using Paneled Forms in Windows Applications' to read that article before proceeding further, as I won't be explaining the process in full detail. As in my previous application, this application has one Windows Form, 'myMainForm' and various user controls. This single form contains a Panel, ' Dim tempObject As New ctrlMain
Me.myPanel.Controls.Clear()
Me.myPanel.Controls.Add(tempObject)
I have included these lines in the Now, we want to transfer to another control, taking the example from our application, let’s say we click the button labeled 'Add New Entry', this calls for swapping our current control with Dim tempObject As New ctrlAddNewEntry
Me.ParentForm.Controls.Item("myPanel").Controls.Add(tempObject)
Me.ParentForm.Controls.Item("myPanel").Controls.RemoveAt(0)
In the first line we create an object of the control The tricky thing is that we are not functioning from our main form, i.e. Also note the major difference between Paneled Forms in VS 2003 and VS 2005, we are accessing the Panel by using its name, if you remember my previous article, we had to use the index of the item, from the designer generated code to access it, no names. This definitely is quite handy, as there’s no chance of getting the wrong index in this case!!! The third line of code removes the control at index 0, from the control collection of the panel. The control collection acts as a queue, thus following the FIFO rule, 'First in First out'. So, when we add a new control to our panel’s control collection, it gets stored at index 1. Upon removal of the control at index 0, the new control moves to index 0 from 1. Warning!!! In common practice, coders try to use Now if we want to swap our user control again, we can use the same three lines of code, the only change being that we will be creating the object of the control we want to swap to. Multi-splash screensNow that we’ve covered the how-to of Paneled Forms in VS 2005, let’s get started on splash screens. Splash screens are often used to display information to a user while an application is loading. They are basically a Windows Form without any form border. Visual Basic provides a preconfigured splash screen form template that can be added to a Windows application project, and a splash screen property in the Project Designer that allows specifying a splash screen for a project. Creating a splash screenTo create a splash screen simply add a Windows Form to the project. Open the form in designer mode, right click on it and open its properties. Scroll down to the Appearance properties of the form and set the Now choose an image that you’d like to use as your splash screen and add it as the background image of the form. I usually add two labels to my splash screens and they are as follows: 1. Application TitleThis label will show the application title on the splash screen. We can set its value on the My.Application.Info.Title
But there is a good chance that the application title is empty, unless we specify it beforehand. To take care of this, we can use the following check: My.Application.Info.Title <> ""
If its true then use this value else we can get the file name of the application without the extension by calling the following method: System.IO.Path.GetFileNameWithoutExtension()
Parameter: My.Application.Info.AssemblyName
2. VersionAnother thing a user might want to see on a splash screen is the version information. I am using the The next thing to do is to have the splash screen show up before any other form in the application. The fastest and easiest way is to use the Project Designer’s splash screen property (Figure 4). But the problem with this is that it is hard to control how long the splash screen stays opened, when it loses focus, how it appears, and to add additional functionality to it. My splash screensI'll jump directly to the splash screen that I am using and how I am using it. The splash screen is shown in Figure 5 . As my application’s startup form is myMainForm, I have added the following code to its 'Splash Screen Call – Version 0.02
mySplashScreen.ShowDialog()
Confused!!! I know. First I was using the following syntax: 'Splash Screen Call – Version 0.01
Dim tempForm As New mySplashScreen
tempForm.ShowDialog()
Why did I change it? Why did I not create an object of Let’s add the following text to If My.Application.Info.Title <> "" Then
ApplicationTitle.Text = My.Application.Info.Title
Else
ApplicationTitle.Text =
System.IO.Path.GetFileNameWithoutExtension(_
My.Application.Info.AssemblyName)
End If
Version.Text = System.String.Format(Version.Text, _
My.Application.Info.Version.Major, _
My.Application.Info.Version.Minor)
Copyright.Text = My.Application.Info.Copyright
As you can guess, this is the code from the default splash screen template. What it does is set the values for the labels on the splash screen. Next add a timer control to the splash screen. We will initialize this control in the same Public Shared opacityRate As Double = 0.0
Public Shared maximizeRate As Boolean = True
Public Shared minimizeRate As Boolean = False
Public Shared killApplication As Boolean = False
Now let’s add code to our Me.Opacity = 0.0
timerSplash.Interval = 50
timerSplash.Enabled = True
timerSplash.Start()
In the first line, I am setting the form’s opacity, i.e. the splash screen’s opacity equal to zero… Thus in the initial stage it will not be visible. Then I am setting the With this ends our splash screen’s The If opacityRate >= 1.0 Then
opacityRate = opacityRate + 1.0
If opacityRate >= 20.0 Then
'Try
'Dim tempBoolean As Boolean = DataLayer.Open()
'If tempBoolean = False Then
'killApplication = True
'End If
'Catch ex As Exception
'killApplication = True
'End Try
opacityRate = 0.99
Me.Opacity = opacityRate
End If
ElseIf maximizeRate Then
opacityRate = opacityRate + 0.025
Me.Opacity = opacityRate
If opacityRate >= 1.0 Then
maximizeRate = False
minimizeRate = True
End If
ElseIf minimizeRate Then
opacityRate = opacityRate - 0.025
If opacityRate < 0 Then
opacityRate = 0
End If
Me.Opacity = opacityRate
If Opacity <= 0.0 Then
minimizeRate = False
maximizeRate = False
End If
Else
timerSplash.Stop()
timerSplash.Enabled = False
timerSplash.Dispose()
Me.Close()
End If
What the above code does is: when the splash screen is called, it materializes into view stays solid for a second and then materializes back. During initialization I am setting four variables. Out of which three; By default, on initialization, the One can easily grasp how the At first this did wonders for me, I used the splash screen call version 0.01, the splash screen showed up before the startup form, and as it closed the main application form appeared. Then I decided to add a test to myMainForm. What I wanted to do was to check whether SQL Express existed and if I could connect to it. In my DataLayer.vb file, which I’ll discuss in detail when I get to SQL Express, I created a function After the splash screen call version 0.01, I added the following code to the Dim tempBoolean As Boolean = DataLayer.Open()
If tempBoolean = False Then
Dim tempForm As New myMessageBox
tempForm.Text = "Error!"
tempForm.lblHeader.Text = "Database Communication Error!!!"
Dim temp(2) As String
temp(0) = "An error occurred while communicating with
database. Please check if SQL Express is
Running or that it exists."
temp(1) = ""
temp(2) = "Application will now exit."
tempForm.txtMessage.Lines = temp
tempForm.ShowDialog()
Me.Close()
End If
The result was as predicted, after showing the splash screen, the application checked for connectivity with the database and if it failed showed a message box and then exited. It is important to mention here that I am using a custom message box, In the best case, when the database connection was established, this worked really good, but when the database could not be connected, it took its sweet time. Thus, after the splash screen closed, a long interval came before either I then transferred the database connectivity check from In the end I decided to add the code to the splash screen. I added the code in the In the Then I made some changes in Thus, in the end I had the following two functions: Private Sub myMainForm_Load(...,...) Handles MyBase.Load
mySplashScreen.ShowDialog()
Dim tempObject As New ctrlMain
Me.myPanel.Controls.Clear()
Me.myPanel.Controls.Add(tempObject)
End Sub
Private Sub myMainForm_Shown(...,...) Handles MyBase.Shown
If mySplashScreen.killApplication Then
Dim tempForm As New myMessageBox
tempForm.Text = "Error!"
tempForm.lblHeader.Text = "Database Communication Error!!!"
Dim temp(2) As String
temp(0) = "An error occurred while communicating with
database. Please check if SQL Express is
Running or that it exists."
temp(1) = ""
temp(2) = "Application will now exit."
tempForm.txtMessage.Lines = temp
tempForm.ShowDialog()
Me.Close()
End IfIf
End Sub
After this resulted in what I wanted, I decided to add another splash screen, a screen that will be displayed when the user quits the application, my endScreen, as shown in Figure 7: Private Sub endScreen_Load(..., ...) Handles MyBase.Load
timerExit.Interval = 1500
timerExit.Enabled = True
timerExit.Start()
End Sub
Private Sub timerExit_Tick(..., ...) Handles timerExit.Tick
timerExit.Stop()
timerExit.Enabled = False
timerExit.Dispose()
Me.Close()
End Sub
As in the previous splash screen, I am calling the splash screen from Private Sub myMainForm_FormClosing(..., ...) Handles MyBase.FormClosing
Me.Hide()
endScreen.ShowDialog()
Try
DataLayer.Close()
Catch ex As Exception
End Try
End Sub
As explained earlier, calling the One thing I forgot to mention is that the connection established in Important: If you want your splash screens to work around the edges, i.e. the form ends where the image does even if it isn’t rectangular, then make your splash screen images either .PNG or .GIF. Look at figure 5, it is the splash screen that I am using, if I click somewhere outside the edge of the image, I click the desktop. Similarly, look at figure 10; if I click anywhere that is not on the image, even the area in between the green border, then I click on the desktop. I am going to include the splash screen’s .PNG image in the resources folder; you can change the background image of But remember, this feature is automated for Visual Studio 2005 only, this doesn’t work in Visual Studio 2003 or in earlier versions. SQL ExpressAdding a SQL Express database to your application is as easy as adding a Windows Forms. Just right-click on your project in the Solution Explorer and select Add>New Item. Select the SQL database from the listed Visual Studio installed templates, it should appear if you have installed SQL Express. Click on Add. The database is created and an icon appears in the Solution Explorer. A new window pops up, this is the default dataset builder. Close it, as we won’t be using that. Double click on the database from the Solution Explorer and the database is loaded into the server explorer. To add a new table, right-click on Tables folder and select Add New Table, and to add a new stored procedure, right-click on Stored procedures and select, Add New Stored Procedure. I won’t go into how-to of defining tables and stored procedures, but I’d like you to take your time and explore SQL Express, especially the query builder, which is quite handy in building complex queries. Note: An application can contain more than on SQL Express database. Let’s start on DataLayer.vb, this is the class containing all the functions and calls to the database. I declared two shared variables; Public Shared dataCommand As New Data.SqlClient.SqlCommand
Public Shared sqlConnection1 As New Data.SqlClient.SqlConnection(...)
The parameter to Next we have to open a connection to the database: Public Shared Function Open() As Boolean
Try
dataCommand.CommandType = Data.CommandType.StoredProcedure
dataCommand.Connection = sqlConnection1
sqlConnection1.Open()
If sqlConnection1.State = Data.ConnectionState.Open Then
Return True
Else
Return False
End If
Catch ex As Exception
Debug.Write("While Opening the connection to the DB: " + _
ex.Message)
Return False
End Try
End Function
And that’s all there is to it… Next let’s see how to get data from the database using a stored procedure: Public Shared Function selectDetails(ByVal id As String) As _
Data.SqlClient.SqlDataReader
Dim reader As Data.SqlClient.SqlDataReader
Try
dataCommand.CommandText = "selectDetails"
dataCommand.Parameters.Clear()
dataCommand.Parameters.Add("@id", Data.SqlDbType.VarChar)
dataCommand.Parameters("@id").Value = id
reader = dataCommand.ExecuteReader()
Return reader
Catch ex As Exception
Debug.Write(ex.Message)
Return Nothing
End Try
End Function
The function above takes a parameter ID, calls a stored procedure ' An important thing to note here is that since I am using a shared The previous function was using a Public Shared Function selectAllRecord() As Data.DataSet
Dim ds As New Data.DataSet
Try
Dim adapter As New Data.SqlClient.SqlDataAdapter
adapter.SelectCommand = New Data.SqlClient.SqlCommand(_
"selectAllRecord", sqlConnection1)
adapter.Fill(ds, "tblAddressBook")
Return ds
Catch ex As Exception
Debug.Write(ex.Message)
Return Nothing
End Try
End Function
Similarly, we can execute non-queries (updates, insert, etc.) using Furthermore, we can use views, functions, and many more in SQL Express to enhance our application. One thing that I’ll definitely be looking into is using more than one database in an application, when data between them has to be cross-referenced. Who knows… maybe that’ll be my next article… Other stuff!!!The main emphasis in this article was on Paneled Forms, splash screens, and SQL Express, but apart from that, when you download the application/code, you will find a very rich usage of And all things must come to an end…I hope you found this article interesting and informative. As I said in my previous article, I am open for suggestions and remarks, both negative and positive. Feel free to contact me at msk.psycho@gmail.com. Hopefully I’ll be back soon with another article. Till then… Bye.
|
||||||||||||||||||||||||