Jun
30
2009

C#: XML with XDocuments



Posted in Programming

I’ve always used XmlDocument when I’ve needed to create or read XML, but I while I was implmenting a SOAP service for ChickenPing I discovered XDocument. XDocument offers a much more concise way of specying XML without all the boilerplate code.

For example, this snippet using XmlDocument:

XmlElement root = doc.CreateElement("recipeml");
XmlElement hdVersion = doc.CreateElement("version");
hdVersion.InnerText = "0.5";
root.AppendChild(hdVersion);
XmlElement hdGenerator = doc.CreateElement("generator");
hdGenerator.InnerText = "ChickenPing v" + Constants.GetVersion().ToString(3);
root.AppendChild(hdGenerator);
...

Read more »

Jun
13
2009

Editable ComboBox with XCeed DataGrid



Posted in Programming and WPF

I’ve recently started using XCeed’s DataGrid for WPF instead of he WPF toolkit DataGrid because overall it seems to have more features. One of the problems which I couldn’t find any reference to was using editable comboboxes when editing an item. I’m using an ObjectDataProvider as the ItemsSource for a ComboBox, but I wanted to let a user type a value as well as select one from the list.

The solution I eventually used lets a user add an item to the list of items which can then be selected when editing other items in the DataGrid.

Once you’ve declared the ObjectDataProvider in XAML, you need a CellEditor template for the column which will use the ComboBox (code below).

<xcdg :Column.CellEditor>
    </xcdg><xcdg :CellEditor>
        </xcdg><xcdg :CellEditor.EditTemplate>
            <datatemplate>
                <combobox x:Name="cboMeasurement" IsVisibleChanged="cbo_IsVisibleChanged"
                      ItemsSource="{Binding Collection,Source={StaticResource measurementProvider}}" SelectedValue="{xcdg:CellEditorBinding}" IsEditable="True" Foreground="Black" IsSynchronizedWithCurrentItem="True" />
            </datatemplate>
        </xcdg>

Read more »

May
7
2009

Guide: Subversion with Visual Studio



Posted in Uncategorized

I’ve just published a guide to source control with Visual Studio, AnkhSVN and TortoiseMerge.

May
1
2009

WPF: DataGrid And The Return Key



Posted in Programming and WPF

A decent DataGrid is really the only thing WPF doesn’t have. The WPF Toolkit DataGrid is a great addition but it’s still lacking in a few places.

To me, it would seem natural for the Tab key to move to the next column and Enter to move to the next row, but there isn’t really support for this.

After experimenting with KeyUp and KeyDown events, I came up with the following method, based on a similar technique by Vincent Sibal. It makes the DataGrid more usable in my opinion.

Read more »

Apr
27
2009

C#: AutoSave with WPF



Posted in Programming and WPF

Creating an automatic save for ResourceBlender Express was actually a lot easier than I thought it would be but I’ll post the source here for anyone else wondering how to do it.

My .xaml.cs file has four class variables to make it work, and the save method executes in a separate thread to prevent it locking the UI during saving (actually a BackgroundWorker).

BackgroundWorker worker;
DispatcherTimer timer;
int autoSaveInterval = 3000;
DateTime lastSave;

The DispatcherTimer times the save at a specified interval and the BackgroundWorker executes the operation. In the class constructor I setup the DispatchTimer:

Read more »

Apr
10
2009

C#: Path from two base paths



Posted in Programming

ResourceBlender dialog

Ever needed to give a user the option to enter some details and generate a path which could be a file or directory depending on what they enter, then change the path when they change their minds?

ResourceBlender uses the dialog to the right to update a filename when either of two sets of radio buttons change, or the user decides the want to output to a zip file instead of a directory.

This static method will go through each path to find a valid one. Once it’s found one, it determines whether the first path provided is a file or directory (it only uses the extension of the file, which worked for me. If you need more accurate checking you’ll need p/invoke). The file and base directory names are combined then the result returned. If the filename (the first parameter), the backup filename is used instead.

Read more »

Mar
11
2009

WPF: GridView Column Width Calculator



Posted in Programming and WPF

The GridView in WPF isn’t bad, but is pretty basic compared to the one in ASP.NET. There’s isn’t really a decent method for setting column width, so I wrote this converter to calculate the width needed to fill the parent ListView.

If you call it with no parameters, it’ll take up the remaining width in the ListView:

// fills remaining width in the ListView
<gridviewcolumn Width="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type ListView}},Converter={StaticResource WidthConverter}}">
</gridviewcolumn>

If you use an integer as a parameter, the value will act as the minimum width

// fills remaining width in the ListView, unless the remaining width is less than the parameter
<gridviewcolumn Width="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type ListView}},Converter={StaticResource WidthConverter},ConverterParameter=200}">
</gridviewcolumn>

Or, you can specify a GridView type width with an asterisk, and the percentage width of the ListView will be returned

// calculates 30% of the ListView width
<gridviewcolumn Width="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type ListView}},Converter={StaticResource WidthConverter},ConverterParameter=0.3*}">
</gridviewcolumn>

Read more »

Jan
20
2009

Java: CheckBoxList



Posted in Programming

I’ve been working a lot with ASP.NET recently, and the one thing .NET does miles better than Java is data-binding. In .NET if you want to show a collection as a list of checkboxes, you databind the collection to a CheckBoxList. Unfortunately, Swing doesn’t contain a CheckBoxList class but creating one didn’t take too long.

The class below provides a list of items. Use a LinkedHashMap<T, Boolean> of items to populate the list, where T, the key, is any type which can be represented by calling it’s toString() method and value is a boolean indicating the initial check state of the item.

package com.lime49.lockcrypt;
 
import java.awt.*;
import java.awt.event.*;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import javax.swing.*;
import javax.swing.border.*;
 
/**
 * A list of checkboxes which maintains a list of custom objects
 * http://www.lime49.com/
 * Copyright 2009 by Harry Jennerway
 * All rights reserved.
 */
public class CheckBoxList<t> extends JList {
    protected static Border noFocusBorder = new EmptyBorder(1, 4, 1, 4);
    private CheckBoxListModel</t><t> model;
 
    /**
     * Initializes a new CheckBoxList with the specified object collection
     * @param items The items to add to the list
     */
    public CheckBoxList(LinkedHashMap</t><t , Boolean> items) {
        model = new CheckBoxListModel</t><t>(items);
        setModel(model);
        addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                int index = locationToIndex(e.getPoint());
                if(index != -1) {
                    ListItem</t><t> item = (ListItem</t><t>)model.getElementAt(index);
                    item.selected = !item.selected;
                    revalidate();
                    repaint();
                }
            }
        });
        setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        setCellRenderer(new CellRenderer());
    }
    ...
}
</t>

Download CheckBoxList.java
Read more »

Jan
5
2009

Default wizard button in ASP.NET



Posted in Uncategorized

When you press enter/return on a wizard control in ASP.NET, the default action is to just submit the form, disregarding any buttons which were pressed. This seems pretty counter-intuitive to me – if I press enter on a form, it should be obvious I want to go ‘Next’. After two hours trying to debug an installer wondering why it seemed to work sometimes and not others I eventually tracked it down to the default button. I’m using a master page, but the principle is the same if you’re not.

The method I used uses the Page_Load event to set the default button for the form.

Read more »

Nov
22
2008

GridView with Dynamic Columns



Posted in ASP.NET and Programming

The ASP.NET GridView control can be a really useful control for displaying and editing tabular data, but if you try to make it work with MySQL or a cross-tab query you’re likely to run into problems.

I wanted to create a grid showing the data from a cross-tab but also allow users to edit the data. If you’ve never used one before, a cross tabulation is a query which maps repeating rows to columns instead. This means that a column in a displayed result might not actually exist as a column in a database table. If you just need to display the data and aren’t bothered about editing it, the GridView works out of the box and will automatically generate the columns for you – but what if you need to display some columns but not others?

Read more »



Page 2 of 29«12345»1020...Last »