tisdag 18 juni 2013

Count occurences of a substring within a string

If you want to figure out how many times a string appears within another string there are numerous ways of doing this. I found a nice way I like that i will share here.

Note that this will not work as intended if the substrings "overlap" each other.
So this example would need another solution: How many times does "bob" appear in "bobobob"?


This particular way of doing it works looks like this:

int count = ( string.Length - string.Replace(substring, string.empty).Length ) / substring.Length;

Basically we count how many times we can remove the substring from the string...


This solution came from here:
http://oldschooldotnet.blogspot.se/2008/12/c-counting-number-of-occurrences-of.html

onsdag 3 oktober 2012

WPF: Automatically scrolling/autoscrolling TextBox for MVVM pattern

If we want a TextBox to automatically scroll to the end and not make a new control inheriting from TextBox or use the view's code-behind we can create an attached property.

First we make a class like so:

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace NameSpace
{
    public class TextBoxAutomaticScrollingExtension
    {
        private static readonly Dictionary<TextBox, TextBoxScrollingTrigger> _textBoxesDictionary = 
            new Dictionary<TextBox, TextBoxScrollingTrigger>();

        public static readonly DependencyProperty ScrollOnTextChangedProperty =
            DependencyProperty.RegisterAttached(
                "ScrollOnTextChanged",
                typeof (bool),
                typeof (TextBoxAutomaticScrollingExtension),
                new UIPropertyMetadata(false, OnScrollOnTextChanged));

        private static void OnScrollOnTextChanged(DependencyObject dependencyObject,
                                                  DependencyPropertyChangedEventArgs e)
        {
            var textBox = dependencyObject as TextBox;
            if (textBox == null)
            {
                return;
            }

            bool oldValue = (bool) e.OldValue;
            bool newValue = (bool) e.NewValue;
            if (newValue == oldValue)
            {
                return;
            }

            if (newValue)
            {
                textBox.Loaded += OnTextBoxLoaded;
                textBox.Unloaded += OnTextBoxUnloaded;
            }
            else
            {
                textBox.Loaded -= OnTextBoxLoaded;
                textBox.Unloaded -= OnTextBoxUnloaded;

                if (_textBoxesDictionary.ContainsKey(textBox))
                {
                    _textBoxesDictionary[textBox].Dispose();
                }
            }
        }

        private static void OnTextBoxLoaded(object sender, RoutedEventArgs e)
        {
            var textBox = (TextBox) sender;
            textBox.Loaded -= OnTextBoxLoaded;
            _textBoxesDictionary[textBox] = new TextBoxScrollingTrigger(textBox);
        }

        private static void OnTextBoxUnloaded(object sender, RoutedEventArgs e)
        {
            var textBox = (TextBox) sender;
            textBox.Unloaded -= OnTextBoxUnloaded;
            _textBoxesDictionary[textBox].Dispose();
        }

        public static bool GetScrollOnTextChanged(DependencyObject dependencyObject)
        {
            return (bool) dependencyObject.GetValue(ScrollOnTextChangedProperty);
        }

        public static void SetScrollOnTextChanged(DependencyObject dependencyObject, bool value)
        {
            dependencyObject.SetValue(ScrollOnTextChangedProperty, value);
        }

        private class TextBoxScrollingTrigger : IDisposable
        {
            private TextBox TextBox { get; set; }

            public TextBoxScrollingTrigger(TextBox textBox)
            {
                TextBox = textBox;
                TextBox.TextChanged += OnTextBoxTextChanged;
            }

            private void OnTextBoxTextChanged(object sender, TextChangedEventArgs args)
            {
                TextBox.ScrollToEnd();
            }

            public void Dispose()
            {
                TextBox.TextChanged -= OnTextBoxTextChanged;
            }
        }

    }
}

Note: The textBoxesDictionary is for when there are more than one TextBox using this attached property, if we are only using one we can modify this code accordingly...

 Then we simply use it like this in XAML:
<TextBox ref:TextBoxAutomaticScrollingExtension.ScrollOnTextChanged="True" VerticalScrollBarVisibility="Auto" />

This solution is from this thread:
http://stackoverflow.com/questions/10097417/how-do-i-create-an-autoscrolling-textbox

tisdag 11 september 2012

SQL Server: Update expression when new value is a string with an accented character

If we want to update a string value and the new value has accented characters (e.g. č) one way to accomplish this is, in the SQL expression, to prefix the value with a capital letter 'n'.
For example like so:
UPDATE DB.table
SET Column = N'Količina'
WHERE Id = x

måndag 10 september 2012

WPF: Set a View's ViewModel for Designer to work against

To set a ViewModel for the Designer to work against you can do the following:
To the view's XAML code simply add the namespace to the viewmodel
xmlns:vm="clr-namespace:EOL.ViewModels"
and the following
d:DataContext="{d:DesignInstance Type=vm:ViewModel, IsDesignTimeCreatable=true}"

In the case of project named 'XYZ' with a UserControl as the view in a folder called  'Views' and a viewmodel called 'ViewModel' in the folder 'ViewModels' the XAML would look something like this:

<UserControl x:Class="XYZ.Views.View"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:vm="clr-namespace:XYZ.ViewModels"
             mc:Ignorable="d"
             d:DataContext="{d:DesignInstance Type=vm:ViewModel, IsDesignTimeCreatable=true}">

Note that the viewmodel must have a parameterless constructor for this to work...