Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Developing Pocket PC Applications In Visual Studio.NET  : Page 3

Microsoft's Visual Studio .NET 2003 ships with a new project template called "Smart Device Application," which lets you target the .NET Compact Framework to build PocketPC applications in almost exactly the same way you build Windows Forms applications.


advertisement
Figure 8. Setting the form's MinimizeBox property to False causes the OK button to appear.
Setting Form Properties and Coding Event Handlers
Setting the MinimizeBox property of the Windows Form to False causes it to display the OK button ; otherwise, it displays an "X". Clicking on the OK button closes the application, whereas the clicking the "X" minimizes the application window. When you are debugging your application, clicking on the OK button terminates the debugging process in Visual Studio .NET; but if you click the "X" button, you have to terminate the debugging process in Visual Studio .NET manually.

To summarize, the OK button terminates the application, whereas the "X" button minimizes the application. Figure 8 shows how the setting affects the final form's title bar.

Activate the Menus
Now, you can activate the menus. But first, you must write the code for the File menu. The first menu item is the New item:

Private Sub mnuNew_Click(ByVal sender As _ System.Object, ByVal e As System.EventArgs) _ Handles mnuNew.Click Dim response As Microsoft.VisualBasic.MsgBoxResult response = MsgBox("Save existing file?", _ MsgBoxStyle.OKCancel, "Save") If response = MsgBoxResult.OK Then Save() Else Return End If TextBox1.Text = "" currentFilename = "" StatusBar1.Text = "New" End Sub

When users click on the New item, the application first displays a message box asking if they want to save the file. If so, the code calls the Save() method and resets the form; otherwise, it exits.

Next, the Open item prompts users to enter a filename using the OpenFileDialog class. It then opens the selected file using the StreamReader class and displays the text in the TextBox control:

Private Sub mnuOpen_Click(ByVal sender As _ System.Object, ByVal e As System.EventArgs) _ Handles mnuOpen.Click Dim openFileDialog1 As New OpenFileDialog Dim sr As StreamReader openFileDialog1.InitialDirectory = "c:\" openFileDialog1.Filter = "txt files (*.txt)|*.txt" If openFileDialog1.ShowDialog() = _ DialogResult.OK Then currentFilename = openFileDialog1.FileName StatusBar1.Text = currentFilename sr = New StreamReader( _ openFileDialog1.FileName, _ System.Text.Encoding.ASCII) TextBox1.Text = sr.ReadToEnd() sr.Close() End If End Sub

Similarly, the Save menu item calls the Save() method:



Private Sub mnuSave_Click(ByVal sender As _ System.Object, ByVal e As System.EventArgs) _ Handles mnuSave.Click Save() End Sub

And the Save As… menu item calls the SaveAs() method:

Private Sub mnuSaveAs_Click(ByVal sender As _ System.Object, ByVal e As System.EventArgs) _ Handles mnuSaveAs.Click SaveAs() End Sub

The SaveAs() method implementation uses the StreamWriter class to save the text in the TextBox control to a file. It prompts the user to enter a filename using the SaveFileDialog class:

Public Sub SaveAs() Dim saveFileDialog1 As New SaveFileDialog Dim sw As StreamWriter saveFileDialog1.InitialDirectory = "\" saveFileDialog1.Filter = "txt files (*.txt)|*.txt" If saveFileDialog1.ShowDialog() = _ DialogResult.OK Then currentFilename = saveFileDialog1.FileName StatusBar1.Text = currentFilename sw = New StreamWriter( _ saveFileDialog1.FileName, True, _ System.Text.Encoding.ASCII) sw.WriteLine(TextBox1.Text) sw.Close() End If End Sub

After saving a file, the application remembers the current filename using the global variable currentFilename.

The Save() method capitalizes on the saved file name by first checking to see if the currentFilename variable contains a filename. If not, the text has not been saved previously, and the code calls the SaveAs() method. If the currentFilename variable contains a filename but the file does not exist (perhaps the user has deleted it), the code also invokes the SaveAs() method. It then saves the file using the StreamWriter class:

Public Sub Save() '---if no filename, invoke SaveAs() If currentFilename = "" Then SaveAs() Return End If '---if have filename, but file may not be there... If (Not File.Exists(currentFilename)) Then SaveAs() Return End If Dim sw As StreamWriter sw = New StreamWriter(currentFilename, False, _ System.Text.Encoding.ASCII) sw.WriteLine(TextBox1.Text) sw.Close() End Sub



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap