TargetInvocationException Error

January 2, 2008 by · Comments Off on TargetInvocationException Error
Filed under: Uncategorized 

I found my team spending most of the day trying to track down an error that was only occurring on Windows Vista.   We built our application and had our testers testing the software.  Some testers use Windows XP while others use Windows Vista.  Everything worked fine on Windows XP but there was an error on the Vista machine.  The entire team was baffled as to why the code worked fine with Windows XP and didn’t work on Vista.  Although it is obvious that some code would not work correctly between the operating systems there is a twist.   Out previous version of this software works perfectly on Vista.  The same exact code in our old version works fine but doesn’t work in the latest version.  I decided to build a test application that would test the part that was breaking on my Vista machine.  We do all of our development in Visual Studio 2005 so I fired it up and started hacking some code.  Ironically enough, the code that threw the error in our application worked fine in my sample code.  After a lot of searching online I finally came across the follow site: https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=299397.  What I found out was that when you build applications with VS 2005 or VS 2008 that contain ATL controls built with VS 2003 you can run into issues.  This only happens after you install visual studio 2008 and the only way to reverse this action is to reformat your machine.  This error is only reproducible on some Vista machines and doesn’t effect Windows XP.  Our solution was to find a machine that hadn’t had VS 2008 installed yet to do our build on.  As soon as we tried that build everything worked great. Before I mentioned that I was successful in running the code in my test application.  This did work, but come to find out it was only because I was running it through visual studio.  When I tried to just go into my bin directory to run the executable file it threw the error.  Apparently, the error will not be thrown through the IDE but as soon as you try to run the application outside of that sandbox it will blow up. 

I hope that this may help others while Visual Studio 2008 is being released incase you run into this same problem.  There are not a lot of listings yet regarding this but the solution did work.

Windows Live Writer Released

December 2, 2007 by · Comments Off on Windows Live Writer Released
Filed under: Uncategorized 

Microsoft has finally released a final version of Windows Live Writer.  You can get it from here http://get.live.com/writer/overview.  My posts prior to this one have been written with the beta version of Live Writer. So far the experience has been really positive.  There are already some plug-ins to insert code snippets.  I haven’t tried them in the final release but in the beta I did notice some issues with the code where it would ignore the line breaks.  This made it difficult because 10 lines of code now got moved to one big long string.  That made it difficult to read and I had to go in and edit it manually.  I will post more when I get a chance to write more with this tool. 

ASP.Net Mobile Authentication

October 28, 2007 by · Comments Off on ASP.Net Mobile Authentication
Filed under: Uncategorized 

I had the opportunity last week to help a co-worker trouble shoot some authentication issues with his ASP.Net mobile web site.  This is the first mobile site the company has created and is response to creating some applications that can be made available on blackberry devices.  The site was put together rather quickly using Forms authentication.  It was interesting to see that on  the blackberry 8800 the site worked fine.  We were able to log in and navigate from page to page.  To my surprise, my Cingular 8525 did not work quite as well.  I was able to log in to the site but as soon as I selected one of the links to another page I was sent back to the log in screen. 

It turns out that pocket IE does not support redirect with cookies.  This is the default for forms authentication.  To solve this issue, one must make sure that they do not use relative urls.   What happens in this scenario is that the authentication token is inserted into the url to be passed between the pages.  If your page path is like this: 

https://jardinesoftware.net/Mobile/Somepage.aspx

It would be changed to be:

https://jardinesoftware.net/Mobile/<authtoken>/Somepage.aspx

Notice how the authtoken is inserted.  By default, when you add a link to the web page on a mobile device it sets the navigateurl to "~Somepage.aspx".   This causes the issue with ie because it overwrites the authtoken piece.   The simple fix was to remove the ‘~’ in the path.  With that small change, the links work fine with pocket ie. 

One other note about the forms authentication for Mobile web sites.  They use a different object for setting the authentication cookie.   Instead of FormsAuthentication you need to use MobileFormsAuthentication.

Outlook 2007 Hyperlink Security Warning

October 23, 2007 by · Comments Off on Outlook 2007 Hyperlink Security Warning
Filed under: Uncategorized 

Everyone knows that when a new version of software is released that there are many new features included. Unfortunately, a lot of these features are not documented that well. I came across an issue regarding a warning message in Outlook 2007 when clicking on a hyperlink pointing to the file system. The warning lets the user know that following the link could be dangerous and asks for confirmation before proceeding. After searching around for about 30 minutes I finally stumbled on to a KB article showing how to stop this warning. The solution requires modifying the registry by adding a new Security key named DisableHyperLinkWarning and setting the value to 1. Be careful changing this value as it will disable all hyperlink warnings in all of your Office 2007 programs. This can create a security risk. I have had other people ask about this issue and I hope that this will help others in fixing the problem.

Visual Studio 2005 C++ Project Creation Failed

October 23, 2007 by · Comments Off on Visual Studio 2005 C++ Project Creation Failed
Filed under: Uncategorized 

I recently was assigned the task of creating a pocket pc service using visual c++ and was going to attempt to use Visual Studio 2005 to do this.  I opened up the IDE and clicked on "New Project" and then selected c++ smart device application.  To my dismay, the IDE got upset and said it could not create the project.  I was baffled.  I tried doing a repair on Visual Studio and that didn’t help.  After searching online for a while I finally stumbled across this blog on msdn http://blogs.msdn.com/vsdteam/ that has an excerpt about Visual Studio 2005 and Vista having problems creating native c++ apps.  The fix, which is in the article, explains how it is a bug with visual studio not getting along with IE7 (which I have installed).  With one simple registry modification I was able to fix the problem.  The blog states that the fix is in Visual Studio 2005 SP1 but until you install that, this registry hack fixes the problem.

Here is the fix.

Open the registry entry:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Ext\PreApproved

Add a new entry and name it {D245F352-3F45-4516-B1E6-04608DA126CC}

(this was originally posted on my original blog on9/13/2006)

WinForms New AutoComplete Functionality

October 23, 2007 by · Comments Off on WinForms New AutoComplete Functionality
Filed under: Uncategorized 

One of the nice new features that have been added to winforms in 2.0 is the AutoComplete functionality for textboxes and comboxes. By setting up a data source (really just a source of strings) one can quickly add auto complete functionality to their forms. It is pretty simple to set up the auto complete functionality. There are three properties that are associated with the auto complete:

  • AutoCompleteMode – Specifies the mode that the automatic completion will use.
    • Append – Appends the remainder of the first candidate string to the existing text, highlighting the remaining characters.
    • None – Disables automatic completion feature.
    • Suggest – Displays a dropdown list of suggested completion strings.
    • SuggestAppend – Applies both Suggest and Append options.
  • AutoCompleteSource – Specifies the source of strings to make suggestions from.
    • AllSystemSources – This is the equivalent of FileSystem and AllUrl as the source. This is the default if AutoCompleteMode is set to a value other than “None”.
    • AllUrl – This is the equivalent of HistoryList and RecentlyUsedList as the source.
    • CustomSource – This specifies strings stored in an AutoCompleteStringCollection. This requires the AutoCompleteCustomSource property to be set.
    • FileSystem – Specifies the file system as the source.
    • FileSystemDirectories – Specifies that only Directory names will be completed.
    • HistoryList – Includes the urls in the history list.
    • ListItems – (combobox) specifies that the items in the combobox act as the source.
    • None – Specifies that no source is in use. This is the default.
    • RecentlyUsedList – Includes URLs in the list of those most recently used.
  • AutoCompleteCustomSource – Specifies a custom AutoCompleteStringCollection as the source for the suggestions.

What is Supported

The auto complete functionality is only supported by the textbox and combobox control. To further limit this list, the textbox only supports this when in single line mode. Multi-line mode ignores any settings for the AutoComplete properties.

The AutoCompleteMode is pretty self explanatory. You might, however, be confused on some of the AutoCompleteSource items. The difference between FileSystem and FileSystemDirectories is that FileSystem will also list files in the suggestion and FileSystemDirectories will only list folders. Both, however, appear to not work at first glance. If you type in C: you will not see any suggestions. This may make you think it is not working but as soon as you type C:\ you will see the list of suggestions. As you continue building onto the path by adding more ‘\’ characters you will continue to receive more suggestions. The url types work a little better because as soon as you type the ‘h’ in http you will start to see a list of suggestions.

So far we have seen how easy it is to set up the textbox or combobox to auto suggest data based on built in values. Although, actually seeing this work makes one wonder where they pull that information from. The real question is how do I add my own list of suggestions. This is where the AutoCompleteCustomSource comes into play. The designers have graciously given us a new Collection called AutoCompleteStringCollection. All this collection does is store a list of string values. Once you populate the collection with your list of suggestions then you can set the AutoCompleteCustomSource to your new collection.

Example CustomCollection

Dim _myCollection as New AutoCompleteStringCollection() 
For _index as Int32 = 0 to 10 
_myCollection.Add(String.Format(“Custom String {0}.”,_index)) 
Next 
txtSuggestions.AutoCompleteCustomSource = _myCollection 
txtSuggestions.AutoCompeteMode = AutoCompleteMode.Suggest 
txtSuggestions.AutoCompleteSource = AutoCompleteSource.CustomSource 

When the above code is added and the form is run then as the user types in the values Custom String the suggestions will appear in the dropdown that is displayed.

(this was originally posted on my original blog on 7/27/2006)

TablePanelLayout: An Overview

October 23, 2007 by · Comments Off on TablePanelLayout: An Overview
Filed under: Uncategorized 

The .net framework 2.0 has brought a few new controls to the winform environment. Among these new controls is the TableLayoutControl. At first glance, one might wonder why there is a need for this new control. It is easy to drag and drop controls exactly where you want them in a windows form. The control does exactly what it says, displays data in a table format. This technique is common to web developers. With correct table design it is possible to make very interesting layouts on the screen. This has now been ported to the desktop, but is it as useful as expected?

The TableLayoutPanel can be found during design time in the toolbox under Containers. When you first drag the control onto the form it will prompt you with a dialog to set up its rows and columns. By default, the panel has 2 rows and 2 columns. The panel is now ready to be set up how it is needed. Row and column spanning is fully supported.

So how are controls added to the table? Controls are added in design time by dragging the control into a cell. The first thing you should start to notice is that a cell can only contain one control. As a control is dragged over a cell with a control already in it, the drag icon signals that the item cannot be dropped there. This appears to be the first limitation with the tablelayoutpanel. To get around this limitation, one would have to create a custom composite control to use. This would allow multiple controls to be placed into a single cell. Adding controls at runtime is done by calling the TableLayoutPanel.Controls.Add method. Controls are added to the next available cell.

The tablelayoutpanel’s GrowStyle property determines how the cells are added at runtime. The GrowStyle property has three values:

  • Fixed Size – This value does not allow adding more columns or rows then what was set up initially. An attempt to add a control when the table is full will result in a ArgumentException.
  • AddColumns – This value means that when new controls are added and the table is full, it will automatically add a new column and add the control there.
  • AddRows (default) – This value means that when new controls are added and the table is full, it will automatically add a new row and add the control there.

If you have already tried the tablelayoutpanel you might have found that all of your data is not displayed. By default, the control sets the AutoSize property to false. When adding controls at runtime it is easy to overrun the the bounds of the default grid. Setting the AutoSize to True will allow the grid to grow with the data.
When a control is added to the tablelayoutpanel it provides four new properties (although the properties actually exist in the panel and not in the control):

  • Column – This represents the column index of the control.
  • ColumnSpan – This represents the number of columns the control spans.
  • Row – This represents the row index of the control.
  • RowSpan – This represents the number of rows the control spans.

The listed properties are available during design time by editing them in the properties window of the specified control. These properties are only available during runtime by using the following methods of the TableLayoutPanel:

  • SetColumnSpan – Sets the column span of the specified control.
  • SetColumn – Sets the column index of the specified control.
  • SetRowSpan – Sets the row span of the specified control.
  • SetRow – Sets the row of the specified control.

An example would be:
TableLayout1.SetColumnSpan(2,Label1)

Here is an example of a table that has 2 buttons side by side followed by an image below them (repeated).

Create a windows form and add a tablelayoutpanel.
Set the AutoSize property to true.
Leave the default 2×2 table layout.
In the Form_Load event add the following code.

 For _index as Int32 = 0 to 3 
 Dim _button1 as New Button 
 _button1.Text = "Save" 
 TableLayoutPanel1.Controls.Add(_button1) 
 Dim _button2 as New Button 
 _button2.Text = "Print" 
 TableLayoutPanel1.Controls.Add(_button2) 
 Dim _picBox as new PictureBox 
 _picBox.Image = Image.fromFile("C:\filePath.jpg") 
 TableLayoutPanel1.Controls.Add(_picBox) 
 'Add the column spanning for the photo.  
 TableLayoutPanel1.SetColumnSpan(2,_picBox) 
Next 


After running this code you should see a table with two buttons above an image repeated a few times. The next question would be how to change the column widths and heights. There is another post about this that you can view. It is performed by using the row and column style classes.


The TablePanelLayout control can be useful when the situation is right. Many people are against the new panel but after gaining a solid understanding of the control, it can be very useful.

(this was originally posted on my original blog on 7/10/2006)

TableLayoutPanel Styles

October 23, 2007 by · Comments Off on TableLayoutPanel Styles
Filed under: Uncategorized 

Setting Styles:

In the midst of working with the tablelayoutpanel I found I needed to control the styles of the cells. There is a lot to cover for controlling the rows and columns, so for now I am just going to cover the basics. The row styles are controlled by the TableLayoutRowStyle class which is contained in the TableLayoutRowStyleCollection. The column styles are controlled by the TableLayoutColumnStyle class which is contained in the TableLayoutColumnStyleCollection. To update a specific row to have a set style you would insert a style into the collection at the index corresponding to the row index. So, for example, if I want to update my 3rd row of 5 to be 200 pixels tall instead of the default then I would add the following code:

tablePanel.RowStyles.Insert(2, New RowStyle(SizeType.Absolute,200)) 

To update a column to be a specific size, ex. Fit contents, then I would add the following code:

tablePanel.ColumnStyles.Insert(1,New ColumnStyle(SizeType.AutoSize)) 

A common need to change the styles would be if you were creating a header column that only needed to be 50 pixels high and the rest of your columns were 150 pixels high. I will be adding more examples for the tablelayoutpanel soon.

(this was originally posted on my original blog on 6/27/2006)

Hide properties in BindingList

October 23, 2007 by · Comments Off on Hide properties in BindingList
Filed under: Uncategorized 

I was recently working on a project that needed a custom list of objects bound to a DataGridView.  I found the BindingList but soon realized that it bound all of my public properties to the DataGridView.  This was not the outcome I was looking for so I started doing a lot of research.  I couldn’t find any specific information relating to hiding properties. 

Browsable Attribute

I started looking at the Browsable attribute found in System.ComponentModel.  The attribute is used to hide properties from a properties grid control but doesn’t make any mention of working with BindingList.  I ran a few tests and found that if I mark my properties with the Browsable attribute it would in fact alter my DataGridView. 

By default, all of the public properties are browsable so I only need to mark the properties I don’t want visible.  Here is a sample hidden property:

[Browsable(false)]
public string MyProperty 
{
 get{return _myProperty;}
 set{_myProperty = value;} 
}

Using this example, I added this attribute to each property I did not want displayed in the DataGridView.  I could have manipulated the DataGridView when I performed the DataBinding but that would have limited me to modifying that code every time I might add or remove a property.  This solution allows me to control the viewable properties from my class.

In my next post I will show how to set the column order for your properties using attributes and a little code when data binding.



« Previous Page