Launching Excel from VB.NET to view a file (.xls, .csv, .etc) has a seemingly simple solution:
Process.Start("c:\temp\test.csv")
However, this simple solution has a problem. After Excel is running, it does not respond to this type of command anymore. Excel has to be closed (killed) before you can successfully call Start again with any file that is associated with Excel.
I see several different approaches to this problem:
http://vbnetsample.blogspot.com/2007/08/start-and-kill-process.html
http://www.devnewsgroups.net/group/microsoft.public.dotnet.framework.windowsforms/topic17364.aspx
http://www.devx.com/dotnet/Article/7914
There is also a totally different approach using Excel's automation to open the file:
http://www.webmaster-talk.com/net-programming-forum/85020-open-excel-file-in-vb-net.html
http://support.microsoft.com/default.aspx/kb/301982
http://support.microsoft.com/kb/302094
http://www.thescripts.com/forum/thread348936.html
This blog is a depository for things I have found regarding VB.NET and C#.NET development (and other things I may need to remember). This blog is primarily for my own reference, but if the information I found is useful to others, then that is great. If others want to contribute with comments or other information, then that is great too.
Sunday, March 23, 2008
Exporting ListView to CSV File in VB.NET
I have posted an article on copying a ListView to the Windows Clipboard, but I have also found it useful to export a ListView directly to a .CSV file (comma separated value file). The process to go from a ListView to the clipboard or to a CSV file is similar.
There are number of rules for creating a CSV file. Here is a good place to look if you want to see them all. I have simplified the process to a few rules that will work for most cases. I put quotes around all values and column names. Technically, this is not required, but by putting them around all cases eliminates the need to determine if you need them or not. I also replace any quote in the data with a double quote.
You may want to get the file name from the user and automatically open the file in Excel too. Here is an example of how to do that:
There are number of rules for creating a CSV file. Here is a good place to look if you want to see them all. I have simplified the process to a few rules that will work for most cases. I put quotes around all values and column names. Technically, this is not required, but by putting them around all cases eliminates the need to determine if you need them or not. I also replace any quote in the data with a double quote.
Public Function ExportListViewToCSV(ByVal filename As String, ByVal lv As ListView) As Boolean
Try
' Open output file
Dim os As New StreamWriter(filename)
' Write Headers
For i As Integer = 0 To lv.Columns.Count - 1
' replace quotes with double quotes if necessary
os.Write("""" & lv.Columns(i).Text.Replace("""", """""") & """,")
Next
os.WriteLine()
' Write records
For i As Integer = 0 To lv.Items.Count - 1
For j As Integer = 0 To lv.Columns.Count - 1
os.Write("""" & lv.Items(i).SubItems(j).Text.Replace("""", """""")+ """,")
Next
os.WriteLine()
Next
os.Close()
Catch ex As Exception
' catch any errors
Return False
End Try
Return True
End Function
You may want to get the file name from the user and automatically open the file in Excel too. Here is an example of how to do that:
Public Sub TestExportToCSV()
Dim dlg As New SaveFileDialog
dlg.Filter = "CSV files (*.CSV)|*.csv"
dlg.FilterIndex = 1
dlg.RestoreDirectory = True
If dlg.ShowDialog = Windows.Forms.DialogResult.OK Then
If ExportListViewToCSV(dlg.FileName, ListView1) Then
Process.Start(dlg.FileName)
End If
End If
End Sub
Saturday, March 15, 2008
Creating Events for a Class in VB.NET
I have found the ability to create and raise events in a class to be a very useful feature of .NET. It also turns out to be very easy to implement.
I could provide an example here, but I will instead point to a good example I have found:
http://www.devsource.com/c/a/Using-VS/Using-Class-Events-in-VBNet/
I could provide an example here, but I will instead point to a good example I have found:
http://www.devsource.com/c/a/Using-VS/Using-Class-Events-in-VBNet/
Comparing Objects (Pointer Equality) in VB.NET
It was not readily obvious to me how to compare references (pointers) to an object to see if both references are pointing to the same object in VB.NET.
The specific example that raised this question for me was the need to see if a reference pointing to a ToolStripMenuItem object was in a list of ToolStripMenuItem objects:
Private m_itemList As New Generic.List(Of ToolStripMenuItem)
.
.
.
Private Function GetItemPosition(ByVal item _
As ToolStripMenuItem) As Integer
For i As Integer = 0 To m_itemList.Count - 1
' If m_itemList(i)=item Then ' This doesn't work
If Object.ReferenceEquals(m_itemList(i), item) Then ' This does
Return i
End If
Next
Return -1
End Function
You can't just directly compare the references. You will get an error message that says: Operator '=' is not defined for types 'System.Windows.Forms.ToolStripMenuItem' and 'System.Windows.Forms.ToolStripMenuItem'. But there is a simple, if not readily obvious to me, solution. Just use the Object.ReferenceEqual method:
If Object.ReferenceEquals(item1, item2) Then
.
.
.
End If
The specific example that raised this question for me was the need to see if a reference pointing to a ToolStripMenuItem object was in a list of ToolStripMenuItem objects:
Private m_itemList As New Generic.List(Of ToolStripMenuItem)
.
.
.
Private Function GetItemPosition(ByVal item _
As ToolStripMenuItem) As Integer
For i As Integer = 0 To m_itemList.Count - 1
' If m_itemList(i)=item Then ' This doesn't work
If Object.ReferenceEquals(m_itemList(i), item) Then ' This does
Return i
End If
Next
Return -1
End Function
You can't just directly compare the references. You will get an error message that says: Operator '=' is not defined for types 'System.Windows.Forms.ToolStripMenuItem' and 'System.Windows.Forms.ToolStripMenuItem'. But there is a simple, if not readily obvious to me, solution. Just use the Object.ReferenceEqual method:
If Object.ReferenceEquals(item1, item2) Then
.
.
.
End If
Friday, March 14, 2008
Tooltips in VB.NET
One nice feature of VB.NET is the ease in which you can implement tooltips for any Control on the desktop (unfortunatley not for .NET CF, see here).
Define a global variable for the tooltip object:
Dim g_toolTip As ToolTip = Nothing
Initialize the tooltip object. There are several settings that can be initialized too:
g_toolTip = New ToolTip()
g_toolTip.AutoPopDelay = 5000
g_toolTip.InitialDelay = 1000
g_toolTip.ReshowDelay = 500
g_toolTip.ShowAlways = True
When you are ready to display a tooltip, you must specify the control that the tooltip is associated with (ctrl in my example). The control is any object derived from Control, so if you make your own .NET control (deriving it from the Control base class), it can be associated with the tooltip. You can also use existing controls like Button and PictureBox.
g_toolTip.SetToolTip(ctrl, "tooltip text")
g_toolTip.Active = True
This functionality is actually documented pretty well in the Microsoft documentation. There are also C# example and detailed descriptions of the options like AutoPopDelay.
Define a global variable for the tooltip object:
Dim g_toolTip As ToolTip = Nothing
Initialize the tooltip object. There are several settings that can be initialized too:
g_toolTip = New ToolTip()
g_toolTip.AutoPopDelay = 5000
g_toolTip.InitialDelay = 1000
g_toolTip.ReshowDelay = 500
g_toolTip.ShowAlways = True
When you are ready to display a tooltip, you must specify the control that the tooltip is associated with (ctrl in my example). The control is any object derived from Control, so if you make your own .NET control (deriving it from the Control base class), it can be associated with the tooltip. You can also use existing controls like Button and PictureBox.
g_toolTip.SetToolTip(ctrl, "tooltip text")
g_toolTip.Active = True
This functionality is actually documented pretty well in the Microsoft documentation. There are also C# example and detailed descriptions of the options like AutoPopDelay.
Sunday, March 9, 2008
Color Selection Dialog in VB.NET
The Color Selection Dialog for VB.NET (and C# would be similar) is fairly easy to do if you have and example:
VB.NET
Dim colorValue As Color = Color.Red
Dim dlg As New ColorDialog
dlg.Color = colorValue
If dlg.ShowDialog() = DialogResult.OK Then
colorValue = dlg.Color
End If
For VB6, here is one way to do it:
http://www.devx.com/vb2themax/Tip/19257
VB.NET
Dim colorValue As Color = Color.Red
Dim dlg As New ColorDialog
dlg.Color = colorValue
If dlg.ShowDialog() = DialogResult.OK Then
colorValue = dlg.Color
End If
For VB6, here is one way to do it:
http://www.devx.com/vb2themax/Tip/19257
Labels:
color dialog,
color picker,
color selection
Monday, March 3, 2008
Tooltips with the .NET Compact Framework
I have been looking for a way to support Tooltips in .NET CF like I do on the desktop with the Tooltip object. There must be something better out there that I just haven't come across. Please post a comment if you know of something.
This is the only thing I have found that is reasonably close to what I want, but it is still not what I really want (wich is the Tooltip object from the desktop):
Here is the full posting from the Windows Mobile Team Blog:
http://blogs.msdn.com/windowsmobile/archive/2005/02/15/373120.aspx
The source code for the control is:
http://www.marcusperryman.members.winisp.net/blogstuff/tooltipCF.zip
The example project that uses it is:
http://marcusperryman.members.winisp.net/BlogStuff/smartdeviceapplication3.zip
This is the only thing I have found that is reasonably close to what I want, but it is still not what I really want (wich is the Tooltip object from the desktop):
Here is the full posting from the Windows Mobile Team Blog:
http://blogs.msdn.com/windowsmobile/archive/2005/02/15/373120.aspx
The source code for the control is:
http://www.marcusperryman.members.winisp.net/blogstuff/tooltipCF.zip
The example project that uses it is:
http://marcusperryman.members.winisp.net/BlogStuff/smartdeviceapplication3.zip
Subscribe to:
Posts (Atom)