|
VB .NET Helper Newsletter
|
Rod Stephens
|
Jun 27, 2009 08:29 PDT
|
Microsoft is pushing Windows 7 early. For a limited time only (until
July 11 "or while supplies last" (How can they run out? Did Microsoft
only give them permission to sell a certain number of copies? Seems hard
to believe that Microsoft would want to restrict the number of people
signing on early.) you can pre-order Windows 7 for at least 50% off at
Amazon. Here are some links:
Windows 7 Resource Center
http://www.amazon.com/b/?node=1286119011&tag=vbhelper
Windows7 Home Premium Upgrade
http://www.amazon.com/Microsoft-GFC-00020-W7HPU/dp/B002DHLUWK?tag=vbhelper
Windows7 Professional Upgrade
http://www.amazon.com/Microsoft-GFC-00020-W7HPU/dp/B002DHGM50?tag=vbhelper
In fact, I'll be without my computer for a few days because I'm having
the hard disk enlarged to make room for a Windows 7 partition in
addition to my existing XP and Vista partitions. (They must have some
sort of stretching machine to enlarge the disk.)
----------
People posted two new reviews of my book "Visual Basic 2008 Programmer's
Reference"
(http://www.amazon.com/exec/obidos/ASIN/0470182628/vbhelper/). Thanks!
Also if you have a copy of my book "Beginning Database Design Solutions"
(http://www.amazon.com/exec/obidos/ASIN/0470385499/vbhelper/), please
post a review when you have a few moments.
----------
Have a great week and thanks for subscribing!
Rod
RodSte-@vb-helper.com
Books To Keep: http://www.BooksToKeep.com
----------
*** Now Available ***
Beginning Database Design Solutions
http://www.amazon.com/exec/obidos/ASIN/0470385499/vbhelper/
Visual Basic 2008 Programmer's Reference
http://www.amazon.com/exec/obidos/ASIN/0470182628/vbhelper/
==========
VB.NET Contents:
1. New HowTo: Build a form from scratch and handle events raised by its
controls in Visual Basic .NET
2. New HowTo: Let the user drag files into a list and then upload them
all to a Web site in Visual Basic .NET
Both Contents:
3. New Tip: Use proper data delimiters for different databases
==========
++++++++++
<VB.NET>
++++++++++
==========
1. New HowTo: Build a form from scratch and handle events raised by its
controls in Visual Basic .NET
http://www.vb-helper.com/howto_net_build_form_from_scratch.html
http://www.vb-helper.com/HowTo/howto_net_build_form_from_scratch.zip
When you click the Make Form button, the program creates a new form and
sets its size and caption.
The program then creates a button and sdets its properties. It uses
AddHandler to make the main form's btn_Click event handler catch the
button's Click event. Finally it adds the button to the form's Controls
collection to add the button to the form.
' Make the new form.
Private Sub btnMakeForm_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnMakeForm.Click
Dim frm As New Form
frm.Size = New Size(300, 200)
frm.Text = "New Form"
Dim btn As New Button
btn.Text = "Click Me"
btn.Size = New Size(100, 30)
btn.Location = New Point( _
(frm.ClientSize.Width - btn.Width) / 2, _
(frm.ClientSize.Height - btn.Height) / 2)
AddHandler btn.Click, AddressOf btn_Click
frm.Controls.Add(btn)
frm.Show()
End Sub
The btn_Click event handler converts the sender parameter into the
button that raised the Click event. It uses that button's FindForm
method to get the button's form. It then closes that form.
' Respond to the new form's button click.
Private Sub btn_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)
Dim btn As Button = DirectCast(sender, Button)
Dim frm As Form = btn.FindForm
frm.Close()
End Sub
Note that building a form from scratch probably won't save the
application much memory or anything. It's usually better to simply build
the form at design time and create an instance of it at run time.
However, if you really need to, you can use these techniques to build a
form that might have different controls depending on the situation.
Also note that you can read the code that Visual Basic writes to build a
form and use some or all of that code to do the same yourself.
==========
2. New HowTo: Let the user drag files into a list and then upload them
all to a Web site in Visual Basic .NET
http://www.vb-helper.com/howto_net_upload_many_files.html
http://www.vb-helper.com/HowTo/howto_net_upload_many_files.zip
The program lets the user drag files into its ListBox. To allow that,
the program must set the ListBox's AllowDrop property to True.
When the user drags something over the ListBox, its DragEnter event
handler executes. It sets the e.Effect parameter to Copy if the user is
dragging files.
Private Sub lstFiles_DragEnter(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles lstFiles.DragEnter
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End Sub
When the user drops the files, the program loops through the file names
and adds them to the list.
Private Sub lstFiles_DragDrop(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles lstFiles.DragDrop
For Each filename As String In e.Data.GetData(DataFormats.FileDrop,
True)
If Not lstFiles.Items.Contains(filename) Then
lstFiles.Items.Add(filename)
End If
Next filename
End Sub
When the user clicks the Go button, the following code executes. It uses
the user-entered host name and remote directory to build the remote
host's name in the form:
ftp://www.myhost.com/remote_directory/
The code saves the entered user name and password, and then loops
through the files in the list. For each file, it calls subroutine
UploadFile and removes the file from the list.
Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnGo.Click
Static running As Boolean = False
running = Not running
If running Then
Me.Cursor = Cursors.WaitCursor
' Build the remote file path as in:
' ftp://www.myhost.com/public_html/
Dim remote_path As String = txtHost.Text
If Not remote_path.ToLower().StartsWith("ftp://") Then
remote_path = "ftp://" & remote_path
End If
If Not remote_path.EndsWith("/") Then remote_path &= "/"
remote_path &= txtDirectory.Text
If Not remote_path.EndsWith("/") Then remote_path &= "/"
Dim user_name As String = txtUserName.Text
Dim password As String = txtPassword.Text
Dim num_copied As Integer = 0
Do While lstFiles.Items.Count > 0
Try
UploadFile(lstFiles.Items(0), remote_path, user_name,
password)
lstFiles.Items.RemoveAt(0)
num_copied += 1
Catch ex As Exception
MessageBox.Show(ex.Message, "Upload Error",
MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Do
End Try
Application.DoEvents()
If Not running Then Exit Do
Loop
running = False
Me.Cursor = Cursors.Default
MessageBox.Show("Copied " & num_copied & " files", _
"Done", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
Subroutine UploadFile composes the remote file's name and makes a
FtpWebRequest object to represent uploading to that file. The code makes
credentials for the entered user name and password.
Next the code copies the input file into a byte array. It opens the
request stream for the upload and writes the bytes into it. Finally the
code closes the stream and checks the result.
' Upload the file.
Private Sub UploadFile(ByVal local_file As String, ByVal remote_path As
String, ByVal user_name As String, ByVal password As String)
' Get the object used to communicate with the server.
Dim fi As New FileInfo(local_file)
Dim remote_file As String = remote_path & fi.Name
' Make the WebRequest.
Dim request As FtpWebRequest = WebRequest.Create(remote_file)
request.Method = WebRequestMethods.Ftp.UploadFile
' Get credentials.
request.Credentials = New NetworkCredential(user_name, password)
' Copy the file into a Byte array.
Dim source_stream As New StreamReader(local_file)
Dim file_bytes As Byte() =
Encoding.UTF8.GetBytes(source_stream.ReadToEnd())
source_stream.Close()
request.ContentLength = file_bytes.Length
' Open the request stream and write the bytes into it.
Dim requestStream As Stream = request.GetRequestStream()
requestStream.Write(file_bytes, 0, file_bytes.Length)
requestStream.Close()
' Check the response.
Dim response As FtpWebResponse = request.GetResponse()
If Not response.StatusDescription.Contains("File successfully
transferred") Then
Dim msg As String = "Error uploading file " & local_file &
vbCrLf & response.StatusDescription
response.Close()
Throw New Exception(msg)
End If
response.Close()
End Sub
==========
++++++++++
<Both>
++++++++++
==========
3. New Tip: Use proper data delimiters for different databases
http://www.vb-helper.com/tip_database_date_delimiters.html
In SQL statements, all databases use quotes to delimit strings but
different databases use different delimiters for dates. This is one of
the most common problems when trying to port database code from Access
to SQL Server or vice versa but at least it's easy to fix once you
understand what the problem is.
* SQL Server uses quotes (') as in '4/1/2010'
* Access (Jet) uses number signs (#) as in #4/1/2010#
==========
Archives:
http://www.topica.com/lists/VBHelper
http://www.topica.com/lists/VB6Helper
http://www.topica.com/lists/VBNetHelper
Post questions at:
http://www.topica.com/lists/VBHelperQA
|
|
 |
|