Saturday, June 30, 2007

Triple/dual booting with Vista and Linux

I just got a 160GB SATA HDD for my Compaq nc8430 and of course I want to run all three OS's
Windows XP to get work done,
Vista to mess about and show off for now,
Linux to just play around.

Now some distros of Linux allow you to install the GRUB bootloader on the actual partition of the installation. This is great because:
1. Vista has already taken over the MBR and thus installin this after linux will render linux unbootable.
2. I know how to BCEdit better than I do GRUB :)

Luckily I found these guides which are idiot proof and so well documented.
The definitive dual-booting guide: Linux, Vista and XP step-by-step
But most especially this one:
How to dual-boot Vista with Linux (Linux is already installed)
Now although I put XP on first then Vista and then Fedora 7 I did put the GRUB on the Linux partition and so this is my set up :)

Tuesday, June 26, 2007

Inserting text into a Word Document from VB.NET

Another post to the 'Office Series', I used to do this everyday in a previous job!! Al we did was automate word, but using templates, which was much cooler. For now this just adds text into a word document.

Private WordApp As New Word.ApplicationClass()

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click

' Choose a word document
If Me.OpenFileDialog2.ShowDialog() = DialogResult.OK Then
' Get the file name from the open file dialog
Dim fileName As Object = OpenFileDialog2.FileName
' Make word visible, so you can see what's happening
WordApp.Visible = True
' Open the document
Dim aDoc As Word.Document = WordApp.Documents.Open(fileName)
' Add the text and a line break
WordApp.Selection.TypeText("VB.NET Rocks")
End If
End Sub

Friday, June 22, 2007

Rotating and moving Graphics/images in VB.NET with Style

I saw a post recently that asked if you could rotate graphics in VB.NET. Now rotating a shape is one thing but an image provided more of a challenge. I knew that matrices are the way to go and not the mathematical calculations using pi, and cos and sin etc. But I also knew that matices cannot be applied directly to the image object. So I had to get my pic into a graphicspath. Well it appears that this is not too bad either. But as soon as I put all four points and decided to rotate, RotateAt gave me a not implemented error??!! So I put only three points and it worked!!! Well now my images is rotating, but flickering like mad. So I looked into double buffering, and finally I have a rotating/sliding image:

Imports System.Drawing.Drawing2D

Public Class Form1

Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork

Dim RotateAngle = 1
Dim offset As Integer = -10
Dim tickcnt As Long = Now.Ticks

Dim BackBuffer As New Bitmap(Me.ClientSize.Width, Me.ClientSize.Height)
Dim DrawingArea As Graphics = Graphics.FromImage(BackBuffer)
Dim Viewable As Graphics = Me.CreateGraphics()
Dim TopRight As New PointF(800, 50)
Dim i As System.Drawing.Image = My.Resources.MyPic
Dim myPath As New GraphicsPath
Dim OrigPoints() As PointF = {TopRight, New Point(TopRight.X - i.Width, TopRight.Y), New Point(TopRight.X, TopRight.Y + i.Height)}
Dim translateMatrix As New Matrix()

Do While Not Me.BackgroundWorker1.CancellationPending

If Now.Ticks - tickcnt > 500000 Then
RotateAngle += 10
offset -= 5

' Do the transforamtion on the Image
translateMatrix.RotateAt(RotateAngle, New Point((TopRight.X - i.Width / 2) + offset, (TopRight.Y + i.Height / 2)))
translateMatrix.Translate(offset, 0)

'Apply it to the path
'DrawingArea.DrawString(myPath.PathPoints(0).X, New Font("Arial", 10, FontStyle.Regular), Brushes.Aqua, TopRight.X, TopRight.Y)

' Draw the image with the new points
DrawingArea.DrawImage(i, myPath.PathPoints)

Viewable.DrawImageUnscaled(BackBuffer, 0, 0)

tickcnt = Now.Ticks
End If

End Sub

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint

End Sub
End Class

Tuesday, June 19, 2007

Accessing Outlook Address Book from VB.NET

Had someone ask me this a while back and lost the link so I decided to blog it this time. As always dont' forget to add a COM reference to your project for the Microsoft.Outlook x.0 Object library in order to create the necessary objects.

Dim oOutlook As New Outlook.Application
Dim oNS As Outlook.NameSpace
Dim oContacts As Outlook.MAPIFolder
Dim oItems As Outlook.Items
oNS = oOutlook.GetNamespace("mapi")
oContacts = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts)
' set to the contact folder
oItems = oContacts.Items.Restrict("[MessageClass] = 'IPM.Contact'")
' filter to select only contact items
oItems.Sort("[EMail1Address]", False) ' sort by email address

For Each oct As Outlook.ContactItem In oItems
MessageBox.Show(oct.CompanyName & "-" & oct.BusinessTelephoneNumber)

Sunday, June 17, 2007

Loading Assemblies into an AppDomain

'Creating the AppDomain
Dim d as AppDomain = AppDomain.CreateDomain("NewDomain")

'Load the assembly using path

'Load the assembly from a referenced library

'Unload it

Reading/writing Excel files in VB.NET

Now there seem to be a few examples of how to do this in C# but few comprehensive ones in VB.NET. So here's how to let the user choose an excel file and then write to cell A1 and read from cell A2. Don't forget to add a reference to the Excel COM library.

Also add Imports Microsoft.Office.Interop to the first line of your class/form

Dim xcFileInfo As IO.FileInfo
Dim xcFileDialog As New OpenFileDialog()
xcFileDialog.Filter = "Excel Spreadsheet Files!*.xls"
xcFileDialog.Title = "Select estimate in excel spreadsheet file!"

If xcFileDialog.ShowDialog = DialogResult.OK Then
xcFileInfo = New IO.FileInfo(xcFileDialog.FileName)
End If

Dim myExcel As Excel.Application ' Interface to Excel
Dim myWorkBookCollection As Excel.Workbooks ' Workbook-collection (note the 's' at the end)
Dim myWorkBook As Excel.Workbook ' Single Workbook (spreadsheet-collection)
Dim myWorkSheet As Excel.Worksheet ' Single spreadsheet

' Initialize the interface to Excel.exe
myExcel = New Excel.Application

If myExcel Is Nothing Then
MessageBox.Show("Could not load Excel.exe")
Exit Sub
End If

' initialise access to Excel's workbook collection
myWorkBookCollection = myExcel.Workbooks

'open spreadsheet from disk
myWorkBook = myWorkBookCollection.Open(xcFileInfo.FullName, , False)

'get 1st sheet from workbook
myWorkSheet = myWorkBook.Sheets.Item(1)

'alter contents of 1st cell
Dim myCell As Object = myWorkSheet.Range("A1", _ System.Reflection.Missing.Value)
myCell.Value2 = "I did it again!!!"

'display the spreadsheet
'myExcel.Visible = True
'Read Cell A2

Dim myCell2 As Object = myWorkSheet.Range("A2", _ System.Reflection.Missing.Value)

Me.Text = myCell.Value2

'save and get out

Thursday, June 14, 2007

Can't upgrade/install SQL 2005 Express Workstation tools

If you are like me, you installed VS2005 and forgot to tell it not to install SQL Express, because you just knew that later you were going to install SQL Server 2005 Developer Edition. Now I managed to install SP2 for Express and all was OK until the day I decided to install SQL 2005 Dev Edition. I get a little error saying version warning but ignored it, only to find out that it will install all the goddies except the most important one, SQL Management Studio. Ahhhhh, so the whole install goes through and I now have tow SQL Servers but no management tools!!! Now I know you are saying, ' why don't you use the osql?' Yeah it's great, but somethings are just easier with the GUI, I want THAT GUI!!! So I read the warning message again, and googled to find Upgrade a SQL Server Express Report Server to Other SQL Server Editions Well now it's easy. I didn't want to upgrade the whole of SQL Express, just the workstation components. So I launched the setup of the just the tools, not the server with the switch mentioned.


Now I can tick on the workstation components .... and all's well :)

Wednesday, June 13, 2007

MSDN Forums (Part II)

Well something just worth shouting about :) two weeks in and 36 answers !! Now I'm in the top 100.
9th in VB General
8th in VB IDE
10th in VB Express

Wednesday, June 6, 2007

MSDN Forums (Part I)

OK time for a little pat on my own back. I started looking in on the MSDN Forums, trying to lend a hand to those stuck on anything VB.NET related. I'm mainly looking at VB.NET Express and the 3 VB.NET ones:IDE, Language and General. Well after a week of posting replies. I've got in to the top ten of the VB.NET:IDE, yeah. So 37 posts in a week is not too bad. I think the forum is a great place to learn and get help, so many great people pumping ideas in so please take a look.

Saturday, June 2, 2007

Setting NTFS Directory/Drive permissions in VB.NET 2005

It seems setting File Permissions in 2005 is easy, but there didn't seem to be much regarding directory or drive ACL's. So a little digging and adapting goes a long way .....

Dim folder_info As New DirectoryInfo("C:\")
' Read the current ACL
Dim foldersecurity As DirectorySecurity = folder_info.GetAccessControl _

' Make up my new ACE
Dim MyRule As New System.Security.AccessControl.FileSystemAccessRule _
("DOMAIN\USER", FileSystemRights.FullControl, AccessControlType.Allow)

' Add it to the old ACL
' Apply it

Friday, June 1, 2007

Marking madatory textboxes/fields with style

Now everyone in VS2003/2005 who likes graphics has done some code in the onPaint event to draw nice little things, but have you ever tried drawing on a textbox, or any other control for that matter. Wemay also be tired of just filling madatory fields with a plain backcolor fill also. So what I set out to do was draw a nice little glyph in the corner to signify that a filed was mandatory

First off we must subclass the good old textbox, but hopefully you aer doing this already in your component lib. Next it's a matter of hooking up to the windows message and drawing accordingly.

Public Class Component1
Inherits TextBox
Private Const WM_PAINT As Integer = &HF

Private bMandatory

Public Property Mandatory() As Boolean
Return bMandatory
End Get
Set(ByVal value As Boolean)
bMandatory = value
End Set
End Property

Protected Overrides Sub WndProc(ByRef m As Message)
If m.Msg = WM_PAINT And bMandatory Then
Dim g As Graphics = Me.CreateGraphics
Dim pts As PointF() = {New PointF(0, 0), New PointF(5, 0), _
New PointF(0, 5)}
g.FillPolygon(Brushes.Red, pts)
End If
Catch ex As Exception
' Call error handler
End Try

End Sub

End Class
You should end up with this ...