question

MojtabaHakim-8125 avatar image
1 Vote"
MojtabaHakim-8125 asked DenysLehonkov-1615 published

How do I separate automaticaly the content of a textbox by number in WPF C# without binding?

want to show the numbers in TextBox with thousand separator without binding it and without programmatically

Just in XAML

like this example : 123456 => 123,456 or 1000000000 => 1,000,000,000

Also this line in XAML have error :

  <TextBox x:Name="text_main2" Text="{Binding StringFormat={}{0:N0}}"  VerticalAlignment="Top" Width="303"/>

Error :
117399-74bd41b8-1265-4ff1-bc06-ba77873b218c.png



Also I don't no can I do this work in TextBoxMask : xcdeed wpf toolkit

   <xctk:MaskedTextBox Mask="0000"  PromptChar=" " x:Name="txtname"  HorizontalContentAlignment="Right" HorizontalAlignment="Left" Height="22" Margin="512,296,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="152"/>


Please help me what's the easiest way to do this without additional code , like other software they have a property for textbox as : Thousands separated = true


windows-wpf
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

HuiLiu-MSFT avatar image
1 Vote"
HuiLiu-MSFT answered

For the TextBox to display the number with thousands separator, it is difficult for us to achieve it without binding or writing code. You could try to use String.Format as a workaround in the TextChanged method of TextBox.

The code of xaml is as follows:

 <StackPanel>
         <TextBox Name="textBox" Text="1234567890" TextChanged="textBox_TextChanged"/>
 </StackPanel>

The code of xaml.cs is as follows:

 private void textBox_TextChanged(object sender, EventArgs e)
     {
       if (!string.IsNullOrEmpty(textBox.Text))
       {
         System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo("en-US");
         var valueBefore = Int64.Parse(textBox.Text, System.Globalization.NumberStyles.AllowThousands);
         textBox.Text = String.Format(culture, "{0:N0}", valueBefore);
         textBox.Select(textBox.Text.Length, 0);
       }
     }

The result is shown in the figure:

117767-01.gif



01.gif (55.6 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DenysLehonkov-1615 avatar image
0 Votes"
DenysLehonkov-1615 answered DenysLehonkov-1615 published

I`ve encountered some Culture Problem, and also i dont want to separate with comma, but with space
Anyway, i used regex and hard-code, maybe someone will need this in future


  public static TextBox Separator(TextBox textBoxName)
         {
             string str = textBoxName.Text;
    
             switch (str)
             {
                 case var someVal when new Regex(@"^\d\d\d\d").IsMatch(str):
                     str = Regex.Replace(str, @"^.", str[0] + " ");
                     textBoxName.Text = str;
                     textBoxName.Select(textBoxName.Text.Length, 0);
                     break;
                 case var someVal when new Regex(@"^\d\s\d\d\d\d").IsMatch(str):
                     str = str.Replace(" ", "");
                     str = Regex.Replace(str, @"^..", str[0] + "" + str[1] + " ");
                     textBoxName.Text = str;
                     textBoxName.Select(textBoxName.Text.Length, 0);
                     break;
                 case var someVal when new Regex(@"^\d\d\s\d\d\d\d").IsMatch(str):
                     str = str.Replace(" ", "");
                     str = Regex.Replace(str, @"^...", str[0] + "" + str[1] + "" + str[2] + " ");
                     textBoxName.Text = str;
                     textBoxName.Select(textBoxName.Text.Length, 0);
                     break;
                 case var someVal when new Regex(@"^\d\d\d\s\d\d\d\d").IsMatch(str):
                     str = str.Replace(" ", "");
                     str = Regex.Replace(str, @"^.\d\d\d", str[0]+" "+str[1]+""+ str[2] + ""+ str[3] + " ");
                     textBoxName.Text = str;
                     textBoxName.Select(textBoxName.Text.Length, 0);
                     break;
                 case var someVal when new Regex(@"^\d\s\d\d\d\s\d\d\d\d").IsMatch(str):
                     str = str.Replace(" ", "");
                     str = Regex.Replace(str, @"^\d\d\d\d\d", str[0] + "" + str[1] + " " + str[2] + "" + str[3] + ""+ str[4] + " ");
                     textBoxName.Text = str;
                     textBoxName.Select(textBoxName.Text.Length, 0);
                     break;
                  case var someVal when new Regex(@"^\d\d\s\d\d\d\s\d\d\d\d").IsMatch(str):
                     str = str.Replace(" ", "");
                     str = Regex.Replace(str, @"^\d\d\d\d\d\d", str[0] + "" + str[1] + "" + str[2] + " " + str[3] + "" + str[4] + "" + str[5]+ " ");
                     textBoxName.Text = str;
                     textBoxName.Select(textBoxName.Text.Length, 0);
                     break;
                 case var someVal when new Regex(@"^\d\d\d\s\d\d\d\s\d\d\d\d").IsMatch(str):
                     str = str.Replace(" ", "");
                     str = Regex.Replace(str, @"^\d\d\d\d\d\d\d", str[0] + " " + str[1] + "" + str[2] + "" + str[3] + " " + str[4] + "" + str[5] + "" + str[6] + " ");
                     textBoxName.Text = str;
                     textBoxName.Select(textBoxName.Text.Length, 0);
                     break;
    
             }
    
             return textBoxName;
         }

226182-testwindow-2022-07-29-13-55-34.gif



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.