c#

Read a file into a string. The StreamReader also accepts an encoding if you have utf-16 input

using( StreamReader sr = new StreamReader(ofd.FileName[, System.Text.Encoding.Unicode]) )
	str = sr.ReadToEnd();

// or from a MemoryStream or other buffer

MemoryStream mem = someFunc(...);
StreamReader	sr = new StreamReader(mem);
string   		contents = sr.ReadToEnd();
		

Use a Regex

{
	string	str = "a string";
	Regex r = new Regex(@"(\w+)", RegexOptions.Compiled));
	Match match = r.Match(str);
}

		

ASP.NET

Single sign on for Asp.Net apps Asp.net page events Restarting asp.net applications

WPF

Set default keyboard focus

In your Window opening tag add:
      FocusManager.FocusedElement="{Binding ElementName=_diagramEditor}"

Create a zoomable scrolling image/canvas

The canvas element in WPF is the only UI element which uses absolute placement for it's children. Unfortunatly it always tells it parent that it has zero size so wrapping it in a ScrollViewer doesn't work as expected. If you want to create an image you can zoom & pan around use the following:

<ScrollViewer Name="_scroller"
	Width="Auto" Height="Auto"
	HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"
	VerticalContentAlignment="Bottom">

	<Canvas Name="_canvas"
		Width="{Binding ElementName=_theImage, Path=ActualWidth}"
		Height="{Binding ElementName=_theImage, Path=ActualHeight}">
		<Image Name="_theImage" Stretch="None">
			<Image.Source>
				<BitmapImage UriSource="image.jpg" />
			</Image.Source>
		</Image>
		<Label Canvas.Top="40" Canvas.Left="40">This get scaled as well</Label>
	</Canvas>

</ScrollViewer>
		

Then you add some code to set the actual zoom by setting the LayoutTransform on the canvas:


private void zoomIn(object sender, RoutedEventArgs e) {
	ScaleTransform st = getScaleTransform();

	st.ScaleX += 1;
	st.ScaleY += 1;
}

private void zoomOut(object sender, RoutedEventArgs e) {
	ScaleTransform st = getScaleTransform();

	if(st.ScaleX > 1) {
		st.ScaleX -= 1;
		st.ScaleY -= 1;
	}
}

private ScaleTransform getScaleTransform() {
	ScaleTransform st = null;
	var obj = _canvas;

	if(obj.LayoutTransform is ScaleTransform)
		st = (ScaleTransform)obj.LayoutTransform;
		st = (ScaleTransform)obj.LayoutTransform;
	else {
		st = new ScaleTransform();
		obj.LayoutTransform = st;
	}
	return st;
}
		

If you don't have a single object which can define the size of the canvas, you'll have to derive your own class from the canvas object and override MeasureOverride; spin round all children and let them Measure themselves then just calc the max required bounds.


ssTk.co.uk
Last updated: 02 February 2012