Hi All,
Whenever we debug the code in Dot Net while using InterOP, it is very frequent to notice a proxy object rather than the actual InterOP object. When you try to get the properties of an InterOP Variable, it shows you some proxy object, and not the actual InterOP object. For example, if I declare a variable like following :
Dim xlApp as XL.Application (The excel Application object)
set xlApp = ME.Globals.ThisAddin.Application
Now, in the debug mode, after executing the above line, if I want to see what values are stored in xlApp variable, the dot net editor will show the variable as a Runtime.Proxy object most of the times. The reason is, that normally, the InterOP layer creates a wrapper object to avoid any problems running in non EN-US locales.
Due to this behavior, I faced one problem. I was not able to obtain the Type Name of application' selection object because the selection object was coming as a proxy wrapper. I tried something like this :
Dim selectionType as string
selectionType = Information.TypeName(xlApp.Selection)
The above line was giving a System.Reflection.TargetInvocationException. I had to change my project's setting to ensure that the InterOp variables are not proxy objects. For that, I went to MyProject folder in my solution, opened the AssemblyInfo.vb file, change the attribute < Assembly: ExcelLocale1033(True) > set to < Assembly: ExcelLocale1033(False) > and saved the file. And see the difference, it didn't give me a proxy object :)
See the following thread For the example:
MSDN VSTO Thread Link.
Friday, April 3, 2009
Monday, March 30, 2009
Using WithEvents in Vb.Net
Hi,
Using withEvents in VB.net is very easy. I faced lot of problem while accessing the events. For example, activating a chart does not fire a selection change event. I had to do something like whenever I select my chart, my custom menu should appear. I couldn't find doing it in C#(please send a mail if you find it), but in VB.Net it is very easy.
So to take a small example, I have taken a scenario, if I select any of the chart in my workbook, it should display a message. So first of all I created a Class ProxyCharts. The purpose of the class is to serve a Wrapper for Charts Object. This is the class where we will bind our chart objects to WithEvent keyword. So as a wrapper, we will have as many Wrappers as the number of charts available in the workbook. The proxy chart is as follows:
Now we have to bind every chart in the workbook with the ProxyChart. So I added a WorkbookOpen method in my AddIn, which loops to all the charts in the opened workbook.
As soon as a workbook is opened, the WorkbookOpen method is called by default and binds all the charts in the opened workbook with ProxyChartType. After binding, the above code will display a message as soon as you select any of the pre existing charts in workbook.
Hope this helps.
Thanks,
Vikas
Using withEvents in VB.net is very easy. I faced lot of problem while accessing the events. For example, activating a chart does not fire a selection change event. I had to do something like whenever I select my chart, my custom menu should appear. I couldn't find doing it in C#(please send a mail if you find it), but in VB.Net it is very easy.
So to take a small example, I have taken a scenario, if I select any of the chart in my workbook, it should display a message. So first of all I created a Class ProxyCharts. The purpose of the class is to serve a Wrapper for Charts Object. This is the class where we will bind our chart objects to WithEvent keyword. So as a wrapper, we will have as many Wrappers as the number of charts available in the workbook. The proxy chart is as follows:
Public Class ProxyCharts
Private WithEvents _Charts As Chart
Public WriteOnly Property _Chart() As Chart
Set(ByVal value As Chart)
_Charts = value
End Set
End Property
Private Sub _Charts_Activate() Handles _Charts.Activate
MsgBox(_Charts.Name, MsgBoxStyle.Exclamation, "ActivatedChart")
End Sub
End Class
Now we have to bind every chart in the workbook with the ProxyChart. So I added a WorkbookOpen method in my AddIn, which loops to all the charts in the opened workbook.
Private Sub _App_WorkbookOpen(ByVal Wb As Microsoft.Office.Interop.Excel.Workbook) Handles _App.WorkbookOpen
Dim chrtCol As Object
Dim chtCollection As Collection
Dim chrtObj As ChartObjects
For Each wkSht As Worksheet In Wb.Worksheets
chrtCol = wkSht.ChartObjects(Type.Missing)
chrtObj = TryCast(chrtCol, ChartObjects)
If Not chrtObj Is Nothing Then
For Each chrt As ChartObject In chrtObj
Dim chh As New ProxyCharts()
chh._Chart = chrt.Chart
Next chrt
End If
Next wkSht
End Sub
As soon as a workbook is opened, the WorkbookOpen method is called by default and binds all the charts in the opened workbook with ProxyChartType. After binding, the above code will display a message as soon as you select any of the pre existing charts in workbook.
Hope this helps.
Thanks,
Vikas
Labels:
Chart Events,
Chart.Activate() Event,
VB.Net,
With Events
Thursday, March 26, 2009
Linq Example for Group Join
Hi All,
I have just started bringing LINQs to my work, and learning some new things. So I thought I will store it here before I forget it ;)
I had a simple problem, where in I had to loop through a list of files and to get out few Extensions. For example, If I have a list of around 500 files in the drive, I need to get the files with some valid extension, for eg, Images. So for that, I created a files list with the name of fileList and created an array with the valid extensions. I named that array by IMAGES.
Now to get the list of files with valid extensions I used the following code:
Dim result = From s In fileList Group Join p In IMAGES On s.Extension.ToUpper() Equals p.ToUpper() _ Into Group From p In Group Select s
I have just started bringing LINQs to my work, and learning some new things. So I thought I will store it here before I forget it ;)
I had a simple problem, where in I had to loop through a list of files and to get out few Extensions. For example, If I have a list of around 500 files in the drive, I need to get the files with some valid extension, for eg, Images. So for that, I created a files list with the name of fileList and created an array with the valid extensions. I named that array by IMAGES.
Now to get the list of files with valid extensions I used the following code:
Dim result = From s In fileList Group Join p In IMAGES On s.Extension.ToUpper() Equals p.ToUpper() _ Into Group From p In Group Select s
WPF Fish Eye Effect
Hi,
I had a fantasy for Fish Eye effect, and I always have wondered how I will be able to do it one day. But guess what, it is barely simple. I didnt have to do anything much to do it. I am going to explain here in detail about the concept I followed:
Edited on : < May 20th 5.15 PM > Finally, I have got the way to store my projects with in my office. So please download it from
Here
First of all, in Xaml, I have a listview with the Following Code:
< ListView Name="lvwCharts" ItemsSource="{ Binding }" Margin="0,49,0,0">
< ListView.ItemTemplate>
< DataTemplate>
< Grid Width="250">
< StackPanel>
< Label Background="AliceBlue" HorizontalAlignment="Stretch" BorderBrush="Black" BorderThickness="1"
Content="{Binding Path = Name}" />
< Image Name="imgChart"
Height="{Binding Path = Height}"
Width="{Binding Path = Width}"
Source ="{Binding Path = Path, Converter={StaticResource converter}}"
MouseEnter="imgChart_MouseEnter"
MouseLeave="imgChart_MouseLeave"
MouseLeftButtonDown="imgChart_MouseLeftButtonDown"
HorizontalAlignment="Center"
VerticalAlignment="Center"
MouseWheel="imgChart_MouseWheel"
Margin="0"
/>
< /StackPanel>
< /Grid>
< /DataTemplate>
< /ListView.ItemTemplate>
< /ListView>
For Binding the object with the listview, create a class with the Chart Proxy
Public Class ChartsProxy
Private _name As String
Private _group As String
Private _height As Double = 100
Private _path As String
Private _width As Double = 100
Public Sub New(ByVal name As String, ByVal path As String, ByVal group As String, ByVal height As Double)
_name = name
_group = group
_path = path
End Sub
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
Public Property Group() As String
Get
Return _group
End Get
Set(ByVal value As String)
_group = value
End Set
End Property
Public Property Path() As String
Get
Return _path
End Get
Set(ByVal value As String)
_path = value
End Set
End Property
Public Property Height() As Double
Get
Return _height
End Get
Set(ByVal value As Double)
_height = value
End Set
End Property
Public Property Width() As Double
Get
Return _width
End Get
Set(ByVal value As Double)
_width = value
End Set
End Property
End Class
And in your Xaml.vb class add the following two Event handlers
Private Sub imgChart_MouseLeftButtonDown(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
Dim img As Image
Dim proxy As ChartsProxy
Dim index As Integer = -1
img = TryCast(sender, Image)
If Not img Is Nothing Then
proxy = GetParent(img)
If Not proxy Is Nothing Then
index = lvwCharts.Items.IndexOf(proxy)
lvwCharts.SelectedIndex = index
End If
End If
lvwCharts.Focus()
lvwCharts.Items.Refresh()
End Sub
Private Sub imgChart_MouseLeave(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseEventArgs)
Dim img As Image
Dim proxy As ChartsProxy
img = TryCast(sender, Image)
If Not img Is Nothing Then
proxy = GetParent(img)
If Not proxy Is Nothing Then
Dim widthAnimation As DoubleAnimation = New DoubleAnimation()
widthAnimation.From = proxy.Width
widthAnimation.To = proxy.Width - 70
proxy.Width = proxy.Width - 70
widthAnimation.Duration = TimeSpan.FromSeconds(0.3)
Dim heightAnimation As DoubleAnimation = New DoubleAnimation()
heightAnimation.From = proxy.Height
heightAnimation.To = proxy.Height - 50
heightAnimation.Duration = TimeSpan.FromSeconds(0.3)
proxy.Height = proxy.Height - 50
img.BeginAnimation(Image.HeightProperty, heightAnimation)
img.BeginAnimation(Image.WidthProperty, widthAnimation)
End If
End If
'lvwCharts.Items.Refresh()
End Sub
Public Function GetParent(ByVal img As Image) As ChartsProxy
Dim proxy As ChartsProxy = Nothing
Dim prop As DependencyObject
Dim presenter As ContentPresenter
Dim bool As Boolean = True
prop = VisualTreeHelper.GetParent(img)
While bool
presenter = TryCast(prop, ContentPresenter)
If (Not presenter Is Nothing) Then
proxy = TryCast(presenter.Content, ChartsProxy)
If Not proxy Is Nothing Then
bool = False
Else
prop = VisualTreeHelper.GetParent(prop)
End If
Else
prop = VisualTreeHelper.GetParent(prop)
End If
End While
Return proxy
End Function
Edited on : < May 15th 2.13 PM > : As I have posted a very basic version here...if you want a complete code then send a mail at vikasbhandari2@gmail.com. I will send you a complete project.
I had a fantasy for Fish Eye effect, and I always have wondered how I will be able to do it one day. But guess what, it is barely simple. I didnt have to do anything much to do it. I am going to explain here in detail about the concept I followed:
Edited on : < May 20th 5.15 PM > Finally, I have got the way to store my projects with in my office. So please download it from
Here
First of all, in Xaml, I have a listview with the Following Code:
< ListView Name="lvwCharts" ItemsSource="{ Binding }" Margin="0,49,0,0">
< ListView.ItemTemplate>
< DataTemplate>
< Grid Width="250">
< StackPanel>
< Label Background="AliceBlue" HorizontalAlignment="Stretch" BorderBrush="Black" BorderThickness="1"
Content="{Binding Path = Name}" />
< Image Name="imgChart"
Height="{Binding Path = Height}"
Width="{Binding Path = Width}"
Source ="{Binding Path = Path, Converter={StaticResource converter}}"
MouseEnter="imgChart_MouseEnter"
MouseLeave="imgChart_MouseLeave"
MouseLeftButtonDown="imgChart_MouseLeftButtonDown"
HorizontalAlignment="Center"
VerticalAlignment="Center"
MouseWheel="imgChart_MouseWheel"
Margin="0"
/>
< /StackPanel>
< /Grid>
< /DataTemplate>
< /ListView.ItemTemplate>
< /ListView>
For Binding the object with the listview, create a class with the Chart Proxy
Public Class ChartsProxy
Private _name As String
Private _group As String
Private _height As Double = 100
Private _path As String
Private _width As Double = 100
Public Sub New(ByVal name As String, ByVal path As String, ByVal group As String, ByVal height As Double)
_name = name
_group = group
_path = path
End Sub
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
Public Property Group() As String
Get
Return _group
End Get
Set(ByVal value As String)
_group = value
End Set
End Property
Public Property Path() As String
Get
Return _path
End Get
Set(ByVal value As String)
_path = value
End Set
End Property
Public Property Height() As Double
Get
Return _height
End Get
Set(ByVal value As Double)
_height = value
End Set
End Property
Public Property Width() As Double
Get
Return _width
End Get
Set(ByVal value As Double)
_width = value
End Set
End Property
End Class
And in your Xaml.vb class add the following two Event handlers
Private Sub imgChart_MouseLeftButtonDown(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
Dim img As Image
Dim proxy As ChartsProxy
Dim index As Integer = -1
img = TryCast(sender, Image)
If Not img Is Nothing Then
proxy = GetParent(img)
If Not proxy Is Nothing Then
index = lvwCharts.Items.IndexOf(proxy)
lvwCharts.SelectedIndex = index
End If
End If
lvwCharts.Focus()
lvwCharts.Items.Refresh()
End Sub
Private Sub imgChart_MouseLeave(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseEventArgs)
Dim img As Image
Dim proxy As ChartsProxy
img = TryCast(sender, Image)
If Not img Is Nothing Then
proxy = GetParent(img)
If Not proxy Is Nothing Then
Dim widthAnimation As DoubleAnimation = New DoubleAnimation()
widthAnimation.From = proxy.Width
widthAnimation.To = proxy.Width - 70
proxy.Width = proxy.Width - 70
widthAnimation.Duration = TimeSpan.FromSeconds(0.3)
Dim heightAnimation As DoubleAnimation = New DoubleAnimation()
heightAnimation.From = proxy.Height
heightAnimation.To = proxy.Height - 50
heightAnimation.Duration = TimeSpan.FromSeconds(0.3)
proxy.Height = proxy.Height - 50
img.BeginAnimation(Image.HeightProperty, heightAnimation)
img.BeginAnimation(Image.WidthProperty, widthAnimation)
End If
End If
'lvwCharts.Items.Refresh()
End Sub
Public Function GetParent(ByVal img As Image) As ChartsProxy
Dim proxy As ChartsProxy = Nothing
Dim prop As DependencyObject
Dim presenter As ContentPresenter
Dim bool As Boolean = True
prop = VisualTreeHelper.GetParent(img)
While bool
presenter = TryCast(prop, ContentPresenter)
If (Not presenter Is Nothing) Then
proxy = TryCast(presenter.Content, ChartsProxy)
If Not proxy Is Nothing Then
bool = False
Else
prop = VisualTreeHelper.GetParent(prop)
End If
Else
prop = VisualTreeHelper.GetParent(prop)
End If
End While
Return proxy
End Function
Edited on : < May 15th 2.13 PM > : As I have posted a very basic version here...if you want a complete code then send a mail at vikasbhandari2@gmail.com. I will send you a complete project.
Subscribe to:
Posts (Atom)