Skip to content

WPF Persist UI State for VB.NET

March 11, 2010

First, I understand that VB.NET is the code behind and that technically WPF being the UI should mean that there is little/no difference in the XAML whether using C# or VB but I wasted time looking for this solution and finally figured the last little bit out on my own.

If you go into project properties (either Project -> Properties or right click on your project in the Solution Explorer and choose properties) one of the selections available is Settings. For a simple example of what you can do here, if you wanted to persist the full state of your window named ‘windowMain’ in windowMain.xaml, you could add the following to your settings list:

List of Settings

List of Settings

Then from your XAML you need to add the following to your window declaration:

Height="{Binding Source={x:Static p:MySettings.Default}, Path=windowMainHeight,Mode=TwoWay}"
Width="{Binding Source={x:Static p:MySettings.Default}, Path=windowMainWidth,Mode=TwoWay}"
Left="{Binding Source={x:Static p:MySettings.Default}, Path=windowMainLeft,Mode=TwoWay}"
Top="{Binding Source={x:Static p:MySettings.Default}, Path=windowMainTop,Mode=TwoWay}"
WindowState="{Binding Source={x:Static p:MySettings.Default}, Path=windowMainState,Mode=TwoWay}"

Finally you need one line of code in your Window’s Closing event:


This has been written so many times by so many different people that I wouldn’t have bothered except for the fact that 100% of the examples I found were for C#. This is the equivalent of using the following in C# (I believe):

Height="{Binding Source={x:Static p:settings.Default}, Path=windowMainHeight,Mode=TwoWay}"
Width="{Binding Source={x:Static p:settings.Default}, Path=windowMainWidth,Mode=TwoWay}"
Left="{Binding Source={x:Static p:settings.Default}, Path=windowMainLeft,Mode=TwoWay}"
Top="{Binding Source={x:Static p:settings.Default}, Path=windowMainTop,Mode=TwoWay}"
WindowState="{Binding Source={x:Static p:settings.Default}, Path=windowMainState,Mode=TwoWay}"

Combining the TwoWay binding with the .Save() call in the Closing event means that however you move or change the state of the window, it will be saved and look the same when you reopen it. I have applied this same concept to all my grid column widths and various other UI elements so a user can truly customize and tweak the little things and have them stay that way.

One caveat is that this does not deal with the problems related to multiple monitors and the potential of restoring a window to a position that no longer exists. I have found what seems to be a good solution, with source code, for this issue if you are encountered with it here.

Also note this stores the settings by user on the machine the application runs on so changes made by one user will not affect another user on the machine, but the settings will not follow the user machine to machine.


Crystal Reports XI: SQL Commands, Parameters and Prompts

January 21, 2010

One of my favorite things about Crystal Reports XI is the SQL Command. This is lamented by co-workers who don’t know SQL but sometimes have to deal with my reports, but I effectively decrease the network traffic (though all reports are run internal so it’s not a huge concern) and significantly offload the process onto the server. That said, they have a few stinging caveats.
Read more…

Microsoft Windows Updates October 2009

October 22, 2009

I cannot find any posts out there in a brief scan about this, and just hope this is picked up and someone who knows the answers can give them. At home I had an issue that seems to revolve around a set of updates released on October 15th… they seem to relate to the Windows Firewall and even with it turned off, my problems persist.

At work last night they installed a set of updates, I think some from the October 15th slew and guess what? This morning we had to turn off Windows Firewall on our servers because suddenly several things stopped working. The only one I can say FOR CERTAIN was broken was Windows File Sharing. Turning off Windows Firewall fixed this. We have exclusions inside the Windows firewall to allow file sharing even when its on, but either the exclusions are ignored in the newest patch, or somewhere else file sharing is specifically broken.

We got a work-around here at work, but now I’ll just wait for a future patch before my home machine will start to run as desired again.

Crystal Reports Autosave

October 14, 2009

I have previously posted that Crystal Reports does not autosave even when you have the option turned on.

I was mistaken.

Crystal Reports saves the currently open report to: C:\Documents and Settings\[[USERNAME]]\Local Settings\temp

With the name: reportname.autosave.rpt

If you have not saved it yet, you’ll see like Report1.autosave.rpt

I stand corrected. Now what they were thinking when they decided that auto-saving to an entirely different directory and file name was a good idea escapes me. Beyond that, why they don’t have some sort of detection that there is an auto-saved version that you may want to load up also escapes me. As I have pointed out before, any modern word processor or document design product out there does this and why they cannot, I don’t know. However, my time should be wasted no more as I can recover the autosaved documents from this folder at least.

Telerik RadGridView Filtering on Strings

August 7, 2009

The Telerik WinForms RadGridView control has proven to be very impressive to me thus far. Anything that automates and manages a large portion of the creation and functionality of your application is going to make decisions you don’t like but overall this one seems very good.

The Good:

I created a typed DataSet, complete with proper relationships between master/detail tables.
I then dropped a radGridView control on the form, set the datasourse as my dataset, the member as my ‘master’ table and then told it to AutoGenerateHierarchy. In those couple easy steps I created a triply-nested datagrid that admittedly wasn’t very pretty but it functioned as would be expected and was about 50% of what I needed out of the simple application request I received.

For the Good of the filters, it was very easy to create a filter following their example, and very easy to perform filtering based upon integer values. Also the filter/gridview are very responsive with small to moderate-sized datasets, but I’ve not tried anything large yet.

The Bad:

I created a filter and programatically placed it on one of the columns per their tutorial and was disturbed to find that using their EqualTo function, pasting the contents of a cell into the filter bar did not return any results. This means that the cell from which I took the value did not match itself. I did figure out what was happening (details here); basically their filter was stripping prepending spaces for comparison despite visually displaying them. This meant that pasting in ‘ 41’ would not match ‘ 41′ but rather ’41’.

The Fix:

Just strip prepending spaces in your query/dataset before putting it into the grid if possible. If you cannot, they have responded in the afore-mentioned and linked forum post that they may change the functionality in the future.

Mutable vs. Immutable / StringBuilder vs. String

July 14, 2009
tags: , , ,

So, strings are immutable. What this means in application is that modifying a string creates new string data on the heap and modifies the pointer on the stack to point to the new location. The data on the heap that is not referenced from the stack will be cleared at garbage collection time, but until then it’s wasteful.

In comparison, StringBuilders are mutable. Modifications/additions to the stringbuilder will update the data in the heap instead of making copies of it each time.

StringBuilder sb = new StringBuilder;


sb.Add(” and test2″);

That code will create a pointer on the stack and a data value on the heap, update that data value to add the second string and that’s it. Doing the same with strings would have left an abandoned string data value on the heap of value “test” waiting on the GC.

Value Types

July 14, 2009
tags: , , ,

I got the books for the MCPD and have started studying and have found a couple interesting facts that I’m sure I should have known but thought I would share a little.

Of the numeric types, I always assumed you should use the smallest type that fit your usage. If you needed numbers 1-100 you could easily use a tinyint, etc. Turns out that since the compiler is optimized for 32-bit, ‘Integer’ is the most efficient type. If you are trying to squeeze every last bit of memory out of the application, fine, but for most apps, Integer will be better performance than any other numeric type.