private StringBuilder WordWrap(string message, int length)
{
StringBuilder build = new StringBuilder();
length++;
while (!string.IsNullOrWhiteSpace(message))
{
var lineLength = message.Length > length ? length : message.Length;
string line = message.Substring(0, lineLength);
if (!line.EndsWith(" "))
{
var lastSpace = line.LastIndexOf(" ");
if (lastSpace != -1)
lineLength = lastSpace;
line = message.Substring(0, lineLength);
}
build.AppendLine(line);
message = message.Remove(0, lineLength);
message = message.TrimStart();
}
return build;
}
Thursday, 5 May 2011
Tuesday, 27 July 2010
Distance between 2 Points
Search for this on the Internet and it gives you loads of technical details when all you want is a simple example you can use. So here it is
private static double GetlineLength(Point startPoint, Point endPoint)
{
var x = startPoint.X - endPoint.X;
var y = startPoint.Y - endPoint.Y;
return Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2));
}
I hope this ends your search :)
private static double GetlineLength(Point startPoint, Point endPoint)
{
var x = startPoint.X - endPoint.X;
var y = startPoint.Y - endPoint.Y;
return Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2));
}
I hope this ends your search :)
Tuesday, 8 June 2010
Sample Extension Method
public static class StringExtentionMethods
{
public static int WordCount(this String str)
{
return str.Split(new char[] { ' ', '.', '?' }, StringSplitOptions.RemoveEmptyEntries).Length;
}
}
{
public static int WordCount(this String str)
{
return str.Split(new char[] { ' ', '.', '?' }, StringSplitOptions.RemoveEmptyEntries).Length;
}
}
Thursday, 22 April 2010
// C# to convert a string to a byte array.
public static byte[] StrToByteArray(string str)
{
System.Text.ASCIIEncoding encoding=new System.Text.ASCIIEncoding();
return encoding.GetBytes(str);
}
// C# to convert a byte array to a string.
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
str = enc.GetString(dBytes);
public static byte[] StrToByteArray(string str)
{
System.Text.ASCIIEncoding encoding=new System.Text.ASCIIEncoding();
return encoding.GetBytes(str);
}
// C# to convert a byte array to a string.
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
str = enc.GetString(dBytes);
Wednesday, 23 September 2009
The Other Event Handler
Normally when I declare an Event I do it like this. The Easy way.
public event EventHandler Closing;
But you can have more power if you do it like this.
private EventHandler canClose;
public event EventHandler Closing
{
add { this.canClose += value; }
remove { this.canClose -= value; }
}
You can find out who is registering against your event.
Very handy..
public event EventHandler
But you can have more power if you do it like this.
private EventHandler
public event EventHandler
{
add { this.canClose += value; }
remove { this.canClose -= value; }
}
You can find out who is registering against your event.
Very handy..
Wednesday, 19 August 2009
C# Inline If Sample
Sample using WPF
Button newbutton = new Button();
bool a = true;
newbutton.Visibility = a ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
newbutton.Visibility = !String.IsNullEmpty(newbutton.Text) ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
Anything that evaluates to bool. If True do the command after the ? else do the command after the :
Thursday, 25 June 2009
DataTrigger Sample
Sample of Single Conditions
<Window.Resources>
<Style x:Key="Prosessing" TargetType="{x:Type ProgressBar}">
<Setter Property="Visibility" Value="Collapsed" />
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsProcessing}" Value="true">
<Setter Property="Visibility" Value="Visible" />
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
Example used on a Control
<ProgressBar Style="{StaticResource Prosessing}" Width="100" Height="15" Maximum="20" Value="0" x:Name="progressBar"/>
Sample of Multi Conditions
<Style TargetType="ListBoxItem">
<Setter Property="Background" Value="Black" />
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Path=Name}" Value="Portland" />
<Condition Binding="{Binding Path=State}" Value="OR" />
</MultiDataTrigger.Conditions>
<Setter Property="Background" Value="Red" />
</MultiDataTrigger>
</Style.Triggers>
</Style>
<Window.Resources>
<Style x:Key="Prosessing" TargetType="{x:Type ProgressBar}">
<Setter Property="Visibility" Value="Collapsed" />
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsProcessing}" Value="true">
<Setter Property="Visibility" Value="Visible" />
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
Example used on a Control
<ProgressBar Style="{StaticResource Prosessing}" Width="100" Height="15" Maximum="20" Value="0" x:Name="progressBar"/>
Sample of Multi Conditions
<Style TargetType="ListBoxItem">
<Setter Property="Background" Value="Black" />
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Path=Name}" Value="Portland" />
<Condition Binding="{Binding Path=State}" Value="OR" />
</MultiDataTrigger.Conditions>
<Setter Property="Background" Value="Red" />
</MultiDataTrigger>
</Style.Triggers>
</Style>
Subscribe to:
Posts (Atom)