Sonntag, 30. Dezember 2007

Visual Studio Basic 2008 Express Edition

The first start

As in the past the first Visual Studio start requires some post setup configurations. I do not know why they are writing 'This might take a few minutes', but okay they will know what they do. In fact it need only a few seconds on my computer.

image 

After configuration you will see the VS window with opened start page.

The evolution of GUI

image

On the first look you will see a few UI changes. The rounds are edges. While the old start page was filled with rounder edged - the new more modern looking design is more edged. The main areas in the start page are as before: MSDN feed, recent projects, getting started and Visual Basic Express Headlines.

The Menu has also redesigned.  The selected item is not only shown in a different background colour it gets a 3D Effekt

image

The active Tab gets a other back colour so if you have many files opened you can easier see where you are.

image

Creating a new Project

The formally included project types : Windows Forms Application, Class Libary and Console Application are extended by WPF Application and WPF Browser Application to meet the new features of .NET 3.5. The express Editions are limited to the .net Framework 3.5 - the can not build projects for .NET 2 and 3.0 like the full Visual Studio 2008 Versions. But such a feature is more interesting for professional Developers than hobbyists which the express editions are made for. If you want to develop .net 2 with Express Editions you have to Download the 2005 ones. But in most cases the .net 3.5 framework will be a good choose since it implement as far as I know all features of .net 2. 

image

Windows Forms Application

Business as usual  - everything is there where I was before. No real amazing changes except the 'Element Host'  for Windows Presentation Foundation Controls. This feature will as far as I think become important, since WPF will get more and more important. So a Control for hosting WPF inside a Windows Form is a good invention.

WPF Application / WPF Browser Application

Here you can not really speak of changes since the project type is completely new. Okay there was an Plug-in for allowing VS 2005 building WPF Projects but I was not as good as the real support in VS 2008.

image

The designer layout reminds me to the Visual Studio layout for web projects. You can view the visual designer in the top of the window and at the button the XAML Code. Other possibilities are: Only view XAML or only view the visual Designer. The right part with solution explorer and properties are like in other projects. But in XAML windows and for XAML Controls you are able so search after special properties.

image 

Application Properties

Here are many changes compared to the past. This changes are not related to special project types.

image

Tab Application
"View UAC Setting"

Since Windows Vista introduced the User Account Protection (UAC) applications must be programmed in mind of this feature. So now you are able to configure easily the UAC requirements for your applications.

image 
Tab Resources

Access Modifier for Resources. Now it is possible to select the public or friend modifier for all your Resources

image

Tab Settings

Access Modifier for Settings. Now it is possible to select the public or friend modifier for all your Settings.

Tab My Extensions

With this complete new feature you can easily add, view, or remove extensions to the My namespace.

image

Will be continued soon.

Visual Studio Express Editions Installation

Getting the express edition

Fist go to http://www.microsoft.com/express/ and then navigate to Download.

download page in Souvergin

(the download page in Souvergin which is a browser I am working on) 

From this point on you have as in the past two way of installing VS Express Editions.

Web Install

In this case you select the version and language of your choose (At that time only English and Japanese are available). Then  your Browser will download a small installer file - e. g. 2,5 MB for Visual Basic Express Edition. The real Application Download is made during setup. You can use this way if you have a fast Internet Connection or do not need to install Visual Studio a few times - otherwise I would prefer the Offline installation.

Offline Install

By using the offline install you only select the language and then your Browser will download DVD Image with about 900 MB. This Version includes the  whole Express Suite. Containing all Express Editions. After Download you will have to burn the iso image on a DVD or mount it into a virtual DVD drive.

Installation

As in past the Visual Studio installer needs a long time for installing everything. Using WebInstall makes the setup even slower, because all files are downloaded while Setup. The setup time depend also on what you have already installed on your Computer. So if .NET FX 3.5, the SDK's MSDN and SQL Server is installed - maybe because if you have already another Express Edition on your PC, the Setup runs much faster.

image 

Looking into the image: They set the wrong image transparent colour for the globe. Even really big companies make such little nasty mistakes - that happens not only to me -)

 

So next post of our VS 2008 Express Edition series is about Visual Basic 2008 Express Edition.

Visual Studio 2008 - Express Editions

In following entries we will focus on the new VS Express Editions and compare them to the predecessors. First we will start with Visual Basic's 2008 Edition followed by C#'s and C++'s and maybe we will also include Visual Web Developer (do not take that for granted, because this blog focuses on Windows Client Developement). Since J# is not a member of the Visual Studio 2008 family anymore we can not review anything about J# and VS 2008.

Sonntag, 16. Dezember 2007

Sending e-mails with System.Net

Many programs call the default e-mail program for sending Mails. But what is if a user use an Webmail Account instead of Outlook, Live Mail, Windows Mail, Thunderbird or all the other mail clients? In many cases e-mail provider support sending e-mails via SMTP (the protocol also mostly is used by real mail clients). 

Know I will show you how to send a mail via .Net and how to attach files to your mails.

Every sting in the code samples could be replaced by a parameter added to the Send method.

Imports System.Net.Mail
Public Class SendMail
Public Shared Sub Send()
Dim message As New MailMessage("sender@servername", "from@servername", "Subject", "MessageText")

Dim emailClient As New SmtpClient("servername")
emailClient.Send(message)
End Sub
End
Class


That's the easiest way. This way does NOT work in most cases. As you see: No account name and account password was used. Which e-mail allows this? No serious one ;) 



In many cases it is useful to define a port which your SmtpClient uses. Normally it is Port 25.



So we are going to extend the code.



Imports System.Net.Mail
Public Class SendMail
Public Shared Sub Send()
Dim message As New MailMessage("sender@servername", "from@servername", "Subject", "MessageText")
message.IsBodyHtml = False

Dim emailClient As New SmtpClient("servername", 25)

emailClient.DeliveryMethod = SmtpDeliveryMethod.Network
emailClient.Credentials = New System.Net.NetworkCredential("username", "password")
emailClient.Send(message)
End Sub
End
Class



Additionally I set message.IsBodyHtml to false, because we are sending just plain text and emailClient's DelivertyMethod was set to Network.



If you replaced every sting in the sample with real data by doing this in the method itself or by using method parameter, now the code should work and you are able to send a mail.



Next step: attachments.



Imports System.Net.Mail
Public Class SendMail
Public Shared Sub Send()
Dim message As New MailMessage("sender@servername", "from@servername", "Subject", "MessageText")
message.IsBodyHtml = False

Dim emailClient As New SmtpClient("servername", 25)

emailClient.DeliveryMethod = SmtpDeliveryMethod.Network
emailClient.Credentials = New System.Net.NetworkCredential("username", "password")

Dim Attachment As New Attachment("Path to your file")
message.Attachments.Add(Attachment)

emailClient.Send(message)

End Sub
End
Class


As you see I just created an System.Net.Mail.Attachment object which constructor uses a parameter for the file I want to send. Then the Attachment is Added to the attachment Collection of our message.

Freitag, 14. Dezember 2007

Clever Controls - Loading URLs instead of local paths

Did you know, that many .net Controls are able to open files over the internet? The word does not end in your local file systen and even not in your LAN - no you can open files over the Internet. Oney way to do this is surely to use System.Net.WebClient and save the Image local another way is just to give the control the URL.

For an example we are using the picturebox and let it display google germany's Logo.

So we create a Windows Forms project, a Form and put there a picture box in.

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CleverPictureBox
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
this.pictureBox1.ImageLocation = "http://www.google.de/intl/de_de/images/logo.gif"; //Points to google germany's logo
}
}
}


All I did was putting the URL into the Images Location. The Control did the rest for me. 



GoogleII



You can do this with many other Controls but there is still one problem remaining, the Control can not use the file as fast as it would be local stored. So it takes longer to download it temporary and opens it as open a file on your drive, which is not any surprise. So in most cases the System.Net.WebClient should be used to download the files bevore the shuld be displayed - but in some cases this open an URL instead a local path metod could be useful.

How to make Screenshots using System.Drawing

The normal unmanaged GDI has a "PrintWindow" Method, where a Screenshot can be taken from a Window Handle. With .net you can also make Screenshots. The folowing example shows how to make a screenshot from a specialised area of the screen.

Imports System
Imports System.Drawing
Public Class Screenshot'Version 1.1.2 for Souvergin Sequaia (NX Codebase)
Public Shared Function Shot(ByVal rectangle As System.Drawing.Rectangle, ByVal Location As Point) As System.Drawing.Bitmap
Dim memoryImage As System.Drawing.Bitmap = New System.Drawing.Bitmap(rectangle.Width, rectangle.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)

Dim memoryGraphics As Graphics = Graphics.FromImage(memoryImage)
myGraphics.CopyFromScreen(Location.X, Location.Y, 0, 0, rectangle.Size, System.Drawing.CopyPixelOperation.SourceCopy)

If Not memoryGraphics Is Nothing Then
memoryGraphics.Dispose()
End If


        Return memoryImage 

End Function
End
Class



The function needs two parameters for taking the screenshot. One the one hand a rectangle which is the size of the screenshot and a Point  - location - which indicates where the staring point is for taking the screenshot.



In the function a Bitmap is first a bitmap with the by the parameter rectangle specified size is created. After that the Graphics memoryGraphics is declared and the screnshot is taken by the CopyFromScreen Method. Location.X and Location.Y are the starting coordinates for the screenshot, the following two zeros are made that 0,0 is the start point in the destination rectangle on the bitmap. Rectangle.Size specifies the size of the screenshot.



At the end the Screenshot is returned.

How to read data with System.Xml.XmlTextReader

theSince in the last post an example of System.Xml.XmlTextWriter was used to write an XML-document this article will show how to read an XML-document.

Private Shared Sub LoadXMLfile(ByVal Path string)
If My.Computer.FileSystem.FileExists(Path) Then
Dim rdrXML As New System.Xml.XmlTextReader(Path) //Consturctor with XML file's path
Try
rdrXML.MoveToContent()

Dim ElementName As String = ""
Dim NextItem As Boolean = True
Dim objListViewItem As Favoritenobjekt = Nothing
Do While rdrXML.Read
If NextItem Then
objListViewItem = New Favoritenobjekt
NextItem = False
End If
Select Case rdrXML.NodeType
Case System.Xml.XmlNodeType.Element
ElementName = rdrXML.Name
Case System.Xml.XmlNodeType.Text
If ElementName = "Name" Then
objListViewItem.Text = rdrXML.Value
End If
If ElementName = "Kategorie" Then
objListViewItem.Kategorie = rdrXML.Value
End If
If ElementName = "URL" Then
objListViewItem.URL = rdrXML.Value
Favoriten_Liste.Add(objListViewItem)
NextItem = True
End If
End Select
Loop
rdrXML.Close()
rdrXML = Nothing
Catch ex As System.Exception
MsgBox("Error. Xmlfile could not be loaded", MsgBoxStyle.Critical)
End Try
End If
End Sub




What is important in the code?



The XMLTextReader is constucted with the path to the file which you are trying to read. rsrXMl.MoveToContent Points the Reader to the Data.  Since you do not know how much information is in the file you have to use a while loop. Important in this way of reading the data is that we are first reading an node then, we check if it is an Element or Text. The Elementname is stored in 'Elementname' which is needed to identify the element the Text (case 2) belongs to. 

System.XML how to store data the easy way

Nearly every application has to story data. Some bigger apps use powerful databases other use text files. One newer thing is storing your Data in XML files, which is a pretty good thing for middle size data.

Okay sure you could realize application storage by the use of .net's application Settings but, then the data is lost if your changing the application version. You are also not able the export date out of the settings.

System.Xml provides very good Classes for developing an XML Storage System.

First we are looking on the System.Xml.XmlTextWriter Class. In the showed scenario we are writing data from a ListView - to be more precisely it is a favourites list for websites

Imports System.Windows.Forms
Imports System.Xml

Public Class FavoritenExport
Private Sub Save()
Dim wtrXML As New XmlTextWriter(Path, System.Text.Encoding.UTF8)
With wtrXML
.Formatting = Formatting.Indented
.WriteStartDocument()
.WriteComment("Favourite file")
.WriteStartElement("WebSites")
Dim objListViewItem As New ListViewItem
For Each objListViewItem In lvwWebSites.Items
.WriteStartElement("WebSite")
.WriteElementString("Name", objListViewItem.Text)
.WriteElementString("Category", objListViewItem.SubItems(1).Text)
.WriteElementString("URL", objListViewItem.SubItems(2).Text)
.WriteEndElement()
Next
.WriteEndElement()
.WriteEndDocument()
.Flush()
.Close()
End With
Me.Close() 'Closes the form
End Sub
End
Class











What does Save Exactly? First we are creating our XmlTextWriter here named "wtrXML". The Variable Path just represents the path where the file should be written into e. g. C:\test.xml or C:\test.fda (the extension don't have to be xml). System.Text.Encoding.UTF8 is the charset, it is possible to use other ones, but UTF8 is the most used for XML files.









The result of such an XML File could be like this one:






<?xml version="1.0" encoding="utf-8"?>

<!--Favourite file-->


<WebSites>


<WebSite>


<Name>A Guided Tour of Windows Presentation Foundation</Name>


<Kategorie>Developement</Kategorie>


<URL>
http://msdn2.microsoft.com/en-us/library/aa480221.aspx</URL>

</WebSite>


<WebSite>


<Name>C# Home : News</Name>


<Kategorie>Developement</Kategorie>


<URL>
http://www.csharp-home.com</URL>

</WebSite>


<WebSite>


<Name>Channel 10</Name>


<Kategorie>Developement</Kategorie>


<URL>
http://on10.net/</URL>

</WebSite>


<WebSite>


<Name>CodePlex</Name>


<Kategorie>Developement</Kategorie>


<URL>
http://www.codeplex.com/Default.aspx</URL>

</WebSite>


</WebSites>



So what is important?



First you have do declare your XmlTextwriter, then you let it write down the start document stuff. After that your storage logic have to be implemented. In this case we use a root element named 'WebSites' this is not needed, but if you store more things which are not as closely related as it is in this case this could be an usefull technique to separate the different parts.



In this root Element we put the real data - our websites.  Because we do not only have on website, or do not know how much websites are in the listview by coding the writer, we use for each. This loop starts with a new StartElement, this time we call it for 'Website', after that the real data is stored by calling WriteElement string. The first parameter indicates the name the second the value of the element. Then WriteEndElement is called for Closing the WebseiteElement. After the for each loop WriteEndElement closes the 'WebSites' Element,  WriteEndDocument closes the docuement.


Next post will show how to read Xml files using System.Xml.XmlTextReader.

Windows Forms Controls "Tag" is your best friend

Nearly every Default Windows Forms Control and everything derived from UserControl has it: a property namend Tag.

What is Tag?

The answer is nothing more than just a simple - normally empty - object. Because of it emptiness you could easiely use it for storing data in it. A simple way is just putting easy things in it like numbers or strings. But since Tag is an object you can put any kind of type in it. This could you use in a very helpful way.

Taking an example: If you are programming a Browser. What is with Favourites? Every Browser has such  functionalities. First you have to read your storage's Favourites, after that you maybe want to show the Favourites in a System.Windows.Forms.ToolstripMenu  or even in an ListView or tree view. But how to cope with the clicks? You do not want to display the URL in de menu, you just put it in the Tag. Every added MenuItem (, ListviewItem or TreeViewNode) uses the same method as every other added ManuItem (, ListviewItem or TreeViewNode). You define a string variable named 'URL' and this Variable gets the value from your sender (Before doing this you have to convert sender from an object to your Controltype). After that you just make "URL = Control.Tag" and then you can navigate to this URL. Sure this is just an easy example of using the Tag property but  your clearly see, that Tag is a very useful property for dynamic GUIs.

With this great property you have the abilities to store nearly everything you need for working with the control in the control it self. So you are able to reduce Code for dozens of Click-methods.

The .net way of life? What we are intending with this blog

The .net Technology in all it's evolution steps gets more and more important on the Windows Platform - even on other platforms people are working on similar implementations - most famous example: the MONO project.

We are focusing on the questions: 'Why .net?' and 'How do we do THIS in .net?'

In most cases we are focusing on Microsoft's .net - often in the 2.0 Version since it might be the most used one. Another fact is that .net 3 and 3.5 is build around the 2.0 Core. So if you use this versions it could be said that you are using in some ways a strongly extended .net 2.

The focus will also be on real Windows applications, not on ASP.net - also a big topic will be: "How to to this API stuff in managed code?"

Since we are trying to provide help and ideas to as many people as possible we will use as manny .net languages as possible.