Sunday, June 12, 2011

VSTO: Changes to Zoom percentage not persisted

I have been playing around with VSTO and for some odd reason, my zoom percentage changed to 10% and any subsequent changes to the zoom percentage were not persisted. Using Google's Code Search, I found some examples of how to change these settings in the code-behind:

this.ActiveWindow.View.Type = Word.WdViewType.wdPrintView;
this.ActiveWindow.View.Zoom.Percentage = 100;

But it is still annoying when going into design-mode and having to zoom in again.

One way to fix this is to close Visual Studio, create a copy of the docx file, make the necessary changes and replace the old file.

Tuesday, February 8, 2011

WCF: Programmatically set dataContractSerializer's maxItemsInObjectGraph value

When programmtically setting up a WCF Client, one often have to set the dataContractSerializer's maxItemsInObjectGraph parameter (within the serviceBehaviors) to a higher value than the default 65536.

I struggled a bit to find this one on-line, but finally came across a solution on here.

Here's the way to do it:
foreach (OperationDescription operation in myClient.Contract.Operations)
   operation.Behaviors.Find<DataContractSerializerOperationBehavior>().MaxItemsInObjectGraph = 2147483646;


Where the myClient object is an instance of the ServiceClient.

Sunday, April 25, 2010

IIS 7 and SSL Error

If ever faced with the error ssl_error_rx_record_too_long in IIS 7, check to see if you have IIS 6 Compatibility switched on (Control Panel -> Programs and Features -> Turn Windows features on or off -> Internet Information Services -> Web Management Tools -> IIS 6 Management Compatibility).

Monday, March 29, 2010

WPF: Dynamically create a Table

Being new to WPF, there are a few funnies you'd need to know about before being able to create a simple table with text.

First, one can't simply add a Table within a Grid container. Tables need to be housed within a FlowDocument. One also need a container to display these FlowDocuments. I found the FlowDocumentScrollViewer to be the simplest. Some code to display a blank table:
<FlowDocumentScrollViewer
VerticalScrollBarVisibility="Disabled"
HorizontalScrollBarVisibility="Disabled">
<FlowDocument>
<Table Name="myTable"></Table>
</FlowDocument>
</FlowDocumentScrollViewer>

To populate the table programmatically with some basic text one need to follow the next procedure (the code is pretty much self-explanatory):
int cols = 5;
int rows = 10;

for (int c = 0; c < cols; c++)
myTable.Columns.Add(New TableColumn());

for (int r = 0; r < rows; r++)
{
TableRow tr = new TableRow();

for (int c = 0; c < cols; c++)
tr.Cells.Add(New TableCell(New Paragraph(New Run("Some Text"))));

TableRowGroup trg = new TableRowGroup();
trg.Rows.Add(tr);
myTable.RowGroups.Add(trg);
}

I must say that this approach (a Run within a Paragraph within a TableCell within a Row within a RowGroup) isn't terribly intuitive and the process seems a bit tedious when only trying to create a simple table.

Thursday, December 10, 2009

C#: Calling/Overriding Grandparent Base Class Methods

I use a base class for 99% of my ASP.Net pages, which executes a few operations in the OnInit() method. Here's a simplified version:
public abstract class BasePage : System.Web.UI.Page, IBasePage
{
protected override void OnInit(EventArgs e)
{
// Do stuff
Method1();
Method2();
Method3();

// Call System.Web.UI.Page.OnInit
base.OnInit(e);
}
}
This works great for all my child classes/pages, but there are a few scenario's where I'd only like to call a specific set of methods, as illustrated here:
public class MyPage: BasePage
{
protected override void OnInit(EventArgs e)
{
// Ommit Method2()
Method1();
Method3();

// This calls BasePage.OnInit, which makes
// redudant calls to Method1() and Method3(),
// and the undesired call to Method2().

base.OnInit(e);
}
}
It is obvious that the last base.OnInit(e) call will have the undesired effect. Being 6:23 PM, after coding all day, my fried brain's initial thought was to bypass the base class' OnInit(e) method and to call the grandparent's OnInit(e) method directly.

An obvious faux pas. Whenever venturing into the "grandparent" or "grandchild" realm (more than one degree of seperation), you're probably going down the wrong path.

The proper and easy approach is merely to move the method calls out to a seperate method, which one overrides in the child class like so:
public abstract class BasePage : System.Web.UI.Page, IBasePage
{
protected override void OnInit(EventArgs e)
{
// Do stuff
Initialize();

// Call System.Web.UI.Page.OnInit
base.OnInit(e);
}

protected virtual void Initialize()
{
Method1();
Method2();
Method3();
}
}

public class MyPage: BasePage
{
protected override void Initialize()
{
// Do stuff
Method1();
Method3();
}
}

Monday, December 7, 2009

GNU Plot and SVG: Change the terminal size and font

Again, not really a dev related issue, but I did not find much info online regarding this.

When using gnuplot to create SVG files, one use the set term svg command that sets the following defaults:
gnuplot> set term svg
Terminal type set to 'svg'
Options are 'size 640 480 fixed fname 'Arial' fsize 12 butt '

One can speficy custom parameters to get the desired output:
gnuplot> set term svg size 640,350 fname 'Times New Roman' fsize 10
Terminal type set to 'svg'
Options are 'size 640 350 fixed fname 'Times New Roman' fsize 10 butt '

Monday, November 23, 2009

Type 'DataContract' is not defined

This error can be resolved by adding a reference to the .net System.Runtime.Serialization assemly and also importing this namespace in the file where applicable.

Also, applies to Type 'DataMember' is not defined.