locked
Split numbers in textbox RRS feed

  • Question

  • Hello. I wonder if its possible to split 10 numbers in a textbox. So if i write 1988551100 it write it in the textbox 1988-55-11-00
    Tuesday, March 9, 2010 6:32 PM

Answers

  • Hi Alcstudio,

    here's a sample window:

    <Window x:Class="WpfTests.Converter_SplitNumbers"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:WpfTests"
            Title="Converter_SplitNumbers" 
            SizeToContent="WidthAndHeight">
       <Window.Resources>
          <local:NumberToStringConverter x:Key="numberToStringConverter"/>
       </Window.Resources>
        <StackPanel>
          <TextBlock Text="{Binding TheValue, Converter={StaticResource numberToStringConverter}}"
                     Margin="10"/>
       </StackPanel>
    </Window>
    

    And the code-behind, including the converter:

    using System;
    using System.Windows;
    using System.Windows.Data;
    using System.Globalization;
    
    namespace WpfTests
    {
       public partial class Converter_SplitNumbers : Window
       {
          public string TheValue { get; set; }
    
          public Converter_SplitNumbers()
          {
             InitializeComponent();
             this.DataContext = this;
             TheValue = "1988551100";
          }
       }
    
       [ValueConversion(typeof(string), typeof(string))]
       public class NumberToStringConverter : IValueConverter
       {
          public object Convert(
                   object value,
                   Type targetType,
                   object parameter,
                   CultureInfo cultureInfo
                )
          {
             if (value == null) return string.Empty;
             string str = value.ToString();
             if (str.Equals(string.Empty) || str.Length<10) return str;
    
             return
                str.Substring(0, 4) + "-" + 
                str.Substring(4, 2) + "-" + 
                str.Substring(4, 2) + "-" + 
                str.Substring(4, 2);
          }
    
          public object ConvertBack(
                   object value, 
                   Type targetType, 
                   object parameter, CultureInfo cultureInfo)
          {
             throw new NotImplementedException();
          }
       }
    }


    Cheers,
    Olaf
    http://blogs.intuidev.com
    Wednesday, March 10, 2010 5:19 PM

All replies

  • Hi Alcstudio,

    you're probably looking for a masked TextBox control. Take a look at this StackOverflow thread where you'll find links to a couple of free ones. If, OTOH, you only want to display a string in a formatted fashion, you can use a simple converter - let me know if that's the case.


    Cheers,
    Olaf
    http://blogs.intuidev.com
    Wednesday, March 10, 2010 10:18 AM
  • Hi Alcstudio,

    Did the solution Olaf Rabbachin provided work for you? Could you please let us know the status of this thread? How about using RichTextBox as a workaround?

    Thanks.
    Sincerely.


    Jim Zhou -MSFT
    Wednesday, March 10, 2010 11:34 AM
  • i just want to display a string.
    Wednesday, March 10, 2010 5:08 PM
  • Hi Alcstudio,

    here's a sample window:

    <Window x:Class="WpfTests.Converter_SplitNumbers"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:WpfTests"
            Title="Converter_SplitNumbers" 
            SizeToContent="WidthAndHeight">
       <Window.Resources>
          <local:NumberToStringConverter x:Key="numberToStringConverter"/>
       </Window.Resources>
        <StackPanel>
          <TextBlock Text="{Binding TheValue, Converter={StaticResource numberToStringConverter}}"
                     Margin="10"/>
       </StackPanel>
    </Window>
    

    And the code-behind, including the converter:

    using System;
    using System.Windows;
    using System.Windows.Data;
    using System.Globalization;
    
    namespace WpfTests
    {
       public partial class Converter_SplitNumbers : Window
       {
          public string TheValue { get; set; }
    
          public Converter_SplitNumbers()
          {
             InitializeComponent();
             this.DataContext = this;
             TheValue = "1988551100";
          }
       }
    
       [ValueConversion(typeof(string), typeof(string))]
       public class NumberToStringConverter : IValueConverter
       {
          public object Convert(
                   object value,
                   Type targetType,
                   object parameter,
                   CultureInfo cultureInfo
                )
          {
             if (value == null) return string.Empty;
             string str = value.ToString();
             if (str.Equals(string.Empty) || str.Length<10) return str;
    
             return
                str.Substring(0, 4) + "-" + 
                str.Substring(4, 2) + "-" + 
                str.Substring(4, 2) + "-" + 
                str.Substring(4, 2);
          }
    
          public object ConvertBack(
                   object value, 
                   Type targetType, 
                   object parameter, CultureInfo cultureInfo)
          {
             throw new NotImplementedException();
          }
       }
    }


    Cheers,
    Olaf
    http://blogs.intuidev.com
    Wednesday, March 10, 2010 5:19 PM