While you're in Microsoft Access creating the tables, go ahead and add a few topics, one or more threads for each topic and a message or two for each thread.. Of course, if you do want e
Trang 1Field Name Data Type
The Threads table is a little more complicated, but not much (see Table 30-2)
Table 30-2: The Threads table
By setting these attributes, you assure that whenever a new row is added, the Name field is filled in and that it is filled in with a value that is different from any of the other names already in the table In other words, all threads must have a unique name The same attributes could have been set for the Topics table's Name field, but because it's the site administrator (and not users) who will be filling that table in, you can probably assume that the administrator will assure that the topics are right
The Messages table has the most columns (see Table 30-4)
Table 30-4: The Messages table
Trang 2The Posted column is important It enables you to organize the messages in the right order within a thread
Finally, Thread holds the ThreadID of the row in the Threads table with which this message is associated
Creating the Database
Once you've decided to go with a relational database, you're going to have to figure out how you want to organize the information into tables and columns
Chatty has two levels of organization for its messages:
§ Topics are the highest level and are chosen by the creator of the site
§ Threads are essentially subtopics that the user can create
Messages, then, are always posted by the user to a thread, which in turn is under a topic
These storage needs can be accommodated with three tables: Topics, Threads, and Messages
Topics is the highest level, but is also the simplest All you have to track is the topic's name Table 30-1 shows the columns and data types
Table 30-1: The Topics table
The Threads table is a little more complicated, but not much (see Table 30-2)
Table 30-2: The Threads table
By setting these attributes, you assure that whenever a new row is added, the Name field is filled in and that it is filled in with a value that is different from any of the other names already in the table In other words, all threads must have a unique name
Trang 3The same attributes could have been set for the Topics table's Name field, but because it's the site administrator (and not users) who will be filling that table in, you can probably assume that the administrator will assure that the topics are right
The Messages table has the most columns (see Table 30-4)
Table 30-4: The Messages table
The Posted column is important It enables you to organize the messages in the right order within a thread
Finally, Thread holds the ThreadID of the row in the Threads table with which this message is associated
Seeding the Database
In order to test the pages as you create them, you'll want to put some of your own data in the database to get you started While you're in Microsoft Access creating the tables, go ahead and add a few topics, one or more threads for each topic and a message or two for each thread Make sure the foreign keys (like the Topic column in the Thread table and the Thread column in the Message table) refer to the primary keys in rows you've already created in the associated tables
If you decide to use this completed application in your own Web sites, you'll have to use Microsoft Access to enter your topics I haven't included a page to add or update topics since the discussion topics are usually created once by the Web site administrator and aren't often changed after that Of course, if you do want easy access to update or add new topics, you can always use the code I've provided here for creating threads as a starting point for a new page that does what you want
Picking a Topic
When a user visits Chatty, the first decision they have to make is which topic they want
to browse The Topics.aspx page, shown here, provides that opportunity:
<%@ Page Explicit="True" Language="VB"
Trang 4If Not isPostBack Then
Dim TopicConnection As OleDbConnection Dim TopicCommand As OleDbCommand TopicConnection = New OleDbConnection( _ "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=" & _
"c:\inetpub\wwwroot\Discuss\DiscussDB.mdb") TopicCommand = New OleDbCommand( _ "Select TopicID, Name from Topics", _
Trang 5Retrieving the topics
When the page is first retrieved, a connection is made to the Access database, and a command object is created with a Select statement that retrieves the topic information:
If Not isPostBack Then
Dim TopicConnection As OleDbConnection
Dim TopicCommand As OleDbCommand
TopicConnection = New OleDbConnection( _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & _
"c:\inetpub\wwwroot\Discuss\DiscussDB.mdb")
TopicCommand = New OleDbCommand( _
"Select TopicID, Name from Topics", _
TopicConnection )
TopicConnection.Open()
The Command object's ExecuteReader method is called, which executes the Select statement and then returns the results in the form of a DataReader object This object is immediately assigned to the DataSource property of the TopicDataList control on this page Calling the DataBind method of that control assures that the topics retrieved will appear there:
TopicDataList.DataSource = _
TopicCommand.ExecuteReader()
TopicDataList.DataBind()
Displaying the topics
The DataList control is used to quickly and easily display and format the information retrieved from the database:
Trang 6The ItemTemplate tag is used to identify how individual items in this list should be displayed In this case, I only want to display one item, the name of the topic But I want
to display it as a link The LinkButton control makes this easy The text is set to the value
of the Container's "Name" data item The container, in this case, is the DataList, and because it is bound to the Topics table result set, this will display the name of the topic The CommandArgument is set to the same value This assures that the Topic name is passed to the appropriate subroutine when the user clicks a topic
Handling topic selection
When the user clicks a topic, the DataList's OnItemCommand attribute tells the control what to do In this case, it tells it to call the SelectTopic subroutine:
Sub SelectTopic(s As Object, _
So I use that list to retrieve the DataKey at the current ItemIndex With the topic ID and name in hand, I'm ready to pass control on to another page, Threads.aspx
Trang 7The Threads page is used to display the list of threads within the selected topic But to
do that, it must know which topic was chosen I pass the topic ID and the topic name as arguments on the URL line This makes them available to be accessed by name in the Threads page by using Request.QueryString
Picking a Thread
Once the user has picked a topic that they're interested in, the Threads page enables them to see all the threads that have been created in that topic It also provides a link that allows the user to go to a page and create a whole new thread, as show in here:
<%@ Page Explicit="True" Language="VB"
Sub Page_Load( s As Object, e As EventArgs )
If Not isPostBack Then
Dim ThreadConnection As OleDbConnection
Dim ThreadCommand As OleDbCommand
ThreadConnection = New OleDbConnection( _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & _
"c:\inetpub\wwwroot\Discuss\DiscussDB.mdb")
ThreadCommand = New OleDbCommand( _
"Select ThreadID, Name from Threads " & _
"Where Topic=" & _
Trang 9Using TopicID as selection criteria
There are a few differences between this page and the Topics page This page uses the TopicID in the query to get only the threads associated with the topic the user requested: ThreadCommand = New OleDbCommand( _
"Select ThreadID, Name from Threads " & _
"Where Topic=" & _
Request.QueryString("TopicID"), _
ThreadConnection )
I do this simply by concatenating the information from the QueryString into my Select statement I could have, instead, used a parameter and filled in the parame ter with the TopicID I decided not to in this case because it would have made the code more
complicated and wouldn't really add any value I do use parameters in both the
Messages and the NewThread pages, and I'll explain why in a section called "Using parameters" later in this chapter
The Thread DataList
Another difference from the Topics page is the DataList:
Trang 10Selecting a thread
When the user clicks one of the threads in the DataList, the SelectThread subroutine
is called Again, this works much the same as the SelectTopic subroutine does in the Topics page The only real difference is that it passes the thread ID and name to the Messages page as well as the topic ID and name as you can see here:
Response.Redirect("Messages.aspx?TopicID=" & _
Request.QueryString("TopicID") & _
"&TopicName=" & Request.QueryString("TopicName") & _
"&ThreadID=" & ThreadID & _
"&ThreadName=" & ThreadName)
Browsing Messages
The Messages page displays all the messages posted for a particular topic and thread:
<%@ Page Explicit="True" Language="VB" Debug="True" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<Script Runat="Server">
Sub Page_Load( s As Object, e As EventArgs )
If Not isPostBack Then
Dim MessageConnection As OleDbConnection
Dim MessageCommand As OleDbCommand
MessageConnection = New OleDbConnection( _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=c:\inetpub\wwwroot\Discuss\DiscussDB.mdb")
MessageConnection.Open()
Trang 11
MessageCommand = New OleDbCommand( _
"Select MessageID, Subject, Message, " & _
"Author, Posted From Messages Where " & _
"Thread=@ThreadID Order By Posted", _
Sub PostClick( obj As Object, e As EventArgs )
Dim MessageConnection As OleDbConnection
Dim MessageCommand As OleDbCommand
Dim InsertString as string
Dim Message As String
MessageConnection = _
New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=c:\inetpub\wwwroot\Discuss\DiscussDB.mdb") InsertString = "Insert Into Messages (Subject, Message, " & _ "Author, Posted, Thread) VALUES (@Subject, @Message, " & _ "@Author, @Posted, @ThreadID)"
Trang 13<table width="100%" border="1" cellspacing="0"
cellpadding="3" bordercolor="DarkBlue">
<tr bgcolor="DarkBlue"><td>
<font color="White" face="arial"><b>
<%# Container.DataItem("Subject") %></b></font> </td>
<td align="right" >
<font color="White" face="arial" size="1">
<%# Container.DataItem("Posted") %></font><br/> </td></tr><tr><td colspan="2">
<font face="arial" size="2">
<p>To post a new message to this thread, type your
message below and click Post.</p>
<p><font size="2">Your Email Address:</font><br />
<asp:textbox id="AuthorText" runat="server"
Trang 14Retrieving the messages
All the messages for all the topics and threads are stored in the same table So to retrieve only the appropriate messages for this page, I concatenate the ThreadID sent as
a QueryString to this page:
MessageCommand = New OleDbCommand( _
"Select MessageID, Subject, Message, " & _
"Author, Posted From Messages Where " & _
"Thread=" & Request.QueryString("ThreadID") & _
" Order By Posted", MessageConnection )
Trang 15In addition, I made the topic name a link When you click it, you are returned to the Threads page, showing the threads for this topic
Displaying the messages
I use a Repeater to display the messages retrieved from the database:
to make the messages look presentable and easy to read So I decide a Repeater would
be a better choice than the DataList
Each message appears inside its own table The table is as wide as the page and has a border all the way around it The Subject line appears white on dark blue, while the message appears below it as the standard black text on a white background The author's e-mail address appears after the message
Links to threads and topics
Typically, the user will come to the Messages page, read through the messages, and then click one of the links at the bottom of the page to go to either the Topics page or back to the Threads page That's what this code provides:
Trang 16Return to list of <b>Topics</b></a>
The new message form
However, sometimes, the reader will decide to add a message to the forum For this, I've provided a form at the bottom of the page
The form itself
I could have placed a link here at the bottom of the Messages page, instead, and put the form on a different page But I decided that having the form readily available would make people more likely to post and be active in the conversations
<table width="100%" border="1" cellspacing="0"
cellpadding="3" bordercolor="DarkBlue">
<tr><td bgcolor="LightGrey">
<p>To post a new message to this thread, type your
message below and click Post.</p>
<p><font size="2">Your Email Address:</font><br />
<asp:textbox id="AuthorText" runat="server"
Sub PostClick( obj As Object, e As EventArgs )
Dim MessageConnection As OleDbConnection
Dim MessageCommand As OleDbCommand
Dim InsertString as string
Dim Message As String
The SQL Insert statement
PostClick inserts a new row in the Message table with the information entered by the user A connection to the database is made, just as you normally would:
MessageConnection = _
Trang 17New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=c:\inetpub\wwwroot\Discuss\DiscussDB.mdb")
In addition, a command object that holds a SQL Insert statement is created:
InsertString = "Insert Into Messages (Subject, Message, " & _
"Author, Posted, Thread) VALUES (@Subject, @Message, " & _
"@Author, @Posted, @ThreadID)"
MessageCommand = _
New OleDbCommand(InsertString, MessageConnection)
However, you'll notice that the data to be filled in isn't concatenated to the string that contains the Insert statement Instead of using concatenation as I did in the Select statement of the BindData subroutine (and in the Select statement used in the Threads page), I decided, here, to use parameters
Using parameters
Parameters enable you to identify several placeholders in your SQL statement that are to
be filled in later The placeholders always begin with an @ sign so that you don't miss them or confuse them with columns from your table
After the command object is created, you add new OleDbParameter objects to the Parameters collection of the command — one for each placeholder in your SQL
New OleDbParameter("@ThreadID", OleDbType.Varchar,50))
When you instantiate the OleDbParameter, you pass the name of the placeholder and the data type of the information (using the OleDbType enumeration object)
Finally, after you have created all the parameters, you can fill them in with their
Trang 18to read and understand
But that's not the best reason to use parameters Suppose the user enters a subject that looks like this:
Can't afford the migration cost
Notice the apostrophe in Can't When you concatenate strings, VB.NET will interpret any
apostrophes as a single quote and will think you are starting a substring, which you didn't finish Double quotes in the string cause even worse problems Fortunately, using
parameters to fill in your SQL statement avoids all of these problems Your quotes will come out looking just as they were typed
Getting hard returns right
There's a couple of lines I skipped as I was describing the Messages page in the
displayed, the browser ignores hard returns So, to be sure the text displayed in the browser looks like the text the user entered, it's necessary to do a little replacement That's what the preceding lines do
Preparing to return the updated page
After the user has entered a new message, and clicked Post, the new message is added
to the database and the updated Messages page appears To make that happen, you have to do two things: clear out the form fields and re-retrieve the messages from the database:
Creating a New Thread
One more page is needed to make this application complete: a link from the Threads page that allows the user to create a new thread This link sends them to the NewThread page
<%@ Page Explicit="True" Language="VB" Debug="True" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
Trang 19
<Script Runat="Server">
Sub PostClick( obj As Object, e As EventArgs )
Dim Connection As OleDbConnection
Dim ThreadCommand As OleDbCommand
Dim GetThreadCommand As OleDbCommand
Dim MessageCommand As OleDbCommand
Dim ThreadReader As OleDbDataReader
Dim InsertThread, InsertMessage As String
Dim GetThread, Message as string
Dim ThreadID As Long
Dim ThreadResult As Integer
Connection = New OleDbConnection( _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=c:\inetpub\wwwroot\Discuss\DiscussDB.mdb")
Feedback.Text = "<font color=red>*** There " & _
"is already a thread with the name <b>" & _
ThreadText.Text & "</b> Please choose a " & _
"different name and click Post.</font><br><br>"
Else
Feedback.Text = "<font color=red>*** " & _
Trang 20InsertMessage = "Insert Into Messages (Subject, " & _
"Message, Author, Posted, Thread) VALUES (@Subject, " & _ "@Message, @Author, @Posted, @ThreadID)"
MessageCommand.ExecuteNonQuery
Connection.Close()
Trang 21<h1><font color="DarkRed">Chatty Discussion Forum</font></h1>
<font size="5">Create New <u>
<%=Request.QueryString("TopicName") %></u> Thread</font>
<p>To create a new thread, enter the name of the thread
and the information for the first message you'd like
to post to the thread If you wish to cancel, simply click
one of the links at the bottom of this page.</p>
<asp:label id="Feedback" runat="server" />
<p><font size="2">New Thread Name:</font><br/>
<asp:textbox id="ThreadText" runat="server"
<p><font size="2">Your Email Address:</font><br />
<asp:textbox id="AuthorText" runat="server"
Trang 22The NewThread form
Unlike the other pages in this application, NewThread does not retrieve anything from the database when it is first opened Instead, it presents the user with a form that looks very much like the form at the bottom of the Messages page:
<asp:label id="Feedback" runat="server" />
<p><font size="2">New Thread Name:</font><br/>
<asp:textbox id="ThreadText" runat="server"
<p><font size="2">Your Email Address:</font><br />
<asp:textbox id="AuthorText" runat="server"
The user is here to create a new thread However, it doesn't make much sense to create
a new thread without also posting a message to it So to save the user the trouble, this page allows the user to do both at once
NewThread's PostClick: a big job
After the user has entered the thread name and the first message for the thread, he or she clicks the Post button The Post button causes the page to return to the server and execute the PostClick subroutine
PostClick has a number of responsibilities It must do the following:
§ Open a connection to the database
Trang 23§ Insert a new row in the Threads table using the name the user entered and associating it with the current topic
§ Retrieve the newly created thread's ThreadID (which is assigned to it automatically by Microsoft Access)
§ Insert a new row in the Message table using the rest of the information the user entered in the form That message must be associated with the newly created thread, using the retrieved ThreadID
Threading a new row
Aside from the primary key, the Threads table has only two columns: the thread name and the topic the thread is associated with I use parameters to fill in these values: InsertThread = "Insert Into Threads (Name, Topic) " & _
Watch for duplicates
When I created this database, I didn't want the user creating threads with the same name, so I created an index on the Threads table's Name column that didn't allow duplicates But because I did that, I now have to check for that possibility in my code That's made relatively painless with VB.NET's new error-trapping capabilities:
Feedback.Text = "<font color=red>*** There " & _
"is already a thread with the name <b>" & _
ThreadText.Text & "</b> Please choose a " & _
"different name and click Post.</font><br><br>"
Trang 24If that is the problem, I use the Feedback label to provide the user with a simple
message informing them of the problem and asking them to change the name of the thread
If there's some other problem, I simply drop the error message into the Feedback label
Getting the ThreadID
When you use the AutoNumber data type with a primary key column in an Access table, Access knows to automatically generate a new, unique number for any newly inserted rows
But to post the first message to this thread, you're going to need the ThreadID How do you get it? Just query for the unique thread name:
GetThread = "Select ThreadID from Threads Where Name='" & _
Posting the messa ge
Now that you have all the information you need, posting the message works much like it did in the Messages page:
InsertMessage = "Insert Into Messages (Subject, " & _
"Message, Author, Posted, Thread) VALUES (@Subject, " & _
"@Message, @Author, @Posted, @ThreadID)"
Trang 25Ideas for Enhancement
The Chatty Discussion Forum is a simple application It could be enhanced or expanded
to meet your needs in many ways Here are a few ideas that I thought of:
§ In its current form, Chatty offers no way to add, update, or delete topics
Because this is a site administrator task, it's assumed that they'll simply use Microsoft Access to go in and add the topics they want when they first
begin using the application But it would be handy if there was a topic
maintenance page that simplified the task Keep in mind that deleting a
topic means you need to delete all the threads and all the threads'
messages!
§ Likewise, Chatty has no way to delete or archive old threads and messages Again, this can be done by hand in Access, but if you begin to have a lot of active discussions going on, it'd be nice if there were maintenance pages that made the task even easier
§ While you're making maintenance easier, you might want to consider
automating some of the tasks For example, you might want to
automatically archive any thread and its messages that hasn't been posted
to in over a month
§ If you have trouble with people posting off-topic or inappropriate messages, you might want to put newly entered messages into a holding bin to be
approved by a site administrator before they are posted
§ Another way to combat the problem of inappropriate postings is to require
users to log in to your site or at least log in to the discussion forum
application before they post a new message This provides you with more information on who the user is and enables you to cancel their account if they act antisocially
§ Provide a new page (or modify the Threads page) so that you can see all the Subject lines of all the messages in a thread Then, allow the user to click one of the Subject lines to jump directly to that message on the Messages page
Trang 26Summary
A discussion forum is a very practical application It can be used on virtually any Web site where you want to enhance the sense of community In addition, this application has demonstrated a number of important ASP.NET techniques Some of the techniques are creating a hierarchical relationship among tables in a relational database; connecting to and retrieving data from a database using ADO.NET; displaying data with the Repeater and DataList server controls using data-binding; using an ItemTemplate inside a repeater
to format repeating data; creating new rows in a database table; connecting new rows to
an exiting row in another table using appropriate foreign keys; and using parameters in queries — as well as when and why it's important
Overview
Welcome to the start of a whole new adventure for both Microsoft and yourself The NET series of application development tools and its accompanying office suite has the potential to take you much further and much faster in productivity than you have gone before The integration of these tools has become much tighter and thus more efficient than in the past Because this is a book on Web application development with ASP.NET supplemented by VB.NET, you will first be taken on a tour through the VB.NET
development environment This will give you the foundation required for you to be a notch NET developer Then you will be taken on a tour of all the pieces of the
top-construction of an application, from designing an MDI (Multiple Document Interface) application to understanding data types and defining variables, and finally to controlling the logical flow of an application
Brief Tour of the GUI
As an application developer, it is the world of the GUI IDE (graphical user interface, integrated development environment) in which you will spend most of your time
VB.NET, although new and improved over VB 6.0, is still a GUI IDE and therefore will take some getting used to if you are new to software development You will now take a brief tour of this IDE You will see the highlights along the way of what is new or may have changed in look, feel, or functionality The IDE, for the most part, looks similar to that of VB 6.0, so for those developers who are seasoned in VB 6.0, finding your way around this IDE shouldn't be very difficult
It is assumed here that you have successfully installed at least the basic feature set of the VB portion of the NET development suite
Note For more detail on the installation process, refer to this book's
Trang 27Figure A-1: The VB.NET GUI with the major sections highlighted
If you are new to IDE development or to VB.NET, then take a few minutes to try out some of these features:
§ Menu: Usually context-sensitive, gives you a pull-down approach to many
commands and environment options
§ Toolbar: Also context-sensitive, the toolbar is a subset of the commands that
you will find on the menu This is a way to quickly call commands, such as Save, Print, and Run, that are used more frequently than other commands
§ Layout bar: This is another kind of toolbar that supplies many of the layout
features used in designing a form Features like aligning controls to the left, making controls the same size, and making controls equidistant are among the options available Many other context-sensitive toolbars are available
for your use, too; simply right-click any toolbar in a vacant slot and you will see the list of the other available toolbars
§ Solution Explorer: This is a tree-view-style interface that shows you all the
pieces to your application, or solution This shows you all the windows and modules, and any other items of that nature, that are part of the solution
and that you may want to edit
§ Properties window: This is where you will spend a lot of time fine-tuning the
behaviors of your application You set the individual features of a particular object (always the one in focus) so that it appears the way you desire
Some examples are push button text value and size, text label values, form background colors, and so on
§ Toolbox: The selection area for the controls that you want to place on a form
you may be designing Push buttons, drop-down lists, picture buttons,
single-line edits, radio buttons, and check boxes are among the many tools
in the toolbox at your disposal
§ Development area: This is the area used to lay out your forms, to see how
they will appear Grouping controls together, aligning them to the left, right,
or top, and sizing the form are among the functions you can perform here Now that the major areas have been pointed out and briefly explained, you can start building the small mailing list system mentioned earlier
Creating a Mailing List Project
The first thing you need to do is to start a new project Complete the following steps to do so:
1 Either select New Project from the File menu or click the New Project
button on the toolbar The New Project dialog box will appear, as shown
Trang 28in Figure A-2 Here you are presented with quite a few options as to what kind of project you want to create
Figure A-2: The New Project dialog box options
2 Select Visual Basic Projects from the available Project Types, if it is not already highlighted Then, select from the available Templates For the purposes of this example, you will be creating a new Windows
application, so be sure that the Windows Application icon is selected Ignore all the other options for now, or if you are interested in them click
on the help button in this window and read up on these additional
features
3 Name the project "Mailman" or something appropriate, and indicate the correct Location for your project
4 Click OK to create the new project
The new window will appear in the development area With the new window in focus, click its properties tab on the right side of the display and name the new form You can also set its Window Style to an MDI parent by making the IsMdiContainer equal to True (see Figure A-3)
Figure A-3: Creating a new project
Trang 29Many types of Windows are available to the developer VB.NET is based on a few interface styles and GUI presentation needs MDI (Multiple Document Interface), SDI (Single Document Interface), and Web interface styles are a few of the most common that you will see This example application will be an MDI application; however, to keep things small and simple, it will only have a few windows
Tip New to VB.NET is the disappearing sidebar feature If you have
ever seen the Windows taskbar as it is set to autohide, you'll be familiar with this feature as well It is designed to save screen space and it is a great feature to have enabled On the top of the toolbox, for example, you'll see a small icon that looks like a stickpin; click this pin to activate the feature The toolbox will move off to the left and reveal more of the design surface
Setting the data elements
The mailing list system will have a small menu control, a data entry screen, and a Help About window It will also have a small database behind it and therefore will consist of the following features:
§ Address entry
§ Help About window
§ Application shutdown and exit
The database will consist of the data elements defined in Table A-1
Table A-1: Data element items for mailing list database
Autonumber Auto
Note There is a database file located on the companion Web site for
this book A further reference will be made to this file later in the appendix when it will actually be used
Trang 30Adding menu controls
The menu designer is the interface that enables you to define the actions or commands available in your application Commands such as Print, Copy, Edit, Window Tile, and Help About are some of the common ones
Note The mailing list system project is going to have only a few menu
items, so that you can see some of the functionality of VB.NET Feel free to add more functionality when you are ready to do so
To add a menu control to the main form, complete the following steps:
1 Find the MainMenu control on the toolbox window You may have to reveal the toolbox on the left of the display, depending on how you
have your IDE configured
2 Drag the MainMenu control to the main window and drop it anywhere
on the form that is being worked on
3 After the MainMenu control is attached to the form, you can begin to build your menu structure The menu design structure will appear on the top of the form, and you can add the menu items by typing directly into the menu option slots that are presented to you This is a marked difference to the menu designer that VB 6.0 developers are used to The menu designer has the following main areas, which are accessed
in different portions of the IDE:
§ Text: The portion of the menu that the user of the
application actually sees while working with it This is accessed simply by typing the caption in the area on the menu designer; alternately, you can set this value
in the properties tab
§ Name: The "internal" name that is assigned to the menu
item If a menu item is to be referenced in programming code, this is how it will be addressed
Typically, menu names are prefixed with "mnu" as in mnuFilePrint This is done to visually cue the software programmer as to what type of entity is being
referenced in the code This name item is accessed in the menu's property page on the right side of the IDE
§ Short Cut: This is the menu shortcut that can be
assigned to each item Take a look at most Microsoft applications and you see that a shortcut for saving information is usually the Ctrl + S keystroke combination You can have similar shortcuts assigned
to your menu items, but be sure to track the ones you use so that you don't have two or more menu items assigned with the same shortcut
§ Enabled & Visible: These two attributes to a menu item
are the most commonly used of the remaining properties Enabled means that the menu item will trigger its supporting code if selected from a running application The opposite of this, to have Enabled turned off, would be similar to a grayed-out option on a menu It is visible, but because of the context that the application is in, it is inoperable An example would be
an Edit menu with Paste grayed out because nothing
is in the computer memory's clipboard to paste The other popular option, Visible, has similar functionality except that it is based on the visual aspect of the menu item You may have also noticed that certain menu items in applications appear and disappear depending
on the part of the program you're working in This is the Visibility option at work
Trang 314 Now build the menu structure for the application using the information and properties from Table A-2 Keep the following tips in mind when
creating the menus:
§ To define shortcut keys, simply place an ampersand (&)
in front of the letter in the menu name that will work in conjunction with the alternate (Alt) key For example,
&File would appear as File and would open the File menu with the Alt + F keystroke combination This functionality is triggered simply by the ampersand being in the caption field; you do not need to also define it in the shortcut property
§ To define menu options as options under, or belonging
to, a top-level menu item (for example, File → Exit), simply use the space underneath the menu option to enter the value Also, for lateral menu growth (for example, View → Toolbars → Standard), use the entry point on the right side of an existing menu item
§ To define a menu separator bar, simply enter a single dash character in the caption field and name the item accordingly
Note Some items in Table A-2 are not yet enabled, and enabling them
doesn't make sense until the correct situation arises The programming code that follows in this example will address such a situation
Table A-2: Menu information and properties table
Trang 32Your completed menu structure should look like the one shown in Figure A-4
Figure A-4: The completed menu structure in MDI Form
Next you will write the programming code to make these menus active
Activating the menu controls
The programming editor will look a little different to the one in previous versions of Visual Basic on at least two fronts First, you will notice that a lot more code is visible to you, which is generated by NET Because this product strives to get closer to true object orientation, it defines more class style code and makes that code visible to the
developer This enables you to see what NET has already generated for you in case you want your code to interact with the generated code Also, and this is a personal opinion only, the visible code is there to impress you with the technical skills of Microsoft's NET development team
The other thing that you may notice as different from previous versions of Visual Basic is that the editor has a cleaner look and feel with some visual improvements Take a look at the code editor as it is accessed within the menu editor To do this, simply double-click a menu item Figure A-5 shows the code editor for the File → Exit menu option
Figure A-5: The code editor for the File → Exit option
Trang 33Enter the following code after the Sub statement and before the End Sub statement The cursor should be at the correct location, but be sure it is in the right place just the same
Me.Close()
This simple command tells the menu to close its owner, which happens to be the main application window This is not a command instructing the menu to be unloaded as is often confused, rather it is telling the application to close Keep in mind that the form owns any controls, including menus that are placed on it After you have entered that code, you can close the code editor whenever you're ready Again, this would be a good chance to look around this particular editor to get a feel for how it works and operates Enter the following code for the other listed menu items Notice that NET already has some functionality built in for these more common menu options, like the window tiling actions Simply implement the commands in the right place
Tip If you are already in the code editor and want to edit code for
multiple events (like the code listings that follow), then simply choose the control entity from the drop-down control on the top left
of the code editor Next, select the event of the entity from the down list on the top right of the code editor The surrounding code will be generated for you, if it has not already been done, and you can continue with writing code
drop-Insert the following code to activate the Edit → Copy command:
Dim tempChild As Form = Me.ActiveMdiChild
If TypeOf tempChild.ActiveControl Is TextBox Then
Clipboard.SetDataObject(CType(tempChild.ActiveControl, TextBox).SelectedText) End If
Insert the following code to activate the Edit → Cut command:
Dim tempChild As Form = Me.ActiveMdiChild
If TypeOf tempChild.ActiveControl Is TextBox Then
Clipboard.SetDataObject(CType(tempChild.ActiveControl, TextBox).SelectedText) ' Clear the contents of the control since it is a cut process
CType(tempChild.ActiveControl, TextBox).SelectedText = ""
End If
Insert the following code to activate the Edit → Paste command:
'Declare an IDataObject named iData to hold the data retrieved from the clipboard Dim iData As IDataObject = Clipboard.GetDataObject()
Dim tempChild As Form = Me.ActiveMdiChild
'Determine whether the data is in a format you can use
If iData.GetDataPresent(DataFormats.Text) Then
'Yes it can be used, so paste it to the active control
tempChild.ActiveControl.Text = CType(iData.GetData(DataFormats.Text), String) Else
'No it is not
MsgBox("Data on clipboard is not retrievable.", MsgBoxStyle.Exclamation,
"Clipboard error")
End If
Trang 34Insert the following code to activate the Window → Tile Horizontal command:
Insert the following code to activate the Help → About command:
Dim frmAbout As New frmAbout()
'Set the Parent Form of the Child window
frmAbout.MdiParent = Me
frmAbout.Show()
Insert the following code to activate the File → Open Address command:
Dim frmAdd1 As New frmAddress()
' enable menu items now that a window will be opened
IsMDIContainer property to True (if not already done), so that the MDI functionality will also be set on the parent window of the application
Tip While you are looking at the properties of the main form window,
you may also want to set the WindowState property to Maximized,
so that when you test-run your application, it will take up your whole computer screen, for a better presentation of what you are
designing
Designing the Help About window
Next, design the Help About window that is referenced in the code, naming the form frmAbout when you are finished To create a new form, complete the following steps:
1 Right -click the application name in the Solution Explorer and choose Add → Add Windows Form
2 Select the default Windows Form template and enter the form Name in the bottom half of the form definition window, as shown in Figure A-6